def tender_init_handler(event): """ initialization handler for closeFrameworkAgreementSelectionUA tenders """ tender = event.tender default_status = type(tender).fields["status"].default if tender.status != default_status: raise Exception( "Allow create tender only in ({default_status}) status".format( default_status=default_status)) if not tender.enquiryPeriod: tender.enquiryPeriod = PeriodEndRequired() tender.enquiryPeriod["__parent__"] = tender if not tender.enquiryPeriod.startDate: tender.enquiryPeriod.startDate = get_now() if not tender.enquiryPeriod.endDate: tender.enquiryPeriod.endDate = tender.enquiryPeriod.startDate + TenderCfaSelectionUAConfigurator.enquiry_period if not tender.tenderPeriod: tender.tenderPeriod = PeriodEndRequired() tender.tenderPeriod["__parent__"] = tender if not tender.tenderPeriod.startDate: tender.tenderPeriod.startDate = tender.enquiryPeriod.endDate if not tender.tenderPeriod.endDate: tender.tenderPeriod.endDate = tender.tenderPeriod.startDate + TenderCfaSelectionUAConfigurator.tender_period now = get_now() tender.date = now if tender.lots: for lot in tender.lots: lot.date = now
def test_get_tender(self): period = PeriodEndRequired({ 'startDate': datetime.now(TZ).isoformat(), 'endDate': datetime.now(TZ).isoformat() }) second_period = PeriodEndRequired({ 'startDate': datetime.now(TZ).isoformat(), 'endDate': datetime.now(TZ).isoformat() }) tender = Tender() period._data['__parent__'] = tender second_period._data['__parent__'] = period parent_tender = get_tender(second_period) self.assertEqual(tender, parent_tender) self.assertIsInstance(parent_tender, Tender) self.assertIsInstance(tender, Tender) period._data['__parent__'] = None with self.assertRaises(AttributeError) as e: get_tender(second_period) self.assertEqual(e.exception.message, "'NoneType' object has no attribute '__parent__'")
def test_validate_start_date(self, mocked_get_tender): start_date = datetime.now(TZ) end_date = datetime.now(TZ) + timedelta(minutes=3) model = PeriodEndRequired({ 'startDate': end_date.isoformat(), 'endDate': start_date.isoformat() }) with self.assertRaises(ModelValidationError) as e: model.validate() self.assertEqual( e.exception.message, {'startDate': [u'period should begin before its end']}) revision = MagicMock() revision.date = datetime.now(TZ) mocked_get_tender.return_value = {'revisions': [revision]} model = PeriodEndRequired({'endDate': end_date.isoformat()}) with self.assertRaises(ModelValidationError) as e: model.validate() self.assertEqual(e.exception.message, {'startDate': [u'This field cannot be deleted']}) model = PeriodEndRequired({ 'startDate': start_date.isoformat(), 'endDate': end_date.isoformat() }) model.validate() self.assertEqual(start_date, model.startDate) self.assertEqual(end_date, model.endDate)
def test_should_start_after(self): tender_period = PeriodEndRequired({ 'startDate': datetime.now(TZ).isoformat(), 'endDate': (datetime.now(TZ) + timedelta(minutes=10)).isoformat() }) tender_enquiry_period = PeriodEndRequired({ 'startDate': datetime.now(TZ).isoformat(), 'endDate': (datetime.now(TZ) + timedelta(minutes=10)).isoformat() }) tender = Tender({'status': 'active.enquiries'}) tender.lots = [] tender.numberOfBids = 2 tender.tenderPeriod = tender_period tender.enquiryPeriod = tender_enquiry_period start_date = datetime.now(TZ) end_date = datetime.now(TZ) + timedelta(minutes=5) tender_auction_period = TenderAuctionPeriod({ 'startDate': start_date.isoformat(), 'endDate': end_date.isoformat() }) # Test with endDate exist serialized = tender_auction_period.serialize() self.assertEqual(serialized, { 'startDate': start_date.isoformat(), 'endDate': end_date.isoformat() }) # Test with wrong status tender_auction_period = TenderAuctionPeriod( {'startDate': start_date.isoformat()}) tender_auction_period.__parent__ = tender serialized = tender_auction_period.serialize() self.assertEqual(serialized, {'startDate': start_date.isoformat()}) # Test with get_now() less than calc_auction_end_time() tender_auction_period = TenderAuctionPeriod( {'startDate': start_date.isoformat()}) tender.status = 'active.tendering' tender_auction_period.__parent__ = tender serialized = tender_auction_period.serialize() should_start_after = datetime.combine( (tender.tenderPeriod.endDate.date() + timedelta(days=1)), time(0, tzinfo=tender.tenderPeriod.endDate.tzinfo)) self.assertEqual( serialized, { 'startDate': start_date.isoformat(), 'shouldStartAfter': should_start_after.isoformat() }) # Test with get_now() greater then calc_auction tender_auction_period.startDate -= timedelta(days=1) serialized = tender_auction_period.serialize() should_start_after = datetime.combine( (tender_auction_period.startDate.date() + timedelta(days=1)), time(0, tzinfo=tender_auction_period.startDate.tzinfo)) self.assertEqual( serialized, { 'startDate': tender_auction_period.startDate.isoformat(), 'shouldStartAfter': should_start_after.isoformat() })
def test_should_start_after(self): tender_period = PeriodEndRequired({ "startDate": datetime.now(TZ).isoformat(), "endDate": (datetime.now(TZ) + timedelta(minutes=10)).isoformat(), }) tender_enquiry_period = PeriodEndRequired({ "startDate": datetime.now(TZ).isoformat(), "endDate": (datetime.now(TZ) + timedelta(minutes=10)).isoformat(), }) tender = Tender({"status": "active.enquiries"}) tender.lots = [] tender.numberOfBids = 2 tender.tenderPeriod = tender_period tender.enquiryPeriod = tender_enquiry_period start_date = datetime.now(TZ) end_date = datetime.now(TZ) + timedelta(minutes=5) tender_auction_period = TenderAuctionPeriod({ "startDate": start_date.isoformat(), "endDate": end_date.isoformat() }) # Test with endDate exist serialized = tender_auction_period.serialize() self.assertEqual(serialized, { "startDate": start_date.isoformat(), "endDate": end_date.isoformat() }) # Test with wrong status tender_auction_period = TenderAuctionPeriod( {"startDate": start_date.isoformat()}) tender_auction_period.__parent__ = tender serialized = tender_auction_period.serialize() self.assertEqual(serialized, {"startDate": start_date.isoformat()}) # Test with get_now() less than calc_auction_end_time() tender_auction_period = TenderAuctionPeriod( {"startDate": start_date.isoformat()}) tender.status = "active.tendering" tender_auction_period.__parent__ = tender serialized = tender_auction_period.serialize() should_start_after = datetime.combine( (tender.tenderPeriod.endDate.date() + timedelta(days=1)), time(0, tzinfo=tender.tenderPeriod.endDate.tzinfo)) self.assertEqual( serialized, { "startDate": start_date.isoformat(), "shouldStartAfter": should_start_after.isoformat() }) # Test with get_now() greater then calc_auction tender_auction_period.startDate -= timedelta(days=1) serialized = tender_auction_period.serialize() should_start_after = datetime.combine( (tender_auction_period.startDate.date() + timedelta(days=1)), time(0, tzinfo=tender_auction_period.startDate.tzinfo), ) # TODO: investigate fail starting at 23:24:00 to 24:00:00 self.assertEqual( serialized, { "startDate": tender_auction_period.startDate.isoformat(), "shouldStartAfter": should_start_after.isoformat(), }, )
def test_validate_start_date(self, mocked_get_tender): start_date = datetime.now(TZ) end_date = datetime.now(TZ) + timedelta(minutes=3) model = PeriodEndRequired({"startDate": end_date.isoformat(), "endDate": start_date.isoformat()}) with self.assertRaises(ModelValidationError) as e: model.validate() self.assertEqual(e.exception.messages, {"startDate": ["period should begin before its end"]}) mocked_get_tender.return_value = {"revisions": [{"date": datetime.now(TZ).isoformat()}]} model = PeriodEndRequired({"endDate": end_date.isoformat()}) with self.assertRaises(ModelValidationError) as e: model.validate() self.assertEqual(e.exception.messages, {"startDate": ["This field cannot be deleted"]}) model = PeriodEndRequired({"startDate": start_date.isoformat(), "endDate": end_date.isoformat()}) model.validate() self.assertEqual(start_date, model.startDate) self.assertEqual(end_date, model.endDate)