<commit_before># -*- coding: utf-8 -*-
import os
from couchdb_schematics.document import SchematicsDocument
from datetime import datetime, timedelta, time
from iso8601 import parse_date, ParseError
from pytz import timezone
from pyramid.security import Allow
from schematics.exceptions import ConversionError, ValidationError
from schematics.models import Model as SchematicsModel
from schematics.transforms import whitelist, blacklist, export_loop, convert
from schematics.types import StringType, FloatType, IntType, URLType, BooleanType, BaseType, EmailType, MD5Type
from schematics.types.compound import ModelType, DictType, ListType as BaseListType
from schematics.types.serializable import serializable
from uuid import uuid4
from barbecue import vnmax
from zope.interface import implementer, Interface
from openprocurement.api.models import (
    IsoDateTimeType, ListType, Model, Value, PeriodEndRequired, SANDBOX_MODE,
    Classification, validate_dkpp, Item, Document, Organization, Parameter, validate_parameters_uniq,
    LotValue, Bid, Revision, Question,  Cancellation, Contract, Award, Feature,
    Lot, schematics_embedded_role, schematics_default_role, ORA_CODES, WORKING_DAYS,
    validate_features_uniq, validate_items_uniq, validate_lots_uniq, Period,
    Complaint as BaseComplaint, TZ, get_now, set_parent, ComplaintModelType,
)
from openprocurement.auctions.core.models import IAuction, get_auction

STAND_STILL_TIME = timedelta(days=2)
COMPLAINT_STAND_STILL_TIME = timedelta(days=3)
BIDDER_TIME = timedelta(minutes=6)
SERVICE_TIME = timedelta(minutes=9)
AUCTION_STAND_STILL_TIME = timedelta(minutes=15)


def read_json(name):
    import os.path
    from json import loads
    curr_dir = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(curr_dir, name)
    with open(file_path) as lang_file:
        data = lang_file.read()
    return loads(data)


CAV_CODES = read_json('cav.json')


class CAVClassification(Classification):
    scheme = StringType(required=True, default=u'CAV', choices=[u'CAV'])
    id = StringType(required=True, choices=CAV_CODES)


class Guarantee(Model):
    amount = FloatType(required=True, min_value=0)  # Amount as a number.
    currency = StringType(required=True, default=u'UAH', max_length=3, min_length=3)  # The currency in 3-letter ISO 4217 format.


def calc_auction_end_time(bids, start):
    return start + bids * BIDDER_TIME + SERVICE_TIME + AUCTION_STAND_STILL_TIME


def rounding_shouldStartAfter(start_after, auction, use_from=datetime(2016, 6, 1, tzinfo=TZ)):
    if (auction.enquiryPeriod and auction.enquiryPeriod.startDate or get_now()) > use_from and not (SANDBOX_MODE and auction.submissionMethodDetails and u'quick' in auction.submissionMethodDetails):
        midnigth = datetime.combine(start_after.date(), time(0, tzinfo=start_after.tzinfo))
        if start_after > midnigth:
            start_after = midnigth + timedelta(1)
    return start_after


class AuctionAuctionPeriod(Period):
    """The auction period."""

    @serializable(serialize_when_none=False)
    def shouldStartAfter(self):
        if self.endDate:
            return
        auction = self.__parent__
        if auction.lots or auction.status not in ['active.tendering', 'active.auction']:
            return
        if self.startDate and get_now() > calc_auction_end_time(auction.numberOfBids, self.startDate):
            start_after = calc_auction_end_time(auction.numberOfBids, self.startDate)
        else:
            start_after = auction.tenderPeriod.endDate
        return rounding_shouldStartAfter(start_after, auction).isoformat()


class LotAuctionPeriod(Period):
    """The auction period."""
