From a46eced4dbcdc2412e8e792a81b4de987558f2f9 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Tue, 24 Sep 2024 20:51:09 +0200 Subject: [PATCH 1/6] mailchimp experiments --- website/classes.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/website/classes.py b/website/classes.py index 159cf7de954..4b0e49a6703 100644 --- a/website/classes.py +++ b/website/classes.py @@ -49,6 +49,39 @@ def create_class(self, user): "name": body["name"], } + # from auth, where we also use this for mailinglist subscriptions + + from auth import MAILCHIMP_API_URL, MAILCHIMP_API_HEADERS + import requests + + # endpoint: /lists/{list_id}/members/{subscriber_hash}/tags + # first part is already handled in auth! + + + request_body = {"email_address": email, "status": "subscribed", "tags": [country, "teacher"]} + r = requests.post(MAILCHIMP_API_URL + "/members", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) + + # Example code from the mailchip docs: + # https://mailchimp.com/developer/marketing/api/list-member-tags/ + + + + # import mailchimp_marketing as MailchimpMarketing + # from mailchimp_marketing.api_client import ApiClientError + # + # try: + # client = MailchimpMarketing.Client() + # client.set_config({ + # "api_key": "YOUR_API_KEY", + # "server": "YOUR_SERVER_PREFIX" + # }) + # + # response = client.lists.update_list_member_tags("list_id", "subscriber_hash", + # {"tags": [{"name": "name", "status": "active"}]}) + # print(response) + # except ApiClientError as error: + # print("Error: {}".format(error.text)) + self.db.store_class(Class) response = {"id": Class["id"]} return make_response(response, 200) From 8b1d1579007976fbdc15c301304d4ddadd6e3881 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 18:52:25 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- website/classes.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/website/classes.py b/website/classes.py index 4b0e49a6703..8e45ca74dc9 100644 --- a/website/classes.py +++ b/website/classes.py @@ -56,7 +56,6 @@ def create_class(self, user): # endpoint: /lists/{list_id}/members/{subscriber_hash}/tags # first part is already handled in auth! - request_body = {"email_address": email, "status": "subscribed", "tags": [country, "teacher"]} r = requests.post(MAILCHIMP_API_URL + "/members", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) @@ -64,8 +63,6 @@ def create_class(self, user): # Example code from the mailchip docs: # https://mailchimp.com/developer/marketing/api/list-member-tags/ - - # import mailchimp_marketing as MailchimpMarketing # from mailchimp_marketing.api_client import ApiClientError # From 2b6473785d34c48b205931b05e3df9f3f82b9361 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Tue, 24 Sep 2024 21:30:29 +0200 Subject: [PATCH 3/6] update fields --- website/classes.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/website/classes.py b/website/classes.py index 4b0e49a6703..17c71fefa9e 100644 --- a/website/classes.py +++ b/website/classes.py @@ -1,3 +1,4 @@ +import json import uuid from flask import make_response, redirect, request, session @@ -53,34 +54,19 @@ def create_class(self, user): from auth import MAILCHIMP_API_URL, MAILCHIMP_API_HEADERS import requests + import hashlib + + subscriber = user["email"].encode('utf-8') + subscriber_hash = hashlib.md5(subscriber) # endpoint: /lists/{list_id}/members/{subscriber_hash}/tags # first part is already handled in auth! - - request_body = {"email_address": email, "status": "subscribed", "tags": [country, "teacher"]} + request_body = {"subscriber_hash": subscriber_hash, "tags": ["class_created"]} r = requests.post(MAILCHIMP_API_URL + "/members", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) - # Example code from the mailchip docs: - # https://mailchimp.com/developer/marketing/api/list-member-tags/ - - - - # import mailchimp_marketing as MailchimpMarketing - # from mailchimp_marketing.api_client import ApiClientError - # - # try: - # client = MailchimpMarketing.Client() - # client.set_config({ - # "api_key": "YOUR_API_KEY", - # "server": "YOUR_SERVER_PREFIX" - # }) - # - # response = client.lists.update_list_member_tags("list_id", "subscriber_hash", - # {"tags": [{"name": "name", "status": "active"}]}) - # print(response) - # except ApiClientError as error: - # print("Error: {}".format(error.text)) + + self.db.store_class(Class) response = {"id": Class["id"]} From dea352b4dca9f786e4462494b5cd30fb2905b744 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Tue, 24 Sep 2024 22:06:44 +0200 Subject: [PATCH 4/6] great success --- website/classes.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/website/classes.py b/website/classes.py index 334f2848395..e8585024891 100644 --- a/website/classes.py +++ b/website/classes.py @@ -52,18 +52,36 @@ def create_class(self, user): # from auth, where we also use this for mailinglist subscriptions - from auth import MAILCHIMP_API_URL, MAILCHIMP_API_HEADERS + import os + + MAILCHIMP_API_URL = None + MAILCHIMP_API_HEADERS = {} + if os.getenv("MAILCHIMP_API_KEY") and os.getenv("MAILCHIMP_AUDIENCE_ID"): + # The domain in the path is the server name, which is contained in the Mailchimp API key + MAILCHIMP_API_URL = ( + "https://" + + os.getenv("MAILCHIMP_API_KEY").split("-")[1] + + ".api.mailchimp.com/3.0/lists/" + + os.getenv("MAILCHIMP_AUDIENCE_ID") + ) + MAILCHIMP_API_HEADERS = { + "Content-Type": "application/json", + "Authorization": "apikey " + os.getenv("MAILCHIMP_API_KEY"), + } + import requests import hashlib subscriber = user["email"].encode('utf-8') - subscriber_hash = hashlib.md5(subscriber) + subscriber_hash = hashlib.md5(subscriber).hexdigest() - # endpoint: /lists/{list_id}/members/{subscriber_hash}/tags - # first part is already handled in auth! + # mailchimp docs: https://mailchimp.com/developer/marketing/api/list-member-tags/add-or-remove-member-tags/ - request_body = {"subscriber_hash": subscriber_hash, "tags": ["class_created"]} - r = requests.post(MAILCHIMP_API_URL + "/members", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) + request_body = {"tags": [{"name": "class_created", "status": "active"}]} + try: + r = requests.post(MAILCHIMP_API_URL + f"/members/{subscriber_hash}/tags", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) + except Exception as E: + pass self.db.store_class(Class) response = {"id": Class["id"]} From 41ec2d03fe273664f7ad103f4aeebff6edfe48d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:08:36 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- website/classes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/classes.py b/website/classes.py index e8585024891..cdbd31c71b7 100644 --- a/website/classes.py +++ b/website/classes.py @@ -79,7 +79,8 @@ def create_class(self, user): request_body = {"tags": [{"name": "class_created", "status": "active"}]} try: - r = requests.post(MAILCHIMP_API_URL + f"/members/{subscriber_hash}/tags", headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) + r = requests.post(MAILCHIMP_API_URL + f"/members/{subscriber_hash}/tags", + headers=MAILCHIMP_API_HEADERS, data=json.dumps(request_body)) except Exception as E: pass From 17332ddcc67d83cf8c5d305494ad4e4848041089 Mon Sep 17 00:00:00 2001 From: Felienne Hermans Date: Tue, 24 Sep 2024 22:19:25 +0200 Subject: [PATCH 6/6] import from auth --- website/classes.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/website/classes.py b/website/classes.py index e8585024891..3e08b5e23f7 100644 --- a/website/classes.py +++ b/website/classes.py @@ -54,20 +54,7 @@ def create_class(self, user): import os - MAILCHIMP_API_URL = None - MAILCHIMP_API_HEADERS = {} - if os.getenv("MAILCHIMP_API_KEY") and os.getenv("MAILCHIMP_AUDIENCE_ID"): - # The domain in the path is the server name, which is contained in the Mailchimp API key - MAILCHIMP_API_URL = ( - "https://" - + os.getenv("MAILCHIMP_API_KEY").split("-")[1] - + ".api.mailchimp.com/3.0/lists/" - + os.getenv("MAILCHIMP_AUDIENCE_ID") - ) - MAILCHIMP_API_HEADERS = { - "Content-Type": "application/json", - "Authorization": "apikey " + os.getenv("MAILCHIMP_API_KEY"), - } + from .auth import MAILCHIMP_API_URL, MAILCHIMP_API_HEADERS import requests import hashlib