Skip to content

Commit

Permalink
GDPR fix
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Oct 17, 2022
1 parent 63079e6 commit 35d62b3
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions app/utils/mailchimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,33 @@ def email_to_hash(email):
return hashlib.md5(email.encode("utf-8").lower()).hexdigest()


def mailchimp_contact_for_user(user: User):
def apply_gdpr_consent(member):
if not MAILCHIMP_IS_ACTIVE:
return False
marketing_permissions = []
for marketing_permission in member.get("marketing_permissions", []):
marketing_permissions.append(
{
"marketing_permission_id": marketing_permission[
"marketing_permission_id"
],
"enabled": True,
}
)
updated = mailchimp.lists.set_list_member(
member["list_id"],
member["id"],
{"marketing_permissions": marketing_permissions},
)
return updated


def mailchimp_contact_for_user(user: User, list_id=settings.MAILCHIMP_LIST_ID):
if not MAILCHIMP_IS_ACTIVE:
return False
try:
updated = mailchimp.lists.set_list_member(
settings.MAILCHIMP_LIST_ID,
member = mailchimp.lists.set_list_member(
list_id,
email_to_hash(user.primary_email),
{
"email_address": user.primary_email,
Expand All @@ -40,17 +61,19 @@ def mailchimp_contact_for_user(user: User):
else "unsubscribed",
},
)
return updated
member = apply_gdpr_consent(member)
return member
except MailchimpApiClientError:
created = mailchimp.lists.add_list_member(
settings.MAILCHIMP_LIST_ID,
member = mailchimp.lists.add_list_member(
list_id,
{
"email_address": user.primary_email,
"merge_fields": {"FNAME": user.first_name, "LNAME": user.last_name},
"status": "subscribed" if user.gdpr_email_consent else "unsubscribed",
},
)
return created
member = apply_gdpr_consent(member)
return member


def tag_user_in_mailchimp(user: User, tags_to_enable=list(), tags_to_disable=list()):
Expand All @@ -71,4 +94,4 @@ def tag_user_in_mailchimp(user: User, tags_to_enable=list(), tags_to_disable=lis
)
print(f"client.lists.update_list_member_tags() response: {response}")
except MailchimpApiClientError as error:
print(f"An exception occurred: {error.text}")
print(f"A Mailchimp API exception occurred: {error.text}")

0 comments on commit 35d62b3

Please sign in to comment.