<commit_msg>Fix rounding_shouldStartAfter when start_after equal midnight<commit_after># -*- coding: utf-8 -*-
import os
from couchdb_schematics.document import SchematicsDocument
from datetime import datetime, timedelta, time
from iso8601 import parse_date, ParseError
from pytz import timezone
from pyramid.security import Allow
from schematics.exceptions import ConversionError, ValidationError
from schematics.models import Model as SchematicsModel
from schematics.transforms import whitelist, blacklist, export_loop, convert
from schematics.types import StringType, FloatType, IntType, URLType, BooleanType, BaseType, EmailType, MD5Type
from schematics.types.compound import ModelType, DictType, ListType as BaseListType
from schematics.types.serializable import serializable
from uuid import uuid4
from barbecue import vnmax
from zope.interface import implementer, Interface
from openprocurement.api.models import (
    IsoDateTimeType, ListType, Model, Value, PeriodEndRequired, SANDBOX_MODE,
    Classification, validate_dkpp, Item, Document, Organization, Parameter, validate_parameters_uniq,
    LotValue, Bid, Revision, Question,  Cancellation, Contract, Award, Feature,
    Lot, schematics_embedded_role, schematics_default_role, ORA_CODES, WORKING_DAYS,
    validate_features_uniq, validate_items_uniq, validate_lots_uniq, Period,
    Complaint as BaseComplaint, TZ, get_now, set_parent, ComplaintModelType,
)
from openprocurement.auctions.core.models import IAuction, get_auction

STAND_STILL_TIME = timedelta(days=2)
COMPLAINT_STAND_STILL_TIME = timedelta(days=3)
BIDDER_TIME = timedelta(minutes=6)
SERVICE_TIME = timedelta(minutes=9)
AUCTION_STAND_STILL_TIME = timedelta(minutes=15)


def read_json(name):
    import os.path
    from json import loads
    curr_dir = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(curr_dir, name)
    with open(file_path) as lang_file:
        data = lang_file.read()
    return loads(data)


CAV_CODES = read_json('cav.json')


class CAVClassification(Classification):
    scheme = StringType(required=True, default=u'CAV', choices=[u'CAV'])
    id = StringType(required=True, choices=CAV_CODES)


class Guarantee(Model):
    amount = FloatType(required=True, min_value=0)  # Amount as a number.
    currency = StringType(required=True, default=u'UAH', max_length=3, min_length=3)  # The currency in 3-letter ISO 4217 format.


def calc_auction_end_time(bids, start):
    return start + bids * BIDDER_TIME + SERVICE_TIME + AUCTION_STAND_STILL_TIME


def rounding_shouldStartAfter(start_after, auction, use_from=datetime(2016, 6, 1, tzinfo=TZ)):
    if (auction.enquiryPeriod and auction.enquiryPeriod.startDate or get_now()) > use_from and not (SANDBOX_MODE and auction.submissionMethodDetails and u'quick' in auction.submissionMethodDetails):
        midnigth = datetime.combine(start_after.date(), time(0, tzinfo=start_after.tzinfo))
        if start_after >= midnigth:
            start_after = midnigth + timedelta(1)
    return start_after


class AuctionAuctionPeriod(Period):
    """The auction period."""

    @serializable(serialize_when_none=False)
    def shouldStartAfter(self):
        if self.endDate:
            return
        auction = self.__parent__
        if auction.lots or auction.status not in ['active.tendering', 'active.auction']:
            return
        if self.startDate and get_now() > calc_auction_end_time(auction.numberOfBids, self.startDate):
            start_after = calc_auction_end_time(auction.numberOfBids, self.startDate)
        else:
            start_after = auction.tenderPeriod.endDate
        return rounding_shouldStartAfter(start_after, auction).isoformat()


class LotAuctionPeriod(Period):
    """The auction period."""

{'fraction_non_alphanumeric': 0.083344553655581, 'fraction_numerical': 0.007809344284367847, 'mean_word_length': 5.058727569331158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4427659', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2261, 'n_words': 557}