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

Emails : ajout des contacts à la liste des acheteurs sur Brevo #1028

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import os

import environ
import sib_api_v3_sdk
from django.contrib.messages import constants as messages


Expand Down Expand Up @@ -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)
Expand Down
62 changes: 56 additions & 6 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import json
import logging

import sib_api_v3_sdk
from django.conf import settings
from huey.contrib.djhuey import task
from sib_api_v3_sdk.rest import ApiException

from lemarche.users.models import User


logger = logging.getLogger(__name__)

ENV_NOT_ALLOWED = ("dev", "test")


def get_api_instance():
api_instance = sib_api_v3_sdk.SMTPApi(sib_api_v3_sdk.ApiClient(settings.brevo_configuration))
return api_instance
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()
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
Expand All @@ -41,7 +50,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
Expand All @@ -51,3 +61,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)
5 changes: 1 addition & 4 deletions lemarche/utils/apis/api_mailjet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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):
"""
Expand Down
4 changes: 3 additions & 1 deletion lemarche/www/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand Down