Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Emails): pouvoir envoyer les e-mails transactionnels depuis TemplateTransactional #1034

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lemarche/conversations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")}),
)
Original file line number Diff line number Diff line change
@@ -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",
),
]
54 changes: 51 additions & 3 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -202,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
Expand Down Expand Up @@ -235,3 +253,33 @@ 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,
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,
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
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,
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
subject=subject,
from_email=from_email,
from_name=from_name,
)
2 changes: 1 addition & 1 deletion lemarche/siaes/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -118,20 +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,
)
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
Expand Down
20 changes: 11 additions & 9 deletions lemarche/utils/apis/api_mailjet.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def add_to_contact_list_async(email_address, properties, contact_list_id, client

@task()
def send_transactional_email_with_template(
template_id,
subject,
recipient_email,
recipient_name,
variables,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
template_id: int,
recipient_email: str,
recipient_name: str,
variables: dict,
subject: str,
from_email: str,
from_name: str,
client=None,
):
data = {
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
12 changes: 6 additions & 6 deletions lemarche/www/dashboard_siaes/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading