polishing map fixes
[wolnelektury.git] / src / newsletter / subscribe.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import requests
5 from django.conf import settings
6 from club.civicrm import civicrm
7
8
9 def subscribe(email, newsletter):
10     if newsletter.crm_id:
11         subscribe_crm(email, newsletter.crm_id)
12     if newsletter.phplist_id:
13         subscribe_phplist(email, newsletter.phplist_id)
14
15 def subscribe_crm(email, group_id):
16     civicrm.add_email_to_group(email, group_id)
17
18 def subscribe_phplist(email, list_id):
19     data = {
20         "email": email,
21         "emailconfirm": email,
22         f"list[{list_id}]": "signup",
23         "htmlemail": 1,
24         "subscribe": "Subscribe",
25     }
26     if settings.NEWSLETTER_PHPLIST_SUBSCRIBE_URL:
27         response = requests.post(
28             settings.NEWSLETTER_PHPLIST_SUBSCRIBE_URL,
29             data=data,
30         )
31         response.raise_for_status()
32     else:
33         print("Newsletter not configured, "
34             "NEWSLETTER_PHPLIST_SUBSCRIBE_URL not set. "
35             f"Trying to subscribe email: {email} to newsletter: {list_id}."
36         )