From daa98f663d6f1c77631312062e15609d22c70858 Mon Sep 17 00:00:00 2001 From: Guilouf Date: Fri, 27 Dec 2024 23:31:13 +0100 Subject: [PATCH] Mail log creation on task end --- lemarche/conversations/models.py | 12 +++-------- lemarche/utils/apis/api_brevo.py | 31 +++++++++++++++++++++++++++++ lemarche/utils/apis/api_mailjet.py | 32 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lemarche/conversations/models.py b/lemarche/conversations/models.py index 282960831..9da6cf999 100644 --- a/lemarche/conversations/models.py +++ b/lemarche/conversations/models.py @@ -309,9 +309,6 @@ def get_template_id(self): return self.brevo_id return None - def create_send_log(self, **kwargs): - TemplateTransactionalSendLog.objects.create(template_transactional=self, **kwargs) - def send_transactional_email( self, recipient_email, @@ -325,6 +322,9 @@ def send_transactional_email( ): if self.is_active: args = { + "object_model_id": self.id, + "recipient_content_object": recipient_content_object, + "parent_content_object": parent_content_object, "template_id": self.get_template_id, "recipient_email": recipient_email, "recipient_name": recipient_name, @@ -337,12 +337,6 @@ def send_transactional_email( api_mailjet.send_transactional_email_with_template(**args) elif self.source == conversation_constants.SOURCE_BREVO: api_brevo.send_transactional_email_with_template(**args) - # create log - self.create_send_log( - recipient_content_object=recipient_content_object, - parent_content_object=parent_content_object, - extra_data={"source": self.source, "args": args}, # "response": result() - ) class TemplateTransactionalSendLog(models.Model): diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 39c399982..da333cb9a 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -7,6 +7,8 @@ from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException +from lemarche.conversations.constants import SOURCE_BREVO +from lemarche.conversations.models import TemplateTransactional from lemarche.tenders import constants as tender_constants from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX from lemarche.utils.data import sanitize_to_send_by_email @@ -348,6 +350,9 @@ def get_all_users_from_list( @task() def send_transactional_email_with_template( + object_model_id: int, + recipient_content_object, + parent_content_object, template_id: int, recipient_email: str, recipient_name: str, @@ -368,12 +373,38 @@ def send_transactional_email_with_template( if subject: data["subject"] = EMAIL_SUBJECT_PREFIX + subject + log_args = { + "source": SOURCE_BREVO, + "args": { + "template_id": template_id, + "recipient_email": recipient_email, + "recipient_name": recipient_name, + "variables": variables, + "subject": subject, + "from_email": from_email, + "from_name": from_name, + }, + } + + # create log + template_transactional = TemplateTransactional.objects.get(pk=object_model_id) + template_transactional.send_logs.create( + recipient_content_object=recipient_content_object, + parent_content_object=parent_content_object, + extra_data={ + **log_args, + "sent": settings.BITOUBI_ENV in ENV_NOT_ALLOWED, # considered successfully in tests and dev + }, + ) + 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") # {'message_id': '<202407151419.84958140835@smtp-relay.mailin.fr>', 'message_ids': None} + template_transactional.extra_data = {**log_args, "sent": True} + template_transactional.save(update_fields=["extra_data"]) return response.to_dict() except ApiException as e: print(f"Exception when calling SMTPApi->send_transac_email: {e}") diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index d1e95fd64..9b2ea5dab 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -4,6 +4,7 @@ from django.conf import settings from huey.contrib.djhuey import task +from lemarche.conversations.constants import SOURCE_MAILJET from lemarche.users import constants as user_constants from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX @@ -95,6 +96,9 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client @task() def send_transactional_email_with_template( + object_model_id: int, + recipient_content_object, + parent_content_object, template_id: int, recipient_email: str, recipient_name: str, @@ -104,6 +108,8 @@ def send_transactional_email_with_template( from_name=settings.DEFAULT_FROM_NAME, client=None, ): + from lemarche.conversations.models import TemplateTransactional + data = { "Messages": [ { @@ -122,12 +128,38 @@ def send_transactional_email_with_template( if not client: client = get_default_client() + log_args = { + "source": SOURCE_MAILJET, + "args": { + "template_id": template_id, + "recipient_email": recipient_email, + "recipient_name": recipient_name, + "variables": variables, + "subject": subject, + "from_email": from_email, + "from_name": from_name, + }, + } + + # create log + template_transactional = TemplateTransactional.objects.get(pk=object_model_id) + template_transactional.send_logs.create( + recipient_content_object=recipient_content_object, + parent_content_object=parent_content_object, + extra_data={ + **log_args, + "sent": settings.BITOUBI_ENV in ENV_NOT_ALLOWED, # considered successfully in tests and dev + }, + ) + if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED: try: response = client.post(SEND_URL, json=data) response.raise_for_status() logger.info("Mailjet: send transactional email with template") # {'Messages': [{'Status': 'success', 'CustomID': '', 'To': [{'Email': '', 'MessageUUID': '', 'MessageID': , 'MessageHref': 'https://api.mailjet.com/v3/REST/message/'}], 'Cc': [], 'Bcc': []}]} # noqa + template_transactional.extra_data = {**log_args, "sent": True} + template_transactional.save(update_fields=["extra_data"]) return response.json() except requests.exceptions.HTTPError as e: logger.error("Error while fetching `%s`: %s", e.request.url, e)