Skip to content

Commit

Permalink
revert log of transactional mail
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Jan 8, 2025
1 parent 171abff commit 0cc72a8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 61 deletions.
14 changes: 11 additions & 3 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ 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,
Expand All @@ -322,9 +325,6 @@ def send_transactional_email(
):
if self.is_active:
args = {
"template_transactional": self,
"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,
Expand All @@ -333,6 +333,14 @@ def send_transactional_email(
"from_email": from_email,
"from_name": from_name,
}

# 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()
)

if self.source == conversation_constants.SOURCE_MAILJET:
api_mailjet.send_transactional_email_with_template(**args)
elif self.source == conversation_constants.SOURCE_BREVO:
Expand Down
29 changes: 0 additions & 29 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from huey.contrib.djhuey import task
from sib_api_v3_sdk.rest import ApiException

from lemarche.conversations.constants import SOURCE_BREVO
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
Expand Down Expand Up @@ -349,9 +348,6 @@ def get_all_users_from_list(

@task()
def send_transactional_email_with_template(
template_transactional,
recipient_content_object,
parent_content_object,
template_id: int,
recipient_email: str,
recipient_name: str,
Expand All @@ -372,37 +368,12 @@ 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.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}")
Expand Down
29 changes: 0 additions & 29 deletions lemarche/utils/apis/api_mailjet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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

Expand Down Expand Up @@ -96,9 +95,6 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client

@task()
def send_transactional_email_with_template(
template_transactional,
recipient_content_object,
parent_content_object,
template_id: int,
recipient_email: str,
recipient_name: str,
Expand Down Expand Up @@ -126,37 +122,12 @@ 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.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': '<recipient_email>', 'MessageUUID': '<uuid>', 'MessageID': <id>, 'MessageHref': 'https://api.mailjet.com/v3/REST/message/<id>'}], '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)
Expand Down

0 comments on commit 0cc72a8

Please sign in to comment.