-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 'update_tender_status_to_rejected' command with its tests
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
lemarche/tenders/management/commands/update_tender_satus_to_rejected.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters