From 50cc60e064da30fed0aba93319d650e16e8b6510 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 27 Mar 2024 09:13:17 +0100 Subject: [PATCH] =?UTF-8?q?refactor(Emails):=20Bouger=20la=20m=C3=A9thode?= =?UTF-8?q?=20d'ajout=20=C3=A0=20une=20liste=20de=20contact=20dans=20les?= =?UTF-8?q?=20utils=20(#1102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lemarche/utils/apis/api_brevo.py | 8 ++-- lemarche/utils/apis/api_hubspot.py | 7 +--- lemarche/utils/apis/api_mailjet.py | 25 ++++++++++++- lemarche/utils/constants.py | 6 +++ lemarche/utils/emails.py | 40 +++++++++++++++++++- lemarche/www/auth/tasks.py | 60 +----------------------------- lemarche/www/auth/views.py | 3 +- lemarche/www/pages/tasks.py | 3 +- lemarche/www/siaes/views.py | 2 +- lemarche/www/tenders/utils.py | 3 +- 10 files changed, 81 insertions(+), 76 deletions(-) diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 8cf3d4641..c92623341 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -6,8 +6,6 @@ from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException -from lemarche.siaes.models import Siae -from lemarche.users.models import User from lemarche.utils.urls import get_object_admin_url, get_object_share_url @@ -27,7 +25,7 @@ def get_api_client(): return sib_api_v3_sdk.ApiClient(config) -def create_contact(user: User, list_id: int): +def create_contact(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( @@ -51,7 +49,7 @@ def create_contact(user: User, list_id: int): logger.error(f"Exception when calling Brevo->ContactsApi->create_contact: {e}") -def remove_contact_from_list(user: User, list_id: int): +def remove_contact_from_list(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]) @@ -67,7 +65,7 @@ def remove_contact_from_list(user: User, list_id: int): logger.error(f"Exception when calling Brevo->ContactsApi->remove_contact_from_list: {e}") -def create_or_update_company(siae: Siae): +def create_or_update_company(siae): """ Brevo docs: - Python library: https://github.com/sendinblue/APIv3-python-library/blob/master/docs/CompaniesApi.md diff --git a/lemarche/utils/apis/api_hubspot.py b/lemarche/utils/apis/api_hubspot.py index a02485d6a..04211ebea 100644 --- a/lemarche/utils/apis/api_hubspot.py +++ b/lemarche/utils/apis/api_hubspot.py @@ -6,9 +6,6 @@ from hubspot import Client from hubspot.crm.contacts import ApiException, SimplePublicObject, SimplePublicObjectInput -from lemarche.tenders.models import Tender -from lemarche.users.models import User - # from huey.contrib.djhuey import task @@ -85,7 +82,7 @@ def add_to_contacts( logger.info("Hubspot: not add contact to the crm (STAGING or TEST environment detected)") -def add_user_to_crm(user: User): +def add_user_to_crm(user): result = add_to_contacts( email=user.email, company=user.company_name, @@ -100,7 +97,7 @@ def add_user_to_crm(user: User): # @task -def create_deal_from_tender(tender: Tender): +def create_deal_from_tender(tender): tender_author_hubspot_contact_id = tender.author.hubspot_contact_id if not tender_author_hubspot_contact_id: user_added_in_crm = add_user_to_crm(tender.author) diff --git a/lemarche/utils/apis/api_mailjet.py b/lemarche/utils/apis/api_mailjet.py index 1376f7473..3208ed45b 100644 --- a/lemarche/utils/apis/api_mailjet.py +++ b/lemarche/utils/apis/api_mailjet.py @@ -4,7 +4,8 @@ from django.conf import settings from huey.contrib.djhuey import task -from lemarche.utils.emails import EMAIL_SUBJECT_PREFIX +from lemarche.users import constants as user_constants +from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX logger = logging.getLogger(__name__) @@ -34,6 +35,28 @@ def get_default_client(params={}): return client +def get_mailjet_cl_on_signup(user, source: str = user_constants.SOURCE_SIGNUP_FORM): + if user.kind == user_constants.KIND_SIAE: + return settings.MAILJET_NL_CL_SIAE_ID + elif user.kind == user_constants.KIND_BUYER: + if source == user_constants.SOURCE_SIGNUP_FORM: + return settings.MAILJET_NL_CL_BUYER_ID + elif source == user_constants.SOURCE_TALLY_FORM: + return settings.MAILJET_NL_CL_BUYER_TALLY_ID + elif source == user_constants.SOURCE_TENDER_FORM: + return settings.MAILJET_NL_CL_BUYER_TENDER_ID + elif user.kind == user_constants.KIND_PARTNER: + if user.partner_kind == user_constants.PARTNER_KIND_FACILITATOR: + return settings.MAILJET_NL_CL_PARTNER_FACILITATORS_ID + elif user.partner_kind in ( + user_constants.PARTNER_KIND_NETWORD_IAE, + user_constants.PARTNER_KIND_NETWORK_HANDICAP, + ): + return settings.MAILJET_NL_CL_PARTNER_NETWORKS_IAE_HANDICAP_ID + elif user.partner_kind == user_constants.PARTNER_KIND_DREETS: + return settings.MAILJET_NL_CL_PARTNER_DREETS_ID + + @task() def add_to_contact_list_async(email_address, properties, contact_list_id, client=None): """ diff --git a/lemarche/utils/constants.py b/lemarche/utils/constants.py index 4f87050c8..e249f6405 100644 --- a/lemarche/utils/constants.py +++ b/lemarche/utils/constants.py @@ -1,3 +1,6 @@ +from django.conf import settings + + EMPTY_CHOICE = (("", ""),) ADMIN_FIELD_HELP_TEXT = "Champ renseigné par un ADMIN" @@ -261,3 +264,6 @@ def format_district(post_code, department): # Could use ordinal from humanize but it would be overkill number = int(post_code) - (int(department) * 1000) return "1er" if number == 1 else f"{number}e" + + +EMAIL_SUBJECT_PREFIX = f"[{settings.BITOUBI_ENV.upper()}] " if settings.BITOUBI_ENV != "prod" else "" diff --git a/lemarche/utils/emails.py b/lemarche/utils/emails.py index 887c061a3..b7dbc8ffd 100644 --- a/lemarche/utils/emails.py +++ b/lemarche/utils/emails.py @@ -4,8 +4,9 @@ from django.core.mail import send_mail from huey.contrib.djhuey import task - -EMAIL_SUBJECT_PREFIX = f"[{settings.BITOUBI_ENV.upper()}] " if settings.BITOUBI_ENV != "prod" else "" +from lemarche.users import constants as user_constants +from lemarche.utils.apis import api_brevo, api_hubspot, api_mailjet +from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX def anonymize_email(email): @@ -25,6 +26,41 @@ def whitelist_recipient_list(recipient_list): return [email for email in recipient_list if (email and email.endswith("beta.gouv.fr"))] +def add_to_contact_list(user, type: str, source: str = user_constants.SOURCE_SIGNUP_FORM): + """Add user to contactlist + + Args: + user (User): the user how will be added in the contact list + type (String): "signup", OR "buyer_download" or "buyer_search" else raise ValueError + """ + if type == "signup": + contact_list_id = api_mailjet.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": + contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_TRAITEUR_LIST_ID + elif type == "buyer_search_nettoyage": + contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_NETTOYAGE_LIST_ID + elif type == "buyer_download": + contact_list_id = settings.MAILJET_NL_CL_BUYER_DOWNLOAD_SIAE_LIST_ID + else: + raise ValueError("type must be defined") + if contact_list_id: + properties = { + "nom": user.last_name.capitalize(), + "prénom": user.first_name.capitalize(), + "pays": "france", + "nomsiae": user.company_name.capitalize() if user.company_name else "", + "poste": user.position.capitalize() if user.position else "", + } + + api_mailjet.add_to_contact_list_async(user.email, properties, contact_list_id) + + @task() def send_mail_async( email_subject, diff --git a/lemarche/www/auth/tasks.py b/lemarche/www/auth/tasks.py index 85bd97815..a0914f635 100644 --- a/lemarche/www/auth/tasks.py +++ b/lemarche/www/auth/tasks.py @@ -5,9 +5,8 @@ from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode -from lemarche.users import constants as user_constants from lemarche.users.models import User -from lemarche.utils.apis import api_brevo, api_hubspot, api_mailjet +from lemarche.utils.apis import api_mailjet from lemarche.utils.emails import send_mail_async, whitelist_recipient_list from lemarche.utils.urls import get_domain_url @@ -61,60 +60,3 @@ def send_new_user_password_reset_link(user: User): recipient_name=recipient_name, variables=variables, ) - - -def get_mailjet_cl_on_signup(user: User, source: str = user_constants.SOURCE_SIGNUP_FORM): - if user.kind == user.KIND_SIAE: - return settings.MAILJET_NL_CL_SIAE_ID - elif user.kind == user.KIND_BUYER: - if source == user_constants.SOURCE_SIGNUP_FORM: - return settings.MAILJET_NL_CL_BUYER_ID - elif source == user_constants.SOURCE_TALLY_FORM: - return settings.MAILJET_NL_CL_BUYER_TALLY_ID - elif source == user_constants.SOURCE_TENDER_FORM: - return settings.MAILJET_NL_CL_BUYER_TENDER_ID - elif user.kind == user.KIND_PARTNER: - if user.partner_kind == user_constants.PARTNER_KIND_FACILITATOR: - return settings.MAILJET_NL_CL_PARTNER_FACILITATORS_ID - elif user.partner_kind in ( - user_constants.PARTNER_KIND_NETWORD_IAE, - user_constants.PARTNER_KIND_NETWORK_HANDICAP, - ): - return settings.MAILJET_NL_CL_PARTNER_NETWORKS_IAE_HANDICAP_ID - elif user.partner_kind == user_constants.PARTNER_KIND_DREETS: - return settings.MAILJET_NL_CL_PARTNER_DREETS_ID - - -def add_to_contact_list(user: User, type: str, source: str = user_constants.SOURCE_SIGNUP_FORM): - """Add user to contactlist - - Args: - user (User): the user how will be added in the contact list - type (String): "signup", OR "buyer_download" or "buyer_search" else raise ValueError - """ - 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": - contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_TRAITEUR_LIST_ID - elif type == "buyer_search_nettoyage": - contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_NETTOYAGE_LIST_ID - elif type == "buyer_download": - contact_list_id = settings.MAILJET_NL_CL_BUYER_DOWNLOAD_SIAE_LIST_ID - else: - raise ValueError("type must be defined") - if contact_list_id: - properties = { - "nom": user.last_name.capitalize(), - "prénom": user.first_name.capitalize(), - "pays": "france", - "nomsiae": user.company_name.capitalize() if user.company_name else "", - "poste": user.position.capitalize() if user.position else "", - } - - api_mailjet.add_to_contact_list_async(user.email, properties, contact_list_id) diff --git a/lemarche/www/auth/views.py b/lemarche/www/auth/views.py index b9273d458..167c1a828 100644 --- a/lemarche/www/auth/views.py +++ b/lemarche/www/auth/views.py @@ -7,9 +7,10 @@ from django.views.generic import CreateView from lemarche.users.models import User +from lemarche.utils.emails import add_to_contact_list from lemarche.utils.urls import get_safe_url from lemarche.www.auth.forms import LoginForm, PasswordResetForm, SignupForm -from lemarche.www.auth.tasks import add_to_contact_list, send_signup_notification_email +from lemarche.www.auth.tasks import send_signup_notification_email class LoginView(auth_views.LoginView): diff --git a/lemarche/www/pages/tasks.py b/lemarche/www/pages/tasks.py index b35e0800a..2cedf0ab4 100644 --- a/lemarche/www/pages/tasks.py +++ b/lemarche/www/pages/tasks.py @@ -2,7 +2,8 @@ from django.core.mail import EmailMessage, send_mail from django.template.loader import render_to_string -from lemarche.utils.emails import EMAIL_SUBJECT_PREFIX, whitelist_recipient_list +from lemarche.utils.constants import EMAIL_SUBJECT_PREFIX +from lemarche.utils.emails import whitelist_recipient_list # TODO: make async (celery) diff --git a/lemarche/www/siaes/views.py b/lemarche/www/siaes/views.py index 75a9e2f39..7c5e42cb8 100644 --- a/lemarche/www/siaes/views.py +++ b/lemarche/www/siaes/views.py @@ -20,10 +20,10 @@ from lemarche.favorites.models import FavoriteList from lemarche.siaes.models import Siae from lemarche.utils.apis import api_elasticsearch +from lemarche.utils.emails import add_to_contact_list from lemarche.utils.export import export_siae_to_csv, export_siae_to_excel from lemarche.utils.s3 import API_CONNECTION_DICT from lemarche.utils.urls import get_domain_url, get_encoded_url_from_params -from lemarche.www.auth.tasks import add_to_contact_list from lemarche.www.conversations.forms import ContactForm from lemarche.www.siaes.forms import ( SiaeDownloadForm, diff --git a/lemarche/www/tenders/utils.py b/lemarche/www/tenders/utils.py index 22652eb18..fa77a7e3c 100644 --- a/lemarche/www/tenders/utils.py +++ b/lemarche/www/tenders/utils.py @@ -4,7 +4,8 @@ from lemarche.tenders.models import Tender, TenderQuestion from lemarche.users import constants as user_constants from lemarche.users.models import User -from lemarche.www.auth.tasks import add_to_contact_list, send_new_user_password_reset_link +from lemarche.utils.emails import add_to_contact_list +from lemarche.www.auth.tasks import send_new_user_password_reset_link def create_questions_list(tender, questions_list):