Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional cleanup
Browse files Browse the repository at this point in the history
raphodn committed May 24, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 408679c commit 25c3baf
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ def create_contact(user, list_id: int):
"""
Brevo docs
- Python library: https://github.com/sendinblue/APIv3-python-library/blob/master/docs/CreateContact.md
- API: https://developers.brevo.com/reference/createcontact
"""
api_client = get_api_client()
api_instance = sib_api_v3_sdk.ContactsApi(api_client)
@@ -44,7 +45,7 @@ def create_contact(user, list_id: int):
"DATE_INSCRIPTION": user.created_at,
"TYPE_ORGANISATION": user.buyer_kind_detail,
"NOM_ENTREPRISE": user.company_name,
"SMS": str(user.phone),
"SMS": user.phone_display,
# WHATSAPP, TYPE_ORGANISATION, LIEN_FICHE_COMMERCIALE, TAUX_DE_COMPLETION
},
ext_id=str(user.id),
@@ -81,7 +82,7 @@ def create_or_update_company(siae):
"""
Brevo docs
- Python library: https://github.com/sendinblue/APIv3-python-library/blob/master/docs/CompaniesApi.md
- API: https://developers.brevo.com/reference/get_companies
- API: https://developers.brevo.com/reference/post_companies
"""
api_client = get_api_client()
api_instance = sib_api_v3_sdk.CompaniesApi(api_client)
@@ -128,47 +129,17 @@ def create_or_update_company(siae):
logger.error(f"Exception when calling Brevo->CompaniesApi->create_or_update_company (create): {e}")


@task()
def send_transactional_email_with_template(
template_id: int,
recipient_email: str,
recipient_name: str,
variables: dict,
subject=None,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client)
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
except ApiException as e:
print(f"Exception when calling SMTPApi->send_transac_email: {e}")
else:
logger.info("Brevo: email not sent (DEV or TEST environment detected)")


def create_deal(tender, owner_email: str):
"""
Creates a new deal in Brevo CRM from a tender and logs the result.
This function configures a deal using the tender's details and the owner's email, and posts it to the Brevo CRM.
If successful, it updates the tender with the new deal ID. If it encounters issues, it logs an error.
Brevo docs
- https://github.com/sendinblue/APIv3-python-library/blob/master/docs/DealsApi.md
- https://developers.brevo.com/reference/post_crm-deals
Args:
tender (Tender): Object with tender details like title, description, amount, and deadlines.
owner_email (str): The email address of the deal's owner.
@@ -181,13 +152,14 @@ def create_deal(tender, owner_email: str):
body_deal = sib_api_v3_sdk.Body3(
name=tender.title,
attributes={
# default attributes
# pipeline, deal_stage, closed_won_reason, closed_lost_reason, total_revenue, lost_reason
"deal_description": tender.description,
# "deal_pipeline": "...",
# "deal_stage": "...",
"deal_owner": owner_email,
"close_date": tender.deadline_date.strftime("%Y-%m-%d"),
# custom attributes
"amount": AMOUNT_RANGE_CHOICE_EXACT.get(tender.amount, 0),
"tender_admin_url": tender.get_admin_url(),
"close_date": tender.deadline_date.strftime("%Y-%m-%d"),
},
)

@@ -331,3 +303,37 @@ def get_all_users_from_list(
logger.error(f"Unexpected error: {e}")
break
return result


@task()
def send_transactional_email_with_template(
template_id: int,
recipient_email: str,
recipient_name: str,
variables: dict,
subject=None,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client)
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
except ApiException as e:
print(f"Exception when calling SMTPApi->send_transac_email: {e}")
else:
logger.info("Brevo: email not sent (DEV or TEST environment detected)")

0 comments on commit 25c3baf

Please sign in to comment.