forked from codeforamerica/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
636 lines (497 loc) · 21.3 KB
/
app.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import json
import os
import re
from requests import get, post
from operator import itemgetter
from urlparse import urlparse
from datetime import datetime
from base64 import b64encode
from flask import Flask, render_template, request, redirect, url_for, make_response, flash
import filters
app = Flask(__name__, static_url_path="/brigade/static")
app.register_blueprint(filters.blueprint)
app.config['BRIGADE_SIGNUP_SECRET'] = os.environ['BRIGADE_SIGNUP_SECRET']
app.secret_key = 'SECRET KEY'
@app.context_processor
def get_fragments():
''' The base template includes the signup form and the footer
pulled from our main site.
'''
# Get universal sign up form
r = get("http://www.codeforamerica.org/fragments/email-signup.html")
signup = r.content
# Get footer html
r = get("http://www.codeforamerica.org/fragments/global-footer.html")
footer = r.content
return dict(signup=signup, footer=footer)
def get_brigades():
# Get location of all civic tech orgs
got = get("https://www.codeforamerica.org/api/organizations.geojson")
geojson = got.json()
brigades = []
# Prepare the geojson for a map
for org in geojson["features"]:
# Add icon info for the map
org["properties"]["marker-symbol"] = "town-hall"
# Official Brigades get to be red
if "Official" in org["properties"]["type"]:
org["properties"]["marker-color"] = "#aa1c3a"
else:
# Other Brigades are grey
org["properties"]["marker-color"] = "#6D6E71"
# Grab only orgs with type Brigade
if "Brigade" in org["properties"]["type"]:
brigades.append(org)
brigades = json.dumps(brigades)
return brigades
def is_existing_organization(orgid):
''' tests that an organization exists on the cfapi'''
got = get("https://www.codeforamerica.org/api/organizations.geojson").json()
orgids = [org["properties"]["id"] for org in got["features"]]
return orgid in orgids
# Load load projects from the cfapi
def get_projects(projects, url, limit=10):
got = get(url)
new_projects = got.json()["objects"]
projects = projects + new_projects
if limit:
if len(projects) >= limit:
return projects
if "next" in got.json()["pages"]:
projects = get_projects(projects, got.json()["pages"]["next"], limit)
return projects
# ROUTES
@app.route('/brigade/list', methods=["GET"])
def brigade_list():
brigades = get_brigades()
brigades = json.loads(brigades)
brigades.sort(key=lambda x: x['properties']['city'])
return render_template("brigade_list.html", brigades=brigades )
@app.route('/brigade/')
def index():
brigades = get_brigades()
return render_template("index.html", brigades=brigades )
@app.route("/brigade/signup/", methods=["POST"])
def signup():
''' Takes in signup requests from /brigade/signup/form
Sends the data to a requested mailchimp list, our mailchimp list, and the peopledb
'''
# Prep mailchimp data
# mailchimp_data = {
# 'FNAME' : request.form.get("FNAME"),
# 'LNAME' : request.form.get("LNAME"),
# 'EMAIL' : request.form.get("EMAIL")
# }
# Optionally POST to Brigade's mailchimp
# mailchimp_url = request.form.get("mailchimp_url", None)
# brigade_mailchimp_response = None
# if mailchimp_url:
# brigade_mailchimp_response = post(mailchimp_url, data=mailchimp_data)
# Always POST to Code for America's mailchimp
# mailchimp_data['group[10273][8192]'] = '8192' # I attend Brigade events
# cfa_mailchimp_url = "http://codeforamerica.us2.list-manage.com/subscribe/post-json?u=d9acf2a4c694efbd76a48936f&id=3ac3aef1a5"
# cfa_mailchimp_response = post(cfa_mailchimp_url, data=mailchimp_data)
# Always POST to PeopleDB
peopledb_data = {
'first_name' : request.form.get("FNAME"),
'last_name' : request.form.get("LNAME"),
'email' : request.form.get("EMAIL"),
'brigade_id' : request.form.get("brigade_id", None)
}
auth = app.config['BRIGADE_SIGNUP_SECRET'], 'x-brigade-signup'
url = 'https://people.codeforamerica.org/brigade/signup'
peopledb_response = post(url, data=peopledb_data, auth=auth)
# Choose a response to show
# if brigade_mailchimp_response:
# return brigade_mailchimp_response
# elif cfa_mailchimp_response:
# return cfa_mailchimp_response.content
if peopledb_response:
response = {
"status_code" : peopledb_response.status_code,
"msg" : peopledb_response.content
}
return json.dumps(response)
else:
response = {
"status_code" : 500,
"msg" : "Something went wrong. You were not added to any lists."
}
return response
@app.route("/brigade/signup/", methods=["GET"])
def signup_form():
# Get all of the organizations from the api
organizations = get('https://www.codeforamerica.org/api/organizations.geojson')
organizations = organizations.json()
# Filter out just the organization names
brigades = []
for org in organizations['features']:
brigades.append(org['properties']['name'])
# Alphabetize names
brigades.sort()
return render_template("signup.html", brigades=brigades)
@app.route("/brigade/numbers/")
def numbers():
# Get the total number of Brigades
got = get("https://www.codeforamerica.org/api/organizations?type=Brigade&per_page=1")
got = got.json()
brigades_total = got['total']
# Get the official Brigades
got = get("https://www.codeforamerica.org/api/organizations?type=Official&per_page=1")
got = got.json()
official_brigades_total = got['total']
# Get the total number of Code for All Groups
got = get("https://www.codeforamerica.org/api/organizations?type=Code for All&per_page=1")
got = got.json()
cfall_total = got['total']
# Get the total number of Government Groups
got = get("https://www.codeforamerica.org/api/organizations?type=Government&per_page=1")
got = got.json()
government_total = got['total']
# Get number of meetup-members
got = get("http://codeforamerica.org/api/organizations/member_count")
got = got.json()
member_count = got['total']
# Get number of RSVPs
got = get("https://www.codeforamerica.org/api/events/rsvps")
got = got.json()
rsvps = got['total']
# Get number of Attendance
got = get("https://www.codeforamerica.org/api/attendance")
got = got.json()
attendance = got['total']
# Get total number of projects
got = get("https://www.codeforamerica.org/api/projects?only_ids&per_page=1")
got = got.json()
projects = got['objects']
projects_total = got['total']
# Get total number of Brigade projects
got = get("https://www.codeforamerica.org/api/projects?only_ids&organization_type=Brigade&per_page=1")
got = got.json()
projects = got['objects']
brigade_projects_total = got['total']
# Get total number of Code for All projects
got = get("https://www.codeforamerica.org/api/projects?only_ids&organization_type=Code for All&per_page=1")
got = got.json()
projects = got['objects']
cfall_projects_total = got['total']
# Get total number of Government projects
got = get("https://www.codeforamerica.org/api/projects?only_ids&organization_type=Government&per_page=1")
got = got.json()
projects = got['objects']
gov_projects_total = got['total']
# Get number of Issues
got = get("https://www.codeforamerica.org/api/issues?per_page=1")
got = got.json()
issues_total = got['total']
# Get number of Help Wanted Issues
got = get("https://www.codeforamerica.org/api/issues/labels/help%20wanted?per_page=1")
got = got.json()
help_wanted_total = got['total']
# Get number of civic issue finder clicks
got = get("https://www.codeforamerica.org/geeks/civicissues/analytics/total_clicks")
got = got.json()
total_issue_clicks = got['total_clicks']
kwargs = dict(brigades_total=brigades_total, official_brigades_total=official_brigades_total,
cfall_total=cfall_total, government_total=government_total,
member_count=member_count, rsvps=rsvps, attendance=attendance,
projects_total=projects_total, brigade_projects_total=brigade_projects_total,
cfall_projects_total=cfall_projects_total, gov_projects_total=gov_projects_total,
issues_total=issues_total, help_wanted_total=help_wanted_total, total_issue_clicks=total_issue_clicks)
return render_template("numbers.html", **kwargs )
@app.route("/brigade/about/")
def about():
return render_template("about.html")
@app.route("/brigade/organize/")
def organize():
got = get("http://www.codeforamerica.org/api/organizations.geojson")
geojson = got.json()
brigades = []
# Prepare the geojson for a map
for org in geojson["features"]:
# Grab only orgs with type Brigade
if "Brigade" in org["properties"]["type"]:
brigades.append(org)
elif "Code for All" in org["properties"]["type"]:
brigades.append(org)
brigades = json.dumps(brigades)
# Get universal sign up form
r = get("http://www.codeforamerica.org/fragments/email-signup.html")
signup = r.content
return render_template("organize.html", brigades=brigades, signup=signup)
@app.route("/brigade/tools/")
@app.route("/brigade/tools/<page>/")
def tools(page=None):
if page:
return render_template("tools/"+page+".html")
else:
return render_template("tools/index.html")
@app.route("/brigade/infrastructure")
def infrastructure():
return render_template("infrastructure.html")
@app.route("/brigade/projects")
@app.route("/brigade/<brigadeid>/projects")
def projects(brigadeid=None):
''' Display a list of projects '''
if brigadeid:
if not is_existing_organization(brigadeid):
return render_template('404.html'), 404
projects = []
brigade = None
search = request.args.get("q", None)
sort_by = request.args.get("sort_by", None)
page = request.args.get("page", None)
if page:
if brigadeid:
next = "/brigade/"+brigadeid+"/projects?page=" + str(int(page) + 1)
else:
next = "/brigade/projects?page=" + str(int(page) + 1)
else:
if brigadeid:
next = "/brigade/"+brigadeid+"/projects?page=2"
else:
next = "/brigade/projects?page=2"
if brigadeid:
url = "https://www.codeforamerica.org/api/organizations/"+ brigadeid +"/projects"
if search or sort_by or page:
url += "?"
if search:
url += "&q=" + search
if sort_by:
url += "&sort_by" + sort_by
if page:
url += "&page=" + page
got = get(url)
projects = get_projects(projects, url)
if projects:
brigade = projects[0]["organization"]
else:
brigade = { "name" : brigadeid.replace("-"," ")}
else:
url = "https://www.codeforamerica.org/api/projects"
if search or sort_by or page:
url += "?"
if search:
url += "&q=" + search
if sort_by:
url += "&sort_by" + sort_by
if page:
url += "&page=" + page
got = get(url)
projects = get_projects(projects, url)
return render_template("projects.html", projects=projects, brigade=brigade, next=next)
@app.route("/brigade/attendance")
@app.route("/brigade/<brigadeid>/attendance")
def attendance(brigadeid=None):
''' Show the Brigade attendance '''
if brigadeid:
if not is_existing_organization(brigadeid):
return render_template('404.html'), 404
if not brigadeid:
got = get("https://www.codeforamerica.org/api/attendance")
else:
got = get("https://www.codeforamerica.org/api/organizations/%s/attendance" % brigadeid)
attendance = got.json()
if attendance["weekly"]:
# GCharts wants a list of lists
attendance["weeks"] = []
for key, value in attendance["weekly"].iteritems():
week = [str(key), value]
attendance["weeks"].append(week)
attendance["weeks"] = sorted(attendance["weeks"], key=itemgetter(0))
attendance["this_week"] = 0
attendance["last_week"] = 0
if len(attendance["weeks"]) >= 1:
attendance["this_week"] = attendance["weeks"][-1][1]
if len(attendance["weeks"]) >= 2:
attendance["last_week"] = attendance["weeks"][-2][1]
return render_template("attendance.html", brigadeid=brigadeid, attendance=attendance)
@app.route("/brigade/rsvps")
@app.route("/brigade/<brigadeid>/rsvps")
def rsvps(brigadeid=None):
''' Show the Brigade rsvps '''
if brigadeid:
if not is_existing_organization(brigadeid):
return render_template('404.html'), 404
if not brigadeid:
got = get("https://www.codeforamerica.org/api/events/rsvps")
else:
got = get("https://www.codeforamerica.org/api/organizations/%s/events/rsvps" % brigadeid)
rsvps = got.json()
if rsvps["weekly"]:
# GCharts wants a list of lists
rsvps["weeks"] = []
for key, value in rsvps["weekly"].iteritems():
week = [str(key), value]
rsvps["weeks"].append(week)
rsvps["weeks"] = sorted(rsvps["weeks"], key=itemgetter(0))
rsvps["this_week"] = 0
rsvps["last_week"] = 0
if len(rsvps["weeks"]) >= 1:
rsvps["this_week"] = rsvps["weeks"][-1][1]
if len(rsvps["weeks"]) >= 2:
rsvps["last_week"] = rsvps["weeks"][-2][1]
return render_template("rsvps.html", brigadeid=brigadeid, rsvps=rsvps)
@app.route('/brigade/index/<brigadeid>/')
def redirect_brigade(brigadeid):
''' Redirect old Brigade links to new Brigade links'''
return redirect("/brigade/"+brigadeid, code=301)
@app.route('/brigade/<brigadeid>/')
def brigade(brigadeid):
''' Get this Brigade's info '''
if brigadeid:
if not is_existing_organization(brigadeid):
return render_template('404.html'), 404
got = get("https://www.codeforamerica.org/api/organizations/" + brigadeid)
brigade = got.json()
return render_template("brigade.html", brigade=brigade, brigadeid=brigadeid)
@app.route("/brigade/checkin/", methods=["GET"])
@app.route("/brigade/<brigadeid>/checkin/", methods=["GET"])
def get_checkin(brigadeid=None):
''' Checkin to a Brigade event '''
if brigadeid:
if not is_existing_organization(brigadeid):
return render_template('404.html'), 404
brigades = None
if not brigadeid:
# Get all of the organizations from the api
organizations = get('https://www.codeforamerica.org/api/organizations.geojson')
organizations = organizations.json()
brigades = []
# Org's names and ids
for org in organizations['features']:
if "Brigade" in org['properties']['type']:
brigades.append({
"name": org['properties']['name'],
"id": org['id']
})
# Alphabetize names
brigades.sort(key=lambda x: x.values()[0])
# If we want to remember the event, question
event = request.args.get("event", None)
question = request.args.get("question", None)
return render_template("checkin.html", brigadeid=brigadeid,
event=event, brigades=brigades, question=question)
@app.route("/brigade/checkin/", methods=["POST"])
@app.route("/brigade/<brigadeid>/checkin/", methods=["POST"])
def post_checkin(brigadeid=None):
''' Prep the checkin for posting to the peopledb '''
# VALIDATE
cfapi_url = request.form.get('cfapi_url')
if not cfapi_url:
return make_response("Missing required cfapi_url", 422)
elif not re.match("https:\/\/www\.codeforamerica\.org\/api\/organizations\/[A-Za-z-]*", cfapi_url):
return make_response("cfapi_url needs to like https://www.codeforamerica.org/api/organizations/Brigade-ID", 422)
brigadeid = request.form.get('cfapi_url').split("/")[-1]
if not is_existing_organization(brigadeid):
return make_response(brigadeid + "is not an existing brigade." , 422)
# MAILCHIMP SIGNUP
if request.form.get("mailinglist", None):
if request.form.get("email", None):
# Split first and last name
name = request.form.get('name', None)
if name:
if ' ' in request.form['name']:
first_name, last_name = name.split(' ', 1)
else:
first_name, last_name = name, ''
else:
first_name, last_name = None, None
mailchimp_data = {
'FNAME' : first_name,
'LNAME' : last_name,
'EMAIL' : request.form.get("email"),
'REFERRAL' : request.url,
'group[10273][8192]' : '8192', # I attend Brigade events
'group[10245][32]' : '32' # Brigade newsletter
}
cfa_mailchimp_url = "http://codeforamerica.us2.list-manage.com/subscribe/post-json?u=d9acf2a4c694efbd76a48936f&id=3ac3aef1a5"
cfa_mailchimp_response = post(cfa_mailchimp_url, data=mailchimp_data)
if cfa_mailchimp_response.status_code != 200:
return cfa_mailchimp_response.content
# Prep PeopleDB post
# Q&A is stored as a json string
extras = {}
extras["question"] = request.form.get("question", None)
extras["answer"] = request.form.get("answer", None)
extras = json.dumps(extras)
peopledb_post = {
"name": request.form.get('name', None),
"email": request.form.get("email", None),
"event": request.form.get("event", None),
"date": request.form.get("date", datetime.now()),
"org_cfapi_url": request.form.get('cfapi_url'),
"extras" : extras
}
auth = app.config["BRIGADE_SIGNUP_SECRET"] + ':x-brigade-signup'
headers = {'Authorization': 'Basic ' + b64encode(auth)}
peopleapp = "https://people.codeforamerica.org/checkin"
r = post(peopleapp, data=peopledb_post, headers=headers)
if r.status_code == 200:
# Remembering event name and brigadeid for later
event = request.form.get("event", None)
question = request.form.get("question", None)
brigadeid = request.form.get("cfapi_url").replace("https://www.codeforamerica.org/api/organizations/","")
flash("Thanks for volunteering")
if brigadeid:
url = "brigade/"+ brigadeid +"/checkin/"
else:
url = "brigade/checkin/"
if event or question:
url += "?"
if event:
event = event.replace(" ","+")
url += "event=" + event
if event and question:
url += "&"
if question:
question = question.replace(" ","+")
url += "question=" + question
return redirect(url)
# Pass any errors through
else:
return make_response(r.content, r.status_code)
@app.route("/brigade/test-checkin/", methods=["POST"])
@app.route("/brigade/<brigadeid>/test-checkin/", methods=["POST"])
def post_test_checkin(brigadeid=None):
''' Prep the checkin for posting to the peopledb '''
test_checkin_data = {
"name": request.form.get('name', None),
"email": request.form.get("email", None),
"event": request.form.get("event", None),
"date": request.form.get("date", str(datetime.now())),
"cfapi_url": request.form.get('cfapi_url'),
"question" : request.form.get("question", None),
"answer" : request.form.get("answer", None)
}
if not test_checkin_data["cfapi_url"]:
return make_response("Missing required cfapi_url", 422)
elif not re.match("https:\/\/www\.codeforamerica\.org\/api\/organizations\/[A-Za-z-]*", test_checkin_data["cfapi_url"]):
return make_response("cfapi_url needs to like https://www.codeforamerica.org/api/organizations/Brigade-ID", 422)
brigadeid = test_checkin_data["cfapi_url"].split("/")[-1]
if not is_existing_organization(brigadeid):
return make_response(brigadeid + "is not an existing brigade." , 422)
else:
return make_response(json.dumps(test_checkin_data), 200)
@app.route('/brigade/projects/monitor')
@app.route('/brigade/<brigadeid>/projects/monitor')
def project_monitor(brigadeid=None):
''' Check for Brigade projects on Travis'''
limit = int(request.args.get('limit',50))
travis_projects = []
projects = []
if not brigadeid:
projects = get_projects(projects, "https://www.codeforamerica.org/api/projects", limit)
else:
projects = get_projects(projects, "https://www.codeforamerica.org/api/organizations/"+brigadeid+"/projects", limit)
# Loop through projects and get
for project in projects:
if project["code_url"]:
url = urlparse(project["code_url"])
if url.netloc == "github.com":
travis_url = "https://api.travis-ci.org/repositories"+url.path+"/builds"
project["travis_url"] = travis_url
travis_projects.append(project)
return render_template('projectmonitor.html', projects=travis_projects, org_name=brigadeid)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True, port=4000)