Skip to content

Commit

Permalink
Create 'update_tender_status_to_rejected' command with its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Dec 24, 2024
1 parent c6118a9 commit c0b8f1a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.utils import timezone

from lemarche.tenders import constants as tender_constants
from lemarche.tenders.models import Tender


class Command(BaseCommand):
help = "Si aucune modification n'est apportée dans les 10 jours suivant la demande, le Besoin est rejeté."

def handle(self, *args, **options):
threshold_date = timezone.now() - timedelta(days=10)
tenders_to_update = []

tenders_draft = Tender.objects.filter(status=tender_constants.STATUS_DRAFT)
tenders_draft_count = tenders_draft.count()

self.stdout.write(f"Besoin(s) à traiter : {tenders_draft_count}")

for tender in tenders_draft:
email_sent_at = None
for log_entry in tender.logs:
if log_entry.get("action") == "send_tender_author_modification_request":
email_sent_at = log_entry.get("email_sent_at")
break

if email_sent_at:
email_sent_at_date = timezone.now().strptime(email_sent_at, "%Y-%m-%dT%H:%M:%S.%f%z")
if email_sent_at_date <= threshold_date:
tenders_to_update.append(tender)

for tender in tenders_to_update:
tender.status = tender_constants.STATUS_REJECTED
tender.save(update_fields=["status"])

if not tenders_to_update:
self.stdout.write("Aucun besoin rejeté")
elif len(tenders_to_update) == 1:
self.stdout.write("1 besoin rejeté")
else:
self.stdout.write(f"{len(tenders_to_update)} besoins rejetés")
37 changes: 37 additions & 0 deletions lemarche/tenders/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from io import StringIO
from unittest.mock import patch

Expand All @@ -8,6 +9,7 @@
from lemarche.sectors.factories import SectorFactory
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.factories import SiaeActivityFactory, SiaeFactory
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.factories import TenderFactory
from lemarche.tenders.models import TenderSiae
from lemarche.users.factories import UserFactory
Expand Down Expand Up @@ -182,3 +184,38 @@ def test_command_on_weekday_dry_run(self, mock_now, mock_send_email):
self.assertNotIn("Email sent to tender author", output)

mock_send_email.assert_not_called()


class UpdateTenderStatusToRejectedCommandTest(TestCase):
def test_update_tender_status_to_rejected(self):
"""
Test 'update_tender_status_to_rejected' command.
"""
recent_date = timezone.now() - timedelta(days=5)
threshold_date = timezone.now() - timedelta(days=10)

tender_recent = TenderFactory(
status=tender_constants.STATUS_DRAFT,
logs=[
{"action": "send_tender_author_modification_request", "email_sent_at": recent_date.isoformat()},
],
)

tender_expired = TenderFactory(
status=tender_constants.STATUS_DRAFT,
logs=[
{"action": "send_tender_author_modification_request", "email_sent_at": threshold_date.isoformat()},
],
)

tender_with_no_modification_request = TenderFactory(status=tender_constants.STATUS_DRAFT)

call_command("update_tender_status_to_rejected")

tender_recent.refresh_from_db()
tender_expired.refresh_from_db()
tender_with_no_modification_request.refresh_from_db()

self.assertEqual(tender_recent.status, tender_constants.STATUS_DRAFT)
self.assertEqual(tender_expired.status, tender_constants.STATUS_REJECTED)
self.assertEqual(tender_with_no_modification_request.status, tender_constants.STATUS_DRAFT)

0 comments on commit c0b8f1a

Please sign in to comment.