# Path: openprocurement/tender/openeu/constants.py
# TENDERING_DURATION = timedelta(days=TENDERING_DAYS)
# 
# QUESTIONS_STAND_STILL = timedelta(days=10)
# 
# TENDERING_AUCTION = timedelta(days=35)
# 
# BID_UNSUCCESSFUL_FROM = datetime(2016, 10, 18, tzinfo=TZ)
# 
# TENDERING_DAYS = 30


from uuid import uuid4
from datetime import timedelta, datetime, time
from iso8601 import parse_date
from pyramid.security import Allow
from zope.interface import implementer
from schematics.types import StringType, MD5Type, BooleanType
from schematics.types.compound import ModelType
from schematics.types.serializable import serializable
from schematics.transforms import blacklist, whitelist, export_loop
from schematics.exceptions import ValidationError
from urlparse import urlparse, parse_qs
from string import hexdigits
from openprocurement.api.utils import get_now
from openprocurement.api.constants import TZ
from openprocurement.api.models import (
    listing_role, Address, Period, Model,
    IsoDateTimeType, ListType, SifterListType, Identifier as BaseIdentifier,
    ContactPoint as BaseContactPoint, plain_role
)
from openprocurement.api.validation import (
    validate_cpv_group, validate_items_uniq
)
from openprocurement.tender.core.models import (
    ITender,
    Bid as BaseBid,
    Contract as BaseContract,
    Cancellation as BaseCancellation,
    Lot as BaseLot,
    Document as BaseDocument,
    LotValue as BaseLotValue,
    ComplaintModelType as BaseComplaintModelType,
    EnquiryPeriod,
    PeriodStartEndRequired,
    create_role, edit_role, view_role,
    auction_view_role, auction_post_role, auction_patch_role, enquiries_role,
    auction_role, chronograph_role, chronograph_view_role, view_bid_role,
    Administrator_bid_role, Administrator_role, schematics_default_role,
    schematics_embedded_role, embedded_lot_role, default_lot_role,
    get_tender, validate_lots_uniq,
    rounding_shouldStartAfter,
    validate_parameters_uniq,
    bids_validation_wrapper
)
from openprocurement.tender.core.utils import (
    calculate_business_date,
    calc_auction_end_time,
    has_unanswered_questions,
    has_unanswered_complaints,
)
from openprocurement.tender.belowthreshold.models import (
    Tender as BaseTender
)
from openprocurement.tender.openua.utils import (
    calculate_normalized_date
)
from openprocurement.tender.openua.models import (
    Complaint as BaseComplaint,
    Award as BaseAward,
    Item as BaseItem,
    Tender as OpenUATender,
    Parameter
)
from openprocurement.tender.openua.constants import (
    COMPLAINT_SUBMIT_TIME,
    ENQUIRY_STAND_STILL_TIME,
    AUCTION_PERIOD_TIME,
)
from openprocurement.tender.openeu.constants import (
    TENDERING_DURATION,
    QUESTIONS_STAND_STILL,
    TENDERING_AUCTION,
    BID_UNSUCCESSFUL_FROM,
    TENDERING_DAYS
)
        from openprocurement.api.utils import generate_docservice_url
        if tender.status == 'active.tendering' and tender.tenderPeriod.endDate:
            start_after = calculate_business_date(tender.tenderPeriod.endDate, TENDERING_AUCTION, tender)
        elif self.startDate and get_now() > calc_auction_end_time(tender.numberOfBids, self.startDate):
            start_after = calc_auction_end_time(tender.numberOfBids, self.startDate)
        elif tender.qualificationPeriod and tender.qualificationPeriod.endDate:
            decision_dates = [
                datetime.combine(complaint.dateDecision.date() + timedelta(days=3), time(0, tzinfo=complaint.dateDecision.tzinfo))
                for qualification in tender.qualifications
                for complaint in qualification.complaints
                if complaint.dateDecision
            ]
            decision_dates.append(tender.qualificationPeriod.endDate)
            start_after = max(decision_dates)
        if start_after:
            return rounding_shouldStartAfter(start_after, tender).isoformat()


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

    @serializable(serialize_when_none=False)
    def shouldStartAfter(self):
        if self.endDate:
            return
        tender = get_tender(self)
        lot = self.__parent__
        if tender.status not in ['active.tendering', 'active.pre-qualification.stand-still', 'active.auction'] or lot.status != 'active':
            return
        start_after = None
        if tender.status == 'active.tendering' and tender.tenderPeriod.endDate:

