Skip to content

Commit

Permalink
Cleaup some duplicate logs. Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jul 22, 2024
1 parent c85c506 commit c3d8b94
Showing 1 changed file with 50 additions and 88 deletions.
138 changes: 50 additions & 88 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def send_tender_emails_to_siaes(tender: Tender):
# @task()
def send_tender_email_to_siae(tender: Tender, siae: Siae, email_subject: str, recipient_to_override: User = None):
email_template = TemplateTransactional.objects.get(code="TENDERS_SIAE_PRESENTATION")
tendersiae = TenderSiae.objects.get(tender=tender, siae=siae)
# override siae.contact_email if email_to_override is provided
email_to = recipient_to_override.email if recipient_to_override else siae.contact_email
recipient_list = whitelist_recipient_list([email_to])
Expand Down Expand Up @@ -143,6 +144,7 @@ def send_tender_email_to_siae(tender: Tender, siae: Siae, email_subject: str, re
"TENDER_DEADLINE_DATE": date_to_string(tender.deadline_date),
"TENDER_URL": tender_url,
"TENDER_NOT_INTERESTED_URL": tender_not_interested_url,
"TENDERSIAE_ID": tendersiae.id,
}

email_template.send_transactional_email(
Expand All @@ -154,7 +156,6 @@ def send_tender_email_to_siae(tender: Tender, siae: Siae, email_subject: str, re
)

# update tendersiae
tendersiae = TenderSiae.objects.get(tender=tender, siae=siae)
tendersiae.email_send_date = timezone.now()
log_item = {
"action": "email_sent",
Expand All @@ -177,7 +178,7 @@ def send_tender_emails_to_partners(tender: Tender):
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)
send_tender_email_to_partner(tender, partner, email_subject)

# log email batch
log_item = {
Expand All @@ -190,7 +191,7 @@ def send_tender_emails_to_partners(tender: Tender):
tender.save()


def send_tender_email_to_partner(email_subject: str, tender: Tender, partner: PartnerShareTender):
def send_tender_email_to_partner(tender: Tender, partner: PartnerShareTender, email_subject: str):
recipient_list = whitelist_recipient_list(partner.contact_email_list)
if recipient_list:
variables = {
Expand Down Expand Up @@ -290,6 +291,7 @@ def send_tender_contacted_reminder_email_to_siae(tendersiae: TenderSiae, email_t
"TENDER_AMOUNT": tendersiae.tender.amount_display,
"TENDER_DEADLINE_DATE": date_to_string(tendersiae.tender.deadline_date),
"TENDER_URL": f"{get_object_share_url(tendersiae.tender)}?siae_id={tendersiae.siae.id}&mtm_campaign=relance-esi-contactees", # noqa
"TENDERSIAE_ID": tendersiae.id,
}

email_template.send_transactional_email(
Expand Down Expand Up @@ -366,6 +368,7 @@ def send_tender_interested_reminder_email_to_siae(
"TENDER_AMOUNT": tendersiae.tender.amount_display,
"TENDER_DEADLINE_DATE": date_to_string(tendersiae.tender.deadline_date),
"TENDER_URL": f"{get_object_share_url(tendersiae.tender)}?siae_id={tendersiae.siae.id}&mtm_campaign=relance-esi-interessees", # noqa
"TENDERSIAE_ID": tendersiae.id,
}

email_template.send_transactional_email(
Expand All @@ -391,46 +394,35 @@ def send_confirmation_published_email_to_author(tender: Tender):
"""
Send email to the author when the tender is published to the siaes
"""
if not tender.contact_notifications_disabled:
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_CONFIRMATION_VALIDATED")
recipient_list = whitelist_recipient_list([tender.author.email])
if len(recipient_list):
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_CONFIRMATION_VALIDATED")
recipient_list = whitelist_recipient_list([tender.author.email])
if len(recipient_list):
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

variables = {
"TENDER_ID": tender.id,
"TENDER_TITLE": tender.title,
"TENDER_KIND": tender.get_kind_display(),
"TENDER_KIND_LOWER": tender.get_kind_display().lower(),
"TENDER_SECTORS": tender.sectors_list_string(),
"TENDER_PERIMETERS": tender.location_display,
"TENDER_AMOUNT": tender.amount_display,
"TENDER_DEADLINE_DATE": date_to_string(tender.deadline_date),
"TENDER_AUTHOR_ID": tender.author.id,
"TENDER_AUTHOR_FIRST_NAME": tender.author.first_name,
"TENDER_NB_MATCH": tender.siaes.count(),
"TENDER_URL": get_object_share_url(tender),
}
variables = {
"TENDER_ID": tender.id,
"TENDER_TITLE": tender.title,
"TENDER_KIND": tender.get_kind_display(),
"TENDER_KIND_LOWER": tender.get_kind_display().lower(),
"TENDER_SECTORS": tender.sectors_list_string(),
"TENDER_PERIMETERS": tender.location_display,
"TENDER_AMOUNT": tender.amount_display,
"TENDER_DEADLINE_DATE": date_to_string(tender.deadline_date),
"TENDER_AUTHOR_ID": tender.author.id,
"TENDER_AUTHOR_FIRST_NAME": tender.author.first_name,
"TENDER_NB_MATCH": tender.siaes.count(),
"TENDER_URL": get_object_share_url(tender),
}

if not tender.contact_notifications_disabled:
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)

# log email
log_item = {
"action": "email_publish_confirmation",
"email_template": email_template.code,
"email_to": recipient_email,
# "email_body": email_body,
"email_timestamp": timezone.now().isoformat(),
}
tender.logs.append(log_item)
tender.save()


def send_siae_interested_email_to_author(tender: Tender):
"""
Expand Down Expand Up @@ -468,7 +460,7 @@ def send_siae_interested_email_to_author(tender: Tender):

if should_send_email:
recipient_list = whitelist_recipient_list([tender.author.email]) # tender.contact_email ?
if len(recipient_list) and not tender.contact_notifications_disabled:
if len(recipient_list):
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

Expand All @@ -480,23 +472,13 @@ def send_siae_interested_email_to_author(tender: Tender):
"TENDER_SIAE_INTERESTED_LIST_URL": f"{get_object_share_url(tender)}/prestataires", # noqa
}

email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)

# log email
log_item = {
"action": "email_siae_interested",
"email_template": email_template.code,
"email_to": recipient_email,
# "email_body": email_body,
"email_timestamp": timezone.now().isoformat(),
}
tender.logs.append(log_item)
tender.save()
if not tender.contact_notifications_disabled:
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)


def notify_admin_tender_created(tender: Tender):
Expand Down Expand Up @@ -529,7 +511,7 @@ def notify_admin_tender_created(tender: Tender):

def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"):
recipient_list = whitelist_recipient_list([tender.author.email])
if len(recipient_list) and not tender.contact_notifications_disabled:
if len(recipient_list):
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

Expand Down Expand Up @@ -559,23 +541,13 @@ def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"):
else:
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_FEEDBACK_30D")

email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)

# log email
log_item = {
"action": f"email_{kind}_sent",
"email_template": email_template.code,
"email_to": recipient_email,
# "email_body": email_body,
"email_timestamp": timezone.now().isoformat(),
}
tender.logs.append(log_item)
tender.save()
if not tender.contact_notifications_disabled:
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)


def send_tenders_siaes_survey(tender: Tender, kind="transactioned_question_7d"):
Expand Down Expand Up @@ -681,7 +653,7 @@ def notify_admin_siae_wants_cocontracting(tender: Tender, siae: Siae):
def send_super_siaes_email_to_author(tender: Tender, top_siaes: list[Siae]):
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SUPER_SIAES")
recipient_list = whitelist_recipient_list([tender.author.email])
if len(recipient_list) and not tender.contact_notifications_disabled:
if len(recipient_list):
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

Expand Down Expand Up @@ -710,20 +682,10 @@ def send_super_siaes_email_to_author(tender: Tender, top_siaes: list[Siae]):
}
)

email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)

# log email
log_item = {
"action": "email_super_siaes",
"email_template": email_template.code,
"email_to": recipient_email,
"email_timestamp": timezone.now().isoformat(),
"email_variables": variables,
}
tender.logs.append(log_item)
tender.save()
if not tender.contact_notifications_disabled:
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
content_object=tender.author,
)

0 comments on commit c3d8b94

Please sign in to comment.