X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/e81500796eb5163e07ac6b02109666747fb0c206..e852900e42eb0bde98daf7070d84816e44c3308e:/src/contact/mailing.py?ds=sidebyside diff --git a/src/contact/mailing.py b/src/contact/mailing.py index 2034766cd..4dee9c5a7 100644 --- a/src/contact/mailing.py +++ b/src/contact/mailing.py @@ -1,5 +1,6 @@ -# -*- coding: utf-8 -*- - +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from hashlib import md5 from django.conf import settings @@ -8,7 +9,7 @@ from mailchimp3.mailchimpclient import MailChimpError def subscriber_hash(email): - return md5(email).hexdigest() + return md5(email.encode('utf-8')).hexdigest() def remove_from_groups(email, client): @@ -48,3 +49,26 @@ def subscribe(email, mailing_lists=None): 'interests': interests, } ) + + +def unsubscribe(email, mailing_lists=None): + client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY, timeout=10.0) + try: + member = client.lists.members.get(settings.MAILCHIMP_LIST_ID, subscriber_hash(email)) + except MailChimpError: + return + else: + if member['status'] != 'subscribed': + return + mailing_lists = mailing_lists or settings.MAILCHIMP_GROUP_IDS + interests = { + settings.MAILCHIMP_GROUP_IDS[mailing_list]: False + for mailing_list in mailing_lists + if mailing_list in settings.MAILCHIMP_GROUP_IDS + } + client.lists.members.update( + settings.MAILCHIMP_LIST_ID, subscriber_hash(email), + data={ + 'interests': interests, + } + )