diff --git a/lemarche/tenders/management/commands/send_validated_tenders.py b/lemarche/tenders/management/commands/send_validated_tenders.py index cb7914909..57feb8ed4 100644 --- a/lemarche/tenders/management/commands/send_validated_tenders.py +++ b/lemarche/tenders/management/commands/send_validated_tenders.py @@ -19,14 +19,14 @@ class Command(BaseCommand): def handle(self, *args, **options): # First send newly validated tenders - validated_tenders_to_send = Tender.objects.validated_but_not_sent() + validated_tenders_to_send = Tender.objects.validated_but_not_sent().is_not_outdated() if validated_tenders_to_send.count(): self.stdout.write(f"Found {validated_tenders_to_send.count()} validated tender(s) to send") for tender in validated_tenders_to_send: send_validated_tender(tender) # Then look at already sent tenders (batch mode) - validated_sent_tenders_batch_to_send = Tender.objects.validated_sent_batch() + validated_sent_tenders_batch_to_send = Tender.objects.validated_sent_batch().is_not_outdated() if validated_sent_tenders_batch_to_send.count(): self.stdout.write( f"Found {validated_sent_tenders_batch_to_send.count()} validated sent tender(s) to batch" diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index c1cea1dd9..ac13d487a 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -74,7 +74,6 @@ def validated_but_not_sent(self): def validated_sent_batch(self): yesterday = timezone.now() - timedelta(days=1) - return ( self.with_siae_stats() .validated() @@ -95,8 +94,11 @@ def is_incremental(self): ] ) + def is_not_outdated(self): + return self.filter(deadline_date__gte=datetime.today()) + def is_live(self): - return self.sent().filter(deadline_date__gte=datetime.today()) + return self.sent().is_not_outdated() def has_amount(self): return self.filter(Q(amount__isnull=False) | Q(amount_exact__isnull=False)).annotate( diff --git a/lemarche/tenders/tests/test_models.py b/lemarche/tenders/tests/test_models.py index 88fb5f425..8a9b2f3fa 100644 --- a/lemarche/tenders/tests/test_models.py +++ b/lemarche/tenders/tests/test_models.py @@ -360,8 +360,17 @@ def test_validated_sent_batch(self): ) self.assertEqual(Tender.objects.validated_sent_batch().count(), 1) + def test_is_not_outdated(self): + TenderFactory(deadline_date=timezone.now() + timedelta(days=1)) + TenderFactory(deadline_date=timezone.now() - timedelta(days=1)) + # TenderFactory(deadline_date=None) # cannot be None + self.assertEqual(Tender.objects.is_not_outdated().count(), 1) + def test_is_live(self): TenderFactory(deadline_date=timezone.now() + timedelta(days=1)) + TenderFactory( + deadline_date=timezone.now() + timedelta(days=1), first_sent_at=None, status=tender_constants.STATUS_DRAFT + ) TenderFactory(deadline_date=timezone.now() - timedelta(days=1)) # TenderFactory(deadline_date=None) # cannot be None self.assertEqual(Tender.objects.is_live().count(), 1)