-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmailchimp.py
More file actions
129 lines (90 loc) · 3.61 KB
/
Copy pathmailchimp.py
File metadata and controls
129 lines (90 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import calendar
from datetime import date
import os
import requests
from django.template.loader import get_template
from ukpython.models import Event, MonthlyMessage, NewsItem, SponsoredNewsItem, UserGroup
API_KEY = os.getenv('MAILCHIMP_API_KEY')
DATA_CENTRE = API_KEY.split('-')[-1]
BASE_URL = 'https://{}.api.mailchimp.com/3.0/'.format(DATA_CENTRE)
AUTH = ('', API_KEY)
LIST_NAME = 'UK Python News'
TEMPLATE_NAME = '1 Column'
def get(path, params=None):
rsp = requests.get(BASE_URL + path, auth=AUTH, params=params)
rsp.raise_for_status()
return rsp.json()
def post(path, data=None):
rsp = requests.post(BASE_URL + path, auth=AUTH, json=data)
rsp.raise_for_status()
return rsp.json()
def put(path, data=None):
rsp = requests.put(BASE_URL + path, auth=AUTH, json=data)
rsp.raise_for_status()
return rsp.json()
def get_list_id(list_name=LIST_NAME):
lists = get('lists')['lists']
for record in lists:
if record['name'] == list_name:
return record['id']
def get_campaign_id(campaign_title):
campaigns = get('campaigns', {'status': 'save'})['campaigns']
for record in campaigns:
if record['settings']['title'] == campaign_title:
return record['id']
def get_template_id(template_name=TEMPLATE_NAME):
templates = get('templates', {'count': 1000})['templates']
for record in templates:
if record['name'] == template_name:
return record['id']
def get_or_create_campaign(year, month):
title = 'UK Python News {}-{:02d}'.format(year, month)
campaign_id = get_campaign_id(title)
if campaign_id is not None:
return campaign_id
list_id = get_list_id()
month_name = calendar.month_name[int(month)]
subject_line = 'UK Python News {} {}'.format(month_name, year)
data = {
'type': 'regular',
'recipients': {'list_id': list_id},
'settings': {
'subject_line': subject_line,
'title': title,
'from_name': 'UK Python Association',
'reply_to': 'news@uk.python.org',
},
}
return post('campaigns', data)['id']
def generate_html(year, month):
monthly_message = MonthlyMessage.objects.for_month(year, month)
news_items = NewsItem.objects.for_newsletter(year, month)
sponsored_news_items = SponsoredNewsItem.objects.for_newsletter(year, month)
upcoming_events = Event.objects.scheduled_in_month(year, month).order_by('date')
groups_with_no_events_scheduled = UserGroup.objects.no_events_scheduled(year, month).order_by('name')
month_name = calendar.month_name[month]
next_month_name = calendar.month_name[(month + 1) % 12]
next_month_deadline = date(year, month, 24)
template = get_template('mailchimp-newsletter.html')
context = {
'monthly_message': monthly_message,
'news_items': news_items,
'sponsored_news_items': sponsored_news_items,
'upcoming_events': upcoming_events,
'groups_with_no_events_scheduled': groups_with_no_events_scheduled,
'month_name': month_name,
'next_month_name': next_month_name,
'next_month_deadline': next_month_deadline,
}
return template.render(context)
def update_campaign_content(campaign_id, year, month):
html = generate_html(year, month)
template_id = get_template_id()
data = {
'html': html,
'template': {'id': template_id}, # This doesn't actually work
}
put('campaigns/{}/content'.format(campaign_id), data)
def update_mailchimp(year, month):
campaign_id = get_or_create_campaign(year, month)
update_campaign_content(campaign_id, year, month)