def to_native(self, value, context=None): if isinstance(value, datetime): return value try: date = parse_date(value, None) if not date.tzinfo: date = TZ.localize(date) return date except ParseError: raise ConversionError(self.messages['parse'].format(value)) except OverflowError as e: raise ConversionError(e.message)
def initialize(self): if not self.enquiryPeriod: self.enquiryPeriod = type(self).enquiryPeriod.model_class() if not self.tenderPeriod: self.tenderPeriod = type(self).tenderPeriod.model_class() now = get_now() start_date = TZ.localize(self.auctionPeriod.startDate.replace(tzinfo=None)) self.tenderPeriod.startDate = self.enquiryPeriod.startDate = now pause_between_periods = start_date - (start_date.replace(hour=20, minute=0, second=0, microsecond=0) - timedelta(days=1)) end_date = calculate_business_date(start_date, -pause_between_periods, self) self.enquiryPeriod.endDate = end_date self.tenderPeriod.endDate = self.enquiryPeriod.endDate if not self.rectificationPeriod: self.rectificationPeriod = generate_rectificationPeriod(self) self.rectificationPeriod.startDate = now self.auctionPeriod.startDate = None self.auctionPeriod.endDate = None self.date = now if self.lots: for lot in self.lots: lot.date = now
def generate_rectificationPeriod_tender_period_margin( auction, tp_margin=MINIMAL_PERIOD_FROM_RECTIFICATION_END): """Generate rectificationPeriod for an auction basing on margin from the end of the tenderPeriod :param auction: auction model :param tp_margin: timedelta; margin from the end of the tenderPertiod to the end of rectificationPeriod. It's default value is 5 days. """ now = get_now() period = auction.rectificationPeriod if not period: period = type(auction).rectificationPeriod.model_class() period.startDate = period.startDate or now if not period.endDate: calculated_endDate = TZ.localize( calculate_business_date(auction.tenderPeriod.endDate, -tp_margin, auction).replace(tzinfo=None)) period.endDate = calculated_endDate if calculated_endDate > now else now period.invalidationDate = None return period
def test_to_primitive_datetime(self): dt = TZ.localize(datetime(2020, 1, 1, 12, 0, 0)) dt_str_result = IsoDateTimeType().to_primitive(dt) dt_str_expected = "2020-01-01T12:00:00+02:00" self.assertEqual(dt_str_result, dt_str_expected)
def test_to_native_datetime(self): dt = TZ.localize(datetime(2020, 1, 1, 12, 0, 0)) dt_result = IsoDateTimeType().to_native(dt) self.assertEqual(dt_result, dt)
def test_to_native_string_with_no_time_and_tz(self): dt_str = "2020-01-01" dt_result = IsoDateTimeType().to_native(dt_str) dt_expected = TZ.localize(datetime(2020, 1, 1)) self.assertEqual(dt_result, dt_expected)
def test_to_native_string(self): dt_str = "2020-01-01T12:00:00+02:00" dt_result = IsoDateTimeType().to_native(dt_str) dt_expected = TZ.localize(datetime(2020, 1, 1, 12, 0, 0)) self.assertEqual(dt_result, dt_expected)
def validate_rectificationPeriod(self, data, period): if not (period and period.startDate) or not period.endDate: return if period.endDate > TZ.localize(calculate_business_date(data['tenderPeriod']['endDate'], -MINIMAL_PERIOD_FROM_RECTIFICATION_END, data).replace(tzinfo=None)): raise ValidationError(u"rectificationPeriod.endDate should come at least 5 working days earlier than tenderPeriod.endDate")