From 867e0b0ae035fa1ace4a76e7c28ec940bbf29c24 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 9 Jan 2024 15:36:19 +0100 Subject: [PATCH 1/3] Emails: send email directly from TemplateTransactional object --- lemarche/conversations/models.py | 24 ++++++++++++++++++++++++ lemarche/utils/apis/api_mailjet.py | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lemarche/conversations/models.py b/lemarche/conversations/models.py index 8c8249253..5edf630b1 100644 --- a/lemarche/conversations/models.py +++ b/lemarche/conversations/models.py @@ -10,6 +10,7 @@ from shortuuid import uuid from lemarche.conversations import constants as conversation_constants +from lemarche.utils.apis import api_brevo, api_mailjet class ConversationQuerySet(models.QuerySet): @@ -235,3 +236,26 @@ def get_template_id(self): elif self.source == conversation_constants.SOURCE_BREVO: return self.brevo_id return None + + def send_transactional_email(self, recipient_email, recipient_name, variables): + print("send_transactional_email", self.source) + if self.source == conversation_constants.SOURCE_MAILJET: + api_mailjet.send_transactional_email_with_template( + template_id=self.get_template_id, + subject=self.email_subject, + recipient_email=recipient_email, + recipient_name=recipient_name, + variables=variables, + # from_email=self.email_from_email, + # from_name=self.email_from_name, + ) + elif self.source == conversation_constants.SOURCE_BREVO: + api_brevo.send_transactional_email_with_template( + template_id=self.get_template_id, + subject=self.email_subject, + recipient_email=recipient_email, + recipient_name=recipient_name, + variables=variables, + # from_email=self.email_from_email, + # from_name=self.email_from_name, + ) diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index 3208ed45b..d6ec9b61a 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -95,7 +95,7 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client @task() def send_transactional_email_with_template( - template_id, + template_id: int, subject, recipient_email, recipient_name, From b24b16def73eb15ee353d14ea42dc45273a7eae9 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 29 Apr 2024 18:36:32 +0200 Subject: [PATCH 2/3] comment subject. add missin types --- lemarche/conversations/models.py | 3 +-- lemarche/utils/apis/api_brevo.py | 1 + lemarche/utils/apis/api_mailjet.py | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lemarche/conversations/models.py b/lemarche/conversations/models.py index 5edf630b1..e3643663e 100644 --- a/lemarche/conversations/models.py +++ b/lemarche/conversations/models.py @@ -238,7 +238,6 @@ def get_template_id(self): return None def send_transactional_email(self, recipient_email, recipient_name, variables): - print("send_transactional_email", self.source) if self.source == conversation_constants.SOURCE_MAILJET: api_mailjet.send_transactional_email_with_template( template_id=self.get_template_id, @@ -252,7 +251,7 @@ def send_transactional_email(self, recipient_email, recipient_name, variables): elif self.source == conversation_constants.SOURCE_BREVO: api_brevo.send_transactional_email_with_template( template_id=self.get_template_id, - subject=self.email_subject, + # subject=self.email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 29a7eb103..56d9c0204 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -128,6 +128,7 @@ def send_transactional_email_with_template( to=[{"email": recipient_email, "name": recipient_name}], template_id=template_id, params=variables, + # subject is managed in Brevo ) if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED: diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index d6ec9b61a..8e0d82f59 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -96,10 +96,10 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client @task() def send_transactional_email_with_template( template_id: int, - subject, - recipient_email, - recipient_name, - variables, + subject: str, + recipient_email: str, + recipient_name: str, + variables: dict, from_email=settings.DEFAULT_FROM_EMAIL, from_name=settings.DEFAULT_FROM_NAME, client=None, From 172a2c67c564ebe3f43eacb31fd782ae9de77072 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 30 Apr 2024 12:12:48 +0200 Subject: [PATCH 3/3] Cleanup subject, from_email & from_name --- lemarche/conversations/admin.py | 3 +- ...transactional_email_from_email_and_more.py | 24 ++++++++++ lemarche/conversations/models.py | 45 ++++++++++++++----- lemarche/siaes/tasks.py | 2 +- lemarche/utils/apis/api_brevo.py | 23 ++++++---- lemarche/utils/apis/api_mailjet.py | 12 ++--- lemarche/www/auth/tasks.py | 2 +- lemarche/www/dashboard_siaes/tasks.py | 12 ++--- lemarche/www/tenders/tasks.py | 14 +++--- 9 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 lemarche/conversations/migrations/0015_remove_templatetransactional_email_from_email_and_more.py diff --git a/lemarche/conversations/admin.py b/lemarche/conversations/admin.py index 969e9bb64..786dd2c1d 100644 --- a/lemarche/conversations/admin.py +++ b/lemarche/conversations/admin.py @@ -137,11 +137,10 @@ class TemplateTransactionalAdmin(admin.ModelAdmin): list_display = ["id", "name", "code", "mailjet_id", "brevo_id", "source", "is_active", "created_at", "updated_at"] search_fields = ["id", "name", "code", "mailjet_id", "brevo_id"] - readonly_fields = ["code", "email_subject", "email_from_email", "email_from_name", "created_at", "updated_at"] + readonly_fields = ["code", "created_at", "updated_at"] fieldsets = ( (None, {"fields": ("name", "code", "description")}), - ("Paramètres de l'e-mail", {"fields": ("email_subject", "email_from_email", "email_from_name")}), ("Paramètres d'envoi", {"fields": ("mailjet_id", "brevo_id", "source", "is_active")}), ("Dates", {"fields": ("created_at", "updated_at")}), ) diff --git a/lemarche/conversations/migrations/0015_remove_templatetransactional_email_from_email_and_more.py b/lemarche/conversations/migrations/0015_remove_templatetransactional_email_from_email_and_more.py new file mode 100644 index 000000000..622988d58 --- /dev/null +++ b/lemarche/conversations/migrations/0015_remove_templatetransactional_email_from_email_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.9 on 2024-04-30 09:57 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("conversations", "0014_alter_templatetransactional_source"), + ] + + operations = [ + migrations.RemoveField( + model_name="templatetransactional", + name="email_from_email", + ), + migrations.RemoveField( + model_name="templatetransactional", + name="email_from_name", + ), + migrations.RemoveField( + model_name="templatetransactional", + name="email_subject", + ), + ] diff --git a/lemarche/conversations/models.py b/lemarche/conversations/models.py index e3643663e..526d59dec 100644 --- a/lemarche/conversations/models.py +++ b/lemarche/conversations/models.py @@ -203,9 +203,26 @@ class TemplateTransactional(models.Model): ) description = models.TextField(verbose_name="Description", blank=True) - email_subject = models.CharField(verbose_name="E-mail : objet", max_length=255, blank=True, null=True) - email_from_email = models.EmailField(verbose_name="E-mail : expéditeur (e-mail)", blank=True, null=True) - email_from_name = models.CharField(verbose_name="E-mail : expéditeur (nom)", max_length=255, blank=True, null=True) + # email_subject = models.CharField( + # verbose_name="E-mail : objet", + # help_text="Laisser vide pour utiliser l'objet présent dans Mailjet/Brevo", + # max_length=255, + # blank=True, + # null=True, + # ) + # email_from_email = models.EmailField( + # verbose_name="E-mail : expéditeur (e-mail)", + # help_text=f"Laisser vide pour utiliser l'e-mail expéditeur par défaut ({settings.DEFAULT_FROM_EMAIL})", + # blank=True, + # null=True, + # ) + # email_from_name = models.CharField( + # verbose_name="E-mail : expéditeur (nom)", + # help_text=f"Laisser vide pour utiliser le nom expéditeur par défaut ({settings.DEFAULT_FROM_NAME})", + # max_length=255, + # blank=True, + # null=True, + # ) mailjet_id = models.IntegerField( verbose_name="Identifiant Mailjet", unique=True, db_index=True, blank=True, null=True @@ -237,24 +254,32 @@ def get_template_id(self): return self.brevo_id return None - def send_transactional_email(self, recipient_email, recipient_name, variables): + def send_transactional_email( + self, + recipient_email, + recipient_name, + variables, + subject=None, + from_email=settings.DEFAULT_FROM_EMAIL, + from_name=settings.DEFAULT_FROM_NAME, + ): if self.source == conversation_constants.SOURCE_MAILJET: api_mailjet.send_transactional_email_with_template( template_id=self.get_template_id, - subject=self.email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, - # from_email=self.email_from_email, - # from_name=self.email_from_name, + subject=subject, + from_email=from_email, + from_name=from_name, ) elif self.source == conversation_constants.SOURCE_BREVO: api_brevo.send_transactional_email_with_template( template_id=self.get_template_id, - # subject=self.email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, - # from_email=self.email_from_email, - # from_name=self.email_from_name, + subject=subject, + from_email=from_email, + from_name=from_name, ) diff --git a/lemarche/siaes/tasks.py b/lemarche/siaes/tasks.py index df25ca4a3..b145d7845 100644 --- a/lemarche/siaes/tasks.py +++ b/lemarche/siaes/tasks.py @@ -52,10 +52,10 @@ def send_completion_reminder_email_to_siae(siae): api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAE_COMPLETION_REMINDER_TEMPLATE_ID, - subject=email_subject, recipient_email=siae_user_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 56d9c0204..5d9d25fb9 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -6,6 +6,7 @@ from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException +from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX from lemarche.utils.urls import get_object_admin_url, get_object_share_url @@ -118,21 +119,25 @@ def send_transactional_email_with_template( recipient_email: str, recipient_name: str, variables: dict, - from_email=settings.DEFAULT_FROM_EMAIL, - from_name=settings.DEFAULT_FROM_NAME, + subject: str, + from_email: str, + from_name: str, ): api_client = get_api_client() api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client) - send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( - sender={"email": from_email, "name": from_name}, - to=[{"email": recipient_email, "name": recipient_name}], - template_id=template_id, - params=variables, - # subject is managed in Brevo - ) + data = { + "sender": {"email": from_email, "name": from_name}, + "to": [{"email": recipient_email, "name": recipient_name}], + "template_id": template_id, + "params": variables, + } + # if subject empty, defaults to Brevo's template subject + if subject: + data["subject"] = EMAIL_SUBJECT_PREFIX + subject if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED: try: + send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(**data) response = api_instance.send_transac_email(send_smtp_email) logger.info("Brevo: send transactional email with template") return response diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index 8e0d82f59..d00339489 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -96,12 +96,12 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client @task() def send_transactional_email_with_template( template_id: int, - subject: str, recipient_email: str, recipient_name: str, variables: dict, - from_email=settings.DEFAULT_FROM_EMAIL, - from_name=settings.DEFAULT_FROM_NAME, + subject: str, + from_email: str, + from_name: str, client=None, ): data = { @@ -111,12 +111,14 @@ def send_transactional_email_with_template( "To": [{"Email": recipient_email, "Name": recipient_name}], "TemplateID": template_id, "TemplateLanguage": True, - "Subject": EMAIL_SUBJECT_PREFIX + subject, "Variables": variables, - # "Variables": {} } ] } + # if subject empty, defaults to Mailjet's template subject + if subject: + data["Messages"][0]["Subject"] = EMAIL_SUBJECT_PREFIX + subject + if not client: client = get_default_client() diff --git a/lemarche/www/auth/tasks.py b/lemarche/www/auth/tasks.py index a0914f635..3a4958318 100644 --- a/lemarche/www/auth/tasks.py +++ b/lemarche/www/auth/tasks.py @@ -55,8 +55,8 @@ def send_new_user_password_reset_link(user: User): api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_NEW_USER_PASSWORD_RESET_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) diff --git a/lemarche/www/dashboard_siaes/tasks.py b/lemarche/www/dashboard_siaes/tasks.py index 642721934..ad8458def 100644 --- a/lemarche/www/dashboard_siaes/tasks.py +++ b/lemarche/www/dashboard_siaes/tasks.py @@ -26,10 +26,10 @@ def send_siae_user_request_email_to_assignee(siae_user_request): api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAEUSERREQUEST_ASSIGNEE_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -68,10 +68,10 @@ def send_siae_user_request_response_email_to_initiator(siae_user_request, respon api_mailjet.send_transactional_email_with_template( template_id=email_template_id, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -112,10 +112,10 @@ def send_siae_user_request_reminder_3_days_email_to_assignee(siae_user_request): api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAEUSERREQUEST_REMINDER_1_ASSIGNEE_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -145,10 +145,10 @@ def send_siae_user_request_reminder_3_days_email_to_initiator(siae_user_request) api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAEUSERREQUEST_REMINDER_1_INITIATOR_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -189,10 +189,10 @@ def send_siae_user_request_reminder_8_days_email_to_assignee(siae_user_request): api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAEUSERREQUEST_REMINDER_2_ASSIGNEE_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -223,10 +223,10 @@ def send_siae_user_request_reminder_8_days_email_to_initiator(siae_user_request) api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_SIAEUSERREQUEST_REMINDER_2_INITIATOR_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email diff --git a/lemarche/www/tenders/tasks.py b/lemarche/www/tenders/tasks.py index 0dbf01ea3..d3741d499 100644 --- a/lemarche/www/tenders/tasks.py +++ b/lemarche/www/tenders/tasks.py @@ -206,10 +206,10 @@ def send_tender_email_to_siae(tender: Tender, siae: Siae, email_subject: str, re api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_TENDERS_SIAE_PRESENTATION_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, from_email=settings.TEAM_CONTACT_EMAIL, from_name="Pauline du Marché de l'inclusion", ) @@ -296,10 +296,10 @@ def send_tender_contacted_reminder_email_to_siae( api_mailjet.send_transactional_email_with_template( template_id=template_id, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -370,10 +370,10 @@ def send_tender_interested_reminder_email_to_siae( api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_TENDERS_SIAE_INTERESTED_REMINDER_2D_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -415,10 +415,10 @@ def send_confirmation_published_email_to_author(tender: Tender, nb_matched_siaes api_mailjet.send_transactional_email_with_template( template_id=settings.MAILJET_TENDERS_AUTHOR_CONFIRMATION_VALIDATED_TEMPLATE_ID, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -485,10 +485,10 @@ def send_siae_interested_email_to_author(tender: Tender): api_mailjet.send_transactional_email_with_template( template_id=template_id, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -563,10 +563,10 @@ def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"): api_mailjet.send_transactional_email_with_template( template_id=template_id, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # log email @@ -634,10 +634,10 @@ def send_tenders_siae_survey(tendersiae: TenderSiae, kind="transactioned_questio api_mailjet.send_transactional_email_with_template( template_id=template_id, - subject=email_subject, recipient_email=recipient_email, recipient_name=recipient_name, variables=variables, + subject=email_subject, ) # update tendersiae