This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
forked from codeforamerica/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
354 lines (281 loc) · 11.5 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
import json
import os
from requests import get, post
import datetime
from flask import Flask, render_template, request, redirect
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.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
# ROUTES
@app.route('/brigade/list', methods=["GET"])
def brigade_list():
brigades = get_brigades()
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 total number of projects
got = get("https://www.codeforamerica.org/api/projects?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?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?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?organization_type=Government&per_page=1")
got = got.json()
projects = got['objects']
gov_projects_total = got['total']
# Get number of Health projects
got = get("https://www.codeforamerica.org/api/projects?q=health&per_page=1")
got = got.json()
projects = got['objects']
health_total = got['total']
# Get number of Money projects
got = get("https://www.codeforamerica.org/api/projects?q=money&per_page=1")
got = got.json()
projects = got['objects']
money_total = got['total']
# Get number of Justice projects
got = get("https://www.codeforamerica.org/api/projects?q=justice&per_page=1")
got = got.json()
projects = got['objects']
justice_total = got['total']
# Get number of Issues
got = get("https://www.codeforamerica.org/api/issues?per_page=1")
got = got.json()
issues = got['objects']
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()
issues = got['objects']
help_wanted_total = got['total']
# Get number of Bug Issues
got = get("https://www.codeforamerica.org/api/issues/labels/bug?per_page=1")
got = got.json()
issues = got['objects']
bug_total = got['total']
# Get number of Enhancement Issues
got = get("https://www.codeforamerica.org/api/issues/labels/enhancement?per_page=1")
got = got.json()
issues = got['objects']
enhancement_total = got['total']
kwargs = dict(brigades_total=brigades_total, official_brigades_total=official_brigades_total, cfall_total=cfall_total, government_total=government_total, projects_total=projects_total, brigade_projects_total=brigade_projects_total, cfall_projects_total=cfall_projects_total, gov_projects_total=gov_projects_total, health_total=health_total, money_total=money_total, justice_total=justice_total, issues_total=issues_total, help_wanted_total=help_wanted_total, bug_total=bug_total, enhancement_total=enhancement_total)
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/projects/")
@app.route("/brigade/<brigadeid>/projects/")
def projects(brigadeid=None):
''' Display a list of projects '''
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"
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
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)
brigade = projects[0]["organization"]
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/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
got = get("https://www.codeforamerica.org/api/organizations/" + brigadeid)
brigade = got.json()
if 'status' in brigade:
if brigade['status'] == 'Resource Not Found':
return render_template('404.html'), 404
return render_template("brigade.html", brigade=brigade, brigadeid=brigadeid)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)