From bd1b5e0aad7db416ab08b4202a0c320c10e32746 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Fri, 29 Dec 2023 12:43:01 +0100 Subject: [PATCH 1/4] add method to add and remove contact list to brevo and update api settings --- config/settings/base.py | 6 ++-- lemarche/utils/apis/api_brevo.py | 61 +++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index d3035183c..685bf8cc8 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -13,7 +13,6 @@ import os import environ -import sib_api_v3_sdk from django.contrib.messages import constants as messages @@ -391,8 +390,9 @@ # -- Sendinblue (BREVO) BREVO_API_KEY = env.str("BREVO_API_KEY", "set-it") -brevo_configuration = sib_api_v3_sdk.Configuration() -brevo_configuration.api_key["api-key"] = BREVO_API_KEY +BREVO_CL_SIGNUP_BUYER_ID = env.int("BREVO_CL_SIGNUP_BUYER_ID", 10) +BREVO_CL_LIVESTORM_BUYER_NOT_SIGNUP_ID = env.int("BREVO_CL_LIVESTORM_BUYER_NOT_SIGNUP_ID", 9) + INBOUND_PARSING_DOMAIN_EMAIL = env.str("INBOUND_PARSING_DOMAIN_EMAIL", "reply.staging.lemarche.inclusion.beta.gouv.fr") INBOUND_EMAIL_IS_ACTIVATED = env.bool("INBOUND_EMAIL_IS_ACTIVATED", True) diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 1b2b1b166..33d078572 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -1,3 +1,4 @@ +import json import logging import sib_api_v3_sdk @@ -5,13 +6,21 @@ from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException +from lemarche.users.models import User + logger = logging.getLogger(__name__) -def get_api_instance(): - api_instance = sib_api_v3_sdk.SMTPApi(sib_api_v3_sdk.ApiClient(settings.brevo_configuration)) - return api_instance +def get_brevo_config(): + brevo_configuration = sib_api_v3_sdk.Configuration() + brevo_configuration.api_key["api-key"] = settings.BREVO_API_KEY + return brevo_configuration + + +def get_api_client(): + brevo_config = get_brevo_config() + return sib_api_v3_sdk.ApiClient(brevo_config) ENV_NOT_ALLOWED = ("dev", "test") @@ -19,7 +28,8 @@ def get_api_instance(): @task() def send_html_email(to: list, sender: dict, html_content: str, headers: dict = {}): - api_instance = get_api_instance() + api_client = get_api_client() + api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client) send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( to=to, headers=headers, html_content=html_content, sender=sender ) # SendSmtpEmail | Values to send a transactional email @@ -41,7 +51,8 @@ def send_transactionnel_email(to: list, sender: dict, template_id: int, params_t params_template (dict): Paramaters of template, ec {"name": "John", "surname": "Doe"} headers (dict, optional): Custom headers of emails. Defaults to {}. """ - api_instance = get_api_instance() + api_client = get_api_client() + api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client) send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( to=to, template_id=template_id, sender=sender, params=params_template, headers=headers ) # SendSmtpEmail | Values to send a transactional email @@ -51,3 +62,43 @@ def send_transactionnel_email(to: list, sender: dict, template_id: int, params_t print(api_response) except ApiException as e: print("Exception when calling SMTPApi->send_transac_email: %s\n" % e) + + +def create_contact(user: User, list_id: int): + api_client = get_api_client() + api_instance = sib_api_v3_sdk.ContactsApi(api_client) + new_contact = sib_api_v3_sdk.CreateContact( + email=user.email, + list_ids=[list_id], + attributes={ + "NOM": user.first_name, + "PRENOM": user.last_name, + "DATE_INSCRIPTION": user.created_at, + "TYPE_ORGANISATION": user.buyer_kind_detail, + "NOM_ENTREPRISE": user.company_name, + }, + ext_id=str(user.id), + update_enabled=True, + ) + + try: + api_response = api_instance.create_contact(new_contact) + logger.info("Succes Brevo->ContactsApi->create_contact: %s\n" % api_response) + except ApiException as e: + logger.error("Exception when calling Brevo->ContactsApi->create_contact: %s\n" % e) + + +def remove_contact_from_list(user: User, list_id: int): + api_client = get_api_client() + api_instance = sib_api_v3_sdk.ContactsApi(api_client) + contact_emails = sib_api_v3_sdk.RemoveContactFromList(emails=[user.email]) + + try: + api_response = api_instance.remove_contact_from_list(list_id=list_id, contact_emails=contact_emails) + logger.info("Succes Brevo->ContactsApi->remove_contact_from_list: %s\n" % api_response) + except ApiException as e: + error_body = json.loads(e.body) + if error_body.get("message") == "Contact already removed from list and/or does not exist": + logger.info("calling Brevo->ContactsApi->remove_contact_from_list: contact doesn't exist in this list") + else: + logger.error("Exception when calling Brevo->ContactsApi->remove_contact_from_list: %s\n" % e) From 1033ac007658bf2c3fa53e0d4d98261107fd7baa Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Fri, 29 Dec 2023 12:43:42 +0100 Subject: [PATCH 2/4] add buyers to brevo list in task --- lemarche/www/auth/tasks.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lemarche/www/auth/tasks.py b/lemarche/www/auth/tasks.py index e8e7009ad..85bd97815 100644 --- a/lemarche/www/auth/tasks.py +++ b/lemarche/www/auth/tasks.py @@ -7,7 +7,7 @@ from lemarche.users import constants as user_constants from lemarche.users.models import User -from lemarche.utils.apis import api_hubspot, api_mailjet +from lemarche.utils.apis import api_brevo, api_hubspot, api_mailjet from lemarche.utils.emails import send_mail_async, whitelist_recipient_list from lemarche.utils.urls import get_domain_url @@ -95,7 +95,9 @@ def add_to_contact_list(user: User, type: str, source: str = user_constants.SOUR if type == "signup": contact_list_id = get_mailjet_cl_on_signup(user, source) if user.kind == user.KIND_BUYER: + # TODO: we still use it ? api_hubspot.add_user_to_crm(user) + api_brevo.create_contact(user=user, list_id=settings.BREVO_CL_SIGNUP_BUYER_ID) elif type == "buyer_search": contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_LIST_ID elif type == "buyer_search_traiteur": From fdb884b90b4f67810aa52813359c434303f9e17b Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 16 Jan 2024 12:54:48 +0100 Subject: [PATCH 3/4] Cleanup --- lemarche/utils/apis/api_brevo.py | 17 ++++++++--------- lemarche/utils/apis/api_mailjet.py | 5 +---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 33d078572..d9fd36aa2 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -11,19 +11,18 @@ logger = logging.getLogger(__name__) - -def get_brevo_config(): - brevo_configuration = sib_api_v3_sdk.Configuration() - brevo_configuration.api_key["api-key"] = settings.BREVO_API_KEY - return brevo_configuration +ENV_NOT_ALLOWED = ("dev", "test") -def get_api_client(): - brevo_config = get_brevo_config() - return sib_api_v3_sdk.ApiClient(brevo_config) +def get_config(): + config = sib_api_v3_sdk.Configuration() + config.api_key["api-key"] = settings.BREVO_API_KEY + return config -ENV_NOT_ALLOWED = ("dev", "test") +def get_api_client(): + config = get_config() + return sib_api_v3_sdk.ApiClient(config) @task() diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index 8976e384d..1376f7473 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) - +ENV_NOT_ALLOWED = ("dev", "test") BASE_URL = "https://api.mailjet.com/v3/REST/" SEND_URL = "https://api.mailjet.com/v3.1/send" @@ -34,9 +34,6 @@ def get_default_client(params={}): return client -ENV_NOT_ALLOWED = ("dev", "test") - - @task() def add_to_contact_list_async(email_address, properties, contact_list_id, client=None): """ From 61155426442ac9384afeb56b5bdc1e784b62445b Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 16 Jan 2024 12:59:29 +0100 Subject: [PATCH 4/4] Remove unused CL --- config/settings/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/config/settings/base.py b/config/settings/base.py index 685bf8cc8..809bb8973 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -391,7 +391,6 @@ # -- Sendinblue (BREVO) BREVO_API_KEY = env.str("BREVO_API_KEY", "set-it") BREVO_CL_SIGNUP_BUYER_ID = env.int("BREVO_CL_SIGNUP_BUYER_ID", 10) -BREVO_CL_LIVESTORM_BUYER_NOT_SIGNUP_ID = env.int("BREVO_CL_LIVESTORM_BUYER_NOT_SIGNUP_ID", 9) INBOUND_PARSING_DOMAIN_EMAIL = env.str("INBOUND_PARSING_DOMAIN_EMAIL", "reply.staging.lemarche.inclusion.beta.gouv.fr")