# Path: openprocurement/tender/openeu/constants.py
# TENDERING_DURATION = timedelta(days=TENDERING_DAYS)
# 
# QUESTIONS_STAND_STILL = timedelta(days=10)
# 
# TENDERING_AUCTION = timedelta(days=35)
# 
# BID_UNSUCCESSFUL_FROM = datetime(2016, 10, 18, tzinfo=TZ)
# 
# TENDERING_DAYS = 30


# Path: openprocurement/tender/openeu/models.py
from uuid import uuid4
from datetime import timedelta, datetime, time
from iso8601 import parse_date
from pyramid.security import Allow
from zope.interface import implementer
from schematics.types import StringType, MD5Type, BooleanType
from schematics.types.compound import ModelType
from schematics.types.serializable import serializable
from schematics.transforms import blacklist, whitelist, export_loop
from schematics.exceptions import ValidationError
from urlparse import urlparse, parse_qs
from string import hexdigits
from openprocurement.api.utils import get_now
from openprocurement.api.constants import TZ
from openprocurement.api.models import (
    listing_role, Address, Period, Model,
    IsoDateTimeType, ListType, SifterListType, Identifier as BaseIdentifier,
    ContactPoint as BaseContactPoint, plain_role
)
from openprocurement.api.validation import (
    validate_cpv_group, validate_items_uniq
)
from openprocurement.tender.core.models import (
    ITender,
    Bid as BaseBid,
    Contract as BaseContract,
    Cancellation as BaseCancellation,
    Lot as BaseLot,
    Document as BaseDocument,
    LotValue as BaseLotValue,
    ComplaintModelType as BaseComplaintModelType,
    EnquiryPeriod,
    PeriodStartEndRequired,
    create_role, edit_role, view_role,
    auction_view_role, auction_post_role, auction_patch_role, enquiries_role,
    auction_role, chronograph_role, chronograph_view_role, view_bid_role,
    Administrator_bid_role, Administrator_role, schematics_default_role,
    schematics_embedded_role, embedded_lot_role, default_lot_role,
    get_tender, validate_lots_uniq,
    rounding_shouldStartAfter,
    validate_parameters_uniq,
    bids_validation_wrapper
)
from openprocurement.tender.core.utils import (
    calculate_business_date,
    calc_auction_end_time,
    has_unanswered_questions,
    has_unanswered_complaints,
)
from openprocurement.tender.belowthreshold.models import (
    Tender as BaseTender
)
from openprocurement.tender.openua.utils import (
    calculate_normalized_date
)
from openprocurement.tender.openua.models import (
    Complaint as BaseComplaint,
    Award as BaseAward,
    Item as BaseItem,
    Tender as OpenUATender,
    Parameter
)
from openprocurement.tender.openua.constants import (
    COMPLAINT_SUBMIT_TIME,
    ENQUIRY_STAND_STILL_TIME,
    AUCTION_PERIOD_TIME,
)
from openprocurement.tender.openeu.constants import (
    TENDERING_DURATION,
    QUESTIONS_STAND_STILL,
    TENDERING_AUCTION,
    BID_UNSUCCESSFUL_FROM,
    TENDERING_DAYS
)
        from openprocurement.api.utils import generate_docservice_url

        if tender.status == 'active.tendering' and tender.tenderPeriod.endDate:
            start_after = calculate_business_date(tender.tenderPeriod.endDate, TENDERING_AUCTION, tender)
        elif self.startDate and get_now() > calc_auction_end_time(tender.numberOfBids, self.startDate):
            start_after = calc_auction_end_time(tender.numberOfBids, self.startDate)
        elif tender.qualificationPeriod and tender.qualificationPeriod.endDate:
            decision_dates = [
                datetime.combine(complaint.dateDecision.date() + timedelta(days=3), time(0, tzinfo=complaint.dateDecision.tzinfo))
                for qualification in tender.qualifications
                for complaint in qualification.complaints
                if complaint.dateDecision
            ]
            decision_dates.append(tender.qualificationPeriod.endDate)
            start_after = max(decision_dates)
        if start_after:
            return rounding_shouldStartAfter(start_after, tender).isoformat()


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

    @serializable(serialize_when_none=False)
    def shouldStartAfter(self):
        if self.endDate:
            return
        tender = get_tender(self)
        lot = self.__parent__
        if tender.status not in ['active.tendering', 'active.pre-qualification.stand-still', 'active.auction'] or lot.status != 'active':
            return
        start_after = None
        if tender.status == 'active.tendering' and tender.tenderPeriod.endDate:

            start_after = calculate_business_date(tender.tenderPeriod.endDate, TENDERING_AUCTION, tender)