-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
58 lines (46 loc) · 1.88 KB
/
server.py
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
import json
from flask import Flask, render_template, request, Response
from mailchimp3 import MailChimp
try:
import config
has_local_config = True
except ImportError:
has_local_config = False
app = Flask(__name__)
@app.route('/')
def hello_world():
if has_local_config:
template_vars = {
'api_key': config.MAILCHIMP_SECRET_KEY,
'analytics_id': config.ANALYTICS_ID,
}
else:
template_vars = {}
return render_template('home.html', **template_vars)
@app.route('/api/lists/', methods=['POST'])
def get_lists():
client = _get_mailchimp_client()
lists = client.lists.all(get_all=True, fields="lists.name,lists.id")
return Response(json.dumps(lists), mimetype='application/json')
@app.route('/api/members/', methods=['POST'])
def get_members():
client = _get_mailchimp_client()
list_id = request.form['list']
members = client.lists.members.all(list_id, get_all=True, status='pending',
fields="members.email_address,members.id,members.timestamp_signup,members.status")
return Response(json.dumps(members), mimetype='application/json')
@app.route('/api/update_member/', methods=['POST'])
def update_member():
client = _get_mailchimp_client()
list_id = request.form['list']
member_id = request.form['member-id']
status = request.form['status']
if status == 'deleted':
# special case deletion
client.lists.members.delete(list_id, subscriber_hash=member_id)
else:
client.lists.members.update(list_id, subscriber_hash=member_id, data={"status": status})
return Response(json.dumps({'id': member_id, 'status': status}), mimetype='application/json')
def _get_mailchimp_client():
api_key = request.form['api-key'] or config.MAILCHIMP_SECRET_KEY
return MailChimp('', api_key) # first argument is username which is not necessary