From c0b8f1a4c28ff39ed0e653c2de53047c066f2679 Mon Sep 17 00:00:00 2001 From: Chloend Date: Tue, 24 Dec 2024 11:04:27 +0100 Subject: [PATCH] Create 'update_tender_status_to_rejected' command with its tests --- .../update_tender_satus_to_rejected.py | 43 +++++++++++++++++++ lemarche/tenders/tests/test_commands.py | 37 ++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 lemarche/tenders/management/commands/update_tender_satus_to_rejected.py diff --git a/lemarche/tenders/management/commands/update_tender_satus_to_rejected.py b/lemarche/tenders/management/commands/update_tender_satus_to_rejected.py new file mode 100644 index 000000000..fc369e48f --- /dev/null +++ b/lemarche/tenders/management/commands/update_tender_satus_to_rejected.py @@ -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") diff --git a/lemarche/tenders/tests/test_commands.py b/lemarche/tenders/tests/test_commands.py index 9d6d501fb..1aac37e13 100644 --- a/lemarche/tenders/tests/test_commands.py +++ b/lemarche/tenders/tests/test_commands.py @@ -1,3 +1,4 @@ +from datetime import timedelta from io import StringIO from unittest.mock import patch @@ -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 @@ -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)