From 285b8c234901411591fe302131e25de6569b12f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chlo=C3=A9?= Date: Thu, 7 Nov 2024 11:44:10 +0100 Subject: [PATCH] =?UTF-8?q?Optimisation=20de=20requ=C3=AAte=20sur=20Tender?= =?UTF-8?q?=20et=20modification=20et=20ajout=20de=20tender.amount=5Fint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lemarche/api/tenders/tests.py | 6 +++--- lemarche/utils/apis/api_brevo.py | 21 +++++++++++++++------ lemarche/www/tenders/tests.py | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lemarche/api/tenders/tests.py b/lemarche/api/tenders/tests.py index 04c4d4810..c345cdfb5 100644 --- a/lemarche/api/tenders/tests.py +++ b/lemarche/api/tenders/tests.py @@ -1,9 +1,9 @@ +from unittest.mock import patch + from django.conf import settings from django.test import TestCase from django.urls import reverse -from unittest.mock import patch - from lemarche.perimeters.factories import PerimeterFactory from lemarche.sectors.factories import SectorFactory from lemarche.tenders import constants as tender_constants @@ -190,7 +190,7 @@ def test_create_contact_call_has_user_buyer_attributes(self, mock_create_contact self.assertEqual(kwargs["email"], user.email) self.assertIn(settings.BREVO_CL_SIGNUP_BUYER_ID, kwargs["list_ids"]) - self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount) + self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount_int) self.assertEqual(attributes["TYPE_BESOIN_ACHETEUR"], tender.kind) if sectors.exists(): diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index ff628a5cf..b85317879 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -4,6 +4,7 @@ import sib_api_v3_sdk from django.conf import settings +from django.core.exceptions import ObjectDoesNotExist from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException @@ -51,17 +52,25 @@ def create_contact(user, list_id: int, tender=None): # WHATSAPP, TYPE_ORGANISATION, LIEN_FICHE_COMMERCIALE, TAUX_DE_COMPLETION } - if tender: - sectors = tender.sectors.all() - attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount + try: + tender = user.tenders.get(id=tender.id) + first_sector = tender.sectors.first() + attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount_int attributes["TYPE_BESOIN_ACHETEUR"] = tender.kind - # Check if there is at least one sector whose tender source is TALLY - if tender.source == tender_constants.SOURCE_TALLY and sectors.exists(): - attributes["TYPE_VERTICALE_ACHETEUR"] = sectors.first().name + # Check if there is one sector whose tender source is TALLY + if tender.source == tender_constants.SOURCE_TALLY and first_sector: + attributes["TYPE_VERTICALE_ACHETEUR"] = first_sector.name else: attributes["TYPE_VERTICALE_ACHETEUR"] = None + except ObjectDoesNotExist: + print("L'objet Tender demandé n'existe pas pour cet utilisateur.") + except AttributeError as e: + print(f"Erreur d'attribut : {e}") + except Exception as e: + print(f"Une erreur inattendue est survenue : {e}") + new_contact = sib_api_v3_sdk.CreateContact( email=user.email, list_ids=[list_id], diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 92407975f..cd9c5b52d 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -293,7 +293,7 @@ def test_create_contact_call_has_user_buyer_attributes(self, mock_create_contact self.assertEqual(kwargs["email"], user.email) self.assertIn(settings.BREVO_CL_SIGNUP_BUYER_ID, kwargs["list_ids"]) - self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount) + self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount_int) self.assertEqual(attributes["TYPE_BESOIN_ACHETEUR"], tender.kind) self.assertIsNone( attributes["TYPE_VERTICALE_ACHETEUR"], "Expected TYPE_VERTICALE_ACHETEUR to be None for non-TALLY sources"