From 1225a0cad121df3eb6c26b037e121da5c4c8acb0 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 27 Feb 2024 18:16:39 +0100 Subject: [PATCH] =?UTF-8?q?feat(Brevo):=20m=C3=A9thode=20pour=20cr=C3=A9er?= =?UTF-8?q?=20(et=20maj)=20des=20entreprises=20(SIAE)=20(#1099)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/settings/base.py | 1 + lemarche/utils/apis/api_brevo.py | 58 +++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index be10a60ab..5c1a4c532 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -864,6 +864,7 @@ "from lemarche.utils import constants", "from lemarche.siaes import constants as siae_constants", "from lemarche.tenders import constants as tender_constants", + "from lemarche.utils.apis import api_brevo, api_mailjet", ] FORM_RENDERER = "django.forms.renderers.TemplatesSetting" diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index db5b04c4e..a22c5fedc 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -6,7 +6,9 @@ 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 logger = logging.getLogger(__name__) @@ -44,9 +46,9 @@ def create_contact(user: User, list_id: int): try: api_response = api_instance.create_contact(new_contact) - logger.info("Succes Brevo->ContactsApi->create_contact: %s\n" % api_response) + logger.info(f"Success Brevo->ContactsApi->create_contact: {api_response}") except ApiException as e: - logger.error("Exception when calling Brevo->ContactsApi->create_contact: %s\n" % e) + logger.error(f"Exception when calling Brevo->ContactsApi->create_contact: {e}") def remove_contact_from_list(user: User, list_id: int): @@ -56,13 +58,59 @@ def remove_contact_from_list(user: User, list_id: int): 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) + logger.info(f"Success Brevo->ContactsApi->remove_contact_from_list: {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) + logger.error(f"Exception when calling Brevo->ContactsApi->remove_contact_from_list: {e}") + + +def create_or_update_company(siae: 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_client = get_api_client() + api_instance = sib_api_v3_sdk.CompaniesApi(api_client) + + siae_brevo_company_body = sib_api_v3_sdk.Body( + name=siae.name, + attributes={ + "app_id": siae.id, + "siae": True, + "description": siae.description, + "kind": siae.kind, + "address_street": siae.address, + "address_post_code": siae.post_code, + "address_city": siae.city, + "contact_email": siae.contact_email, + "contact_phone": siae.contact_phone, + "domain": siae.website, + "logo_url": siae.logo_url, + "geo_range": siae.geo_range, + "app_url": get_object_share_url(siae), + "app_admin_url": get_object_admin_url(siae), + }, + ) + + if siae.brevo_company_id: # update + try: + api_response = api_instance.companies_id_patch(siae.brevo_company_id, siae_brevo_company_body) + # logger.info(f"Success Brevo->CompaniesApi->create_or_update_company (update): {api_response}") + # api_response: {'attributes': None, 'id': None, 'linked_contacts_ids': None, 'linked_deals_ids': None} + except ApiException as e: + logger.error(f"Exception when calling Brevo->CompaniesApi->create_or_update_company (update): {e}") + else: # create + try: + api_response = api_instance.companies_post(siae_brevo_company_body) + logger.info(f"Success Brevo->CompaniesApi->create_or_update_company (create): {api_response}") + # api_response: {'id': ''} + siae.set_brevo_id(api_response.id) + except ApiException as e: + logger.error(f"Exception when calling Brevo->CompaniesApi->create_or_update_company (create): {e}") @task() @@ -91,6 +139,6 @@ def send_transactional_email_with_template( logger.info("Brevo: send transactional email with template") return response except ApiException as e: - print("Exception when calling SMTPApi->send_transac_email: %s\n" % e) + print(f"Exception when calling SMTPApi->send_transac_email: {e}") else: logger.info("Brevo: email not sent (DEV or TEST environment detected)")