Skip to content

Commit

Permalink
Move add_to_contact_list to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Mar 13, 2024
1 parent 14c2e05 commit 85949d0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 62 deletions.
24 changes: 24 additions & 0 deletions lemarche/utils/apis/api_mailjet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.conf import settings
from huey.contrib.djhuey import task

from lemarche.users import constants as user_constants
from lemarche.users.models import User
from lemarche.utils.emails import EMAIL_SUBJECT_PREFIX


Expand Down Expand Up @@ -34,6 +36,28 @@ def get_default_client(params={}):
return client


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


@task()
def add_to_contact_list_async(email_address, properties, contact_list_id, client=None):
"""
Expand Down
39 changes: 39 additions & 0 deletions lemarche/utils/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from django.core.mail import send_mail
from huey.contrib.djhuey import task

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


EMAIL_SUBJECT_PREFIX = f"[{settings.BITOUBI_ENV.upper()}] " if settings.BITOUBI_ENV != "prod" else ""

Expand All @@ -25,6 +29,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: 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,
Expand Down
60 changes: 1 addition & 59 deletions lemarche/www/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion lemarche/www/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lemarche/www/tenders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 85949d0

Please sign in to comment.