-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Brevo update contacts : Commencer par la création des nouveaux contacts et ensuite MAJ des contacts existants #4587
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b31beb2
Remplacer le niveau de logging d Exception à Warning quand un batch n…
c67a138
Rfeacto brevo tasks
ba57656
Change order to first create missing Brevo contacts
6439c69
Merge branch 'staging' of github.com:betagouv/ma-cantine into 4493-pr…
bd6a58c
Adapt tests to refacto
929a5eb
Fix des test pour prendre en compte la séparation creation et update
93824bd
Refacto
b58ff55
Refacto : renommage fonction
6c313b5
Update macantine/tasks.py
qloridant 0d3e0d0
Update macantine/brevo.py
qloridant 7d62fb9
Enlever test obsolète
17d837f
Merge branch '4493-problme-avec-la-configuration-brevo' of github.com…
157c56f
Fix minor errors in refacto
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import logging | ||
import time | ||
|
||
import requests | ||
import sib_api_v3_sdk | ||
from django.conf import settings | ||
|
||
from data.models import Canteen | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
configuration = sib_api_v3_sdk.Configuration() | ||
configuration.api_key["api-key"] = settings.ANYMAIL.get("SENDINBLUE_API_KEY") | ||
api_client = sib_api_v3_sdk.ApiClient(configuration) | ||
email_api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client) | ||
contacts_api_instance = sib_api_v3_sdk.ContactsApi(api_client) | ||
|
||
|
||
def send_sib_template(template_id, parameters, to_email, to_name): | ||
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( | ||
to=[{"email": to_email, "name": to_name}], | ||
params=parameters, | ||
sender={"email": settings.CONTACT_EMAIL, "name": "ma cantine"}, | ||
reply_to={"email": settings.CONTACT_EMAIL, "name": "ma cantine"}, | ||
template_id=template_id, | ||
) | ||
email_api_instance.send_transac_email(send_smtp_email) | ||
|
||
|
||
def user_to_brevo_payload(user, bulk=True): | ||
has_canteens = user.canteens.exists() | ||
date_joined = user.date_joined | ||
|
||
def missing_diag_for_year(year, user): | ||
return user.canteens.exists() and any(not x.has_diagnostic_for_year(year) for x in user.canteens.all()) | ||
|
||
def missing_td_for_year(year, user): | ||
return user.canteens.exists() and any(not x.has_teledeclaration_for_year(year) for x in user.canteens.all()) | ||
|
||
missing_publications = ( | ||
has_canteens and user.canteens.filter(publication_status=Canteen.PublicationStatus.DRAFT).exists() | ||
) | ||
|
||
dict_attributes = { | ||
"MA_CANTINE_DATE_INSCRIPTION": date_joined.strftime("%Y-%m-%d"), | ||
"MA_CANTINE_COMPTE_DEV": user.is_dev, | ||
"MA_CANTINE_COMPTE_ELU_E": user.is_elected_official, | ||
"MA_CANTINE_GERE_UN_ETABLISSEMENT": has_canteens, | ||
"MA_CANTINE_MANQUE_BILAN_DONNEES_2023": missing_diag_for_year(2023, user), | ||
"MA_CANTINE_MANQUE_BILAN_DONNEES_2022": missing_diag_for_year(2022, user), | ||
"MA_CANTINE_MANQUE_BILAN_DONNEES_2021": missing_diag_for_year(2021, user), | ||
"MA_CANTINE_MANQUE_TD_DONNEES_2023": missing_td_for_year(2023, user), | ||
"MA_CANTINE_MANQUE_TD_DONNEES_2022": missing_td_for_year(2022, user), | ||
"MA_CANTINE_MANQUE_TD_DONNEES_2021": missing_td_for_year(2021, user), | ||
"MA_CANTINE_MANQUE_PUBLICATION": missing_publications, | ||
} | ||
if bulk: | ||
return sib_api_v3_sdk.UpdateBatchContactsContacts(email=user.email, attributes=dict_attributes) | ||
return sib_api_v3_sdk.CreateContact(email=user.email, attributes=dict_attributes, update_enabled=True) | ||
|
||
|
||
def update_existing_brevo_contacts(users_tu_update, today): | ||
qloridant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for chunk in users_tu_update: | ||
contacts = [user_to_brevo_payload(user) for user in chunk] | ||
update_object = sib_api_v3_sdk.UpdateBatchContacts(contacts) | ||
try: | ||
contacts_api_instance.update_batch_contacts(update_object) | ||
for user in chunk: | ||
user.last_brevo_update = today | ||
user.save() | ||
except requests.exceptions.HTTPError as e: | ||
logger.warning(f"Bulk updating Brevo users: One or more of the users don't exist. {e}") | ||
except Exception as e: | ||
logger.exception(f"Bulk updating Brevo users: Error updating Brevo users {e}", stack_info=True) | ||
time.sleep(0.1) # API rate limit is 10 req per second | ||
|
||
|
||
def create_new_brevo_contacts(users_to_create, today): | ||
for user in users_to_create: | ||
try: | ||
contact = user_to_brevo_payload(user, bulk=False) | ||
contacts_api_instance.create_contact(contact) | ||
user.last_brevo_update = today | ||
user.save() | ||
except Exception as e: | ||
logger.exception(f"Error creating/updating an individual Brevo user {e}", stack_info=True) | ||
time.sleep(0.1) # API rate limit is 10 req per second |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merci pour le refacto !