From 9cab0e1796d4d58ccd27e101b5028aeb7cfec8e9 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Wed, 14 Feb 2024 17:50:34 +0100 Subject: [PATCH] clean tasks to save the log in the tenders only when partners count is postive --- lemarche/www/tenders/tasks.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lemarche/www/tenders/tasks.py b/lemarche/www/tenders/tasks.py index d873ebcbd..85c1982aa 100644 --- a/lemarche/www/tenders/tasks.py +++ b/lemarche/www/tenders/tasks.py @@ -113,20 +113,22 @@ def send_tender_emails_to_partners(tender: Tender): All corresponding partners (PartnerShareTender) will be contacted """ partners = PartnerShareTender.objects.filter_by_tender(tender) - email_subject = f"{tender.get_kind_display()} : {tender.title} ({tender.author.company_name})" - - for partner in partners: - send_tender_email_to_partner(email_subject, tender, partner) - - # log email batch - log_item = { - "action": "email_partners_matched", - "email_subject": email_subject, - "email_count": partners.count(), - "email_timestamp": timezone.now().isoformat(), - } - tender.logs.append(log_item) - tender.save() + partners_count = partners.count() + + if partners_count > 0: + email_subject = f"{tender.get_kind_display()} : {tender.title} ({tender.author.company_name})" + for partner in partners: + send_tender_email_to_partner(email_subject, tender, partner) + + # log email batch + log_item = { + "action": "email_partners_matched", + "email_subject": email_subject, + "email_count": partners_count, + "email_timestamp": timezone.now().isoformat(), + } + tender.logs.append(log_item) + tender.save() def send_tender_email_to_partner(email_subject: str, tender: Tender, partner: PartnerShareTender):