This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
/
app.py
executable file
·421 lines (324 loc) · 13.7 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
#!/usr/bin/env python
import cStringIO
import datetime
import json
from mimetypes import guess_type
import urllib
from csvkit.unicsv import UnicodeCSVDictWriter
import envoy
from flask import Flask, Markup, abort, render_template, url_for
from peewee import fn
import app_config
import copytext
from models import Expenditure, Legislator, Lobbyist, Organization
from render_utils import flatten_app_config, make_context
app = Flask(app_config.PROJECT_NAME)
def get_ago():
"""
Generate a datetime that will include 24 reporting periods
for which we have data.
"""
most_recent = Expenditure.select().order_by(Expenditure.report_period.desc()).limit(1)[0].report_period
# Get the previous month. If previous month is December, set to 12.
month = most_recent.month + 1
if month > 12:
month = 12
ago = datetime.date(most_recent.year - 2, month, 1)
return ago
@app.route('/')
def index():
"""
Example view demonstrating rendering a simple HTML page.
"""
context = make_context()
ago = get_ago()
expenditures = Expenditure.select().where(Expenditure.report_period >= ago)
organizations = Organization.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
lobbyists = Lobbyist.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
legislators = Legislator.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
for legislator in legislators:
legislator.total_spending = legislator.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))
legislators_total_spending = sorted(legislators, key=lambda l: l.total_spending, reverse=True)[:10]
categories_total_spending = {}
for org in organizations:
org.total_spending = org.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))
if not org.total_spending:
continue
if org.category in categories_total_spending:
categories_total_spending[org.category] += org.total_spending
else:
categories_total_spending[org.category] = org.total_spending
organizations_total_spending = sorted(organizations, key=lambda o: o.total_spending, reverse=True)[:10]
categories_total_spending = sorted(categories_total_spending.items(), key=lambda c: c[1], reverse=True)
context['senators'] = Legislator.select().where(Legislator.office == 'Senator')
context['representatives'] = Legislator.select().where(Legislator.office == 'Representative')
context['expenditures'] = expenditures
context['total_spending'] = expenditures.aggregate(fn.Sum(Expenditure.cost))
context['total_expenditures'] = expenditures.count()
context['total_organizations'] = organizations.count()
context['total_lobbyists'] = lobbyists.count()
context['organizations_total_spending'] = organizations_total_spending
context['legislators_total_spending'] = legislators_total_spending
context['categories_total_spending'] = categories_total_spending
return render_template('index.html', **context)
@app.route('/legislators/')
def legislators():
"""
Legislator list page.
"""
context = make_context()
senate_list = Legislator.select().where(Legislator.office == 'Senator')
house_list = Legislator.select().where(Legislator.office == 'Representative')
context['senate_list'] = senate_list
context['house_list'] = house_list
return render_template('legislator_list.html', **context)
@app.route('/organizations/')
def organizations():
"""
Legislator list page.
"""
context = make_context()
context['organizations'] = Organization.select().order_by(Organization.name)
return render_template('organization_list.html', **context)
@app.route('/methodology/')
def methodology():
"""
Methodology explainer page.
"""
context = make_context()
return render_template('methodology.html', **context)
@app.route('/download/lobbyingmissouri.csv')
def download_csv():
"""
Generate a data download.
"""
f = cStringIO.StringIO()
writer = UnicodeCSVDictWriter(f, [
'lobbyist_first_name',
'lobbyist_last_name',
'report_period',
'recipient_name',
'recipient_type',
'legislator_first_name',
'legislator_last_name',
'legislator_office',
'legislator_party',
'legislator_district',
'event_date',
'category',
'description',
'cost',
'organization_name',
'organization_industry',
'group',
'ethics_board_id',
'is_solicitation'
])
writer.writeheader()
expenditures = Expenditure.select()
for ex in expenditures:
row = {
'lobbyist_first_name': ex.lobbyist.first_name,
'lobbyist_last_name': ex.lobbyist.last_name,
'report_period': ex.report_period,
'recipient_name': ex.recipient,
'recipient_type': ex.recipient_type,
'legislator_first_name': ex.legislator.first_name if ex.legislator else None,
'legislator_last_name': ex.legislator.last_name if ex.legislator else None,
'legislator_office': ex.legislator.office if ex.legislator else None,
'legislator_party': ex.legislator.party if ex.legislator else None,
'legislator_district': ex.legislator.district if ex.legislator else None,
'event_date': ex.event_date,
'category': ex.category,
'description': ex.description,
'cost': ex.cost,
'organization_name': ex.organization.name,
'organization_industry': ex.organization.category,
'group': ex.group.name if ex.group else None,
'ethics_board_id': ex.ethics_id,
'is_solicitation': ex.is_solicitation
}
writer.writerow(row)
return f.getvalue().decode('utf-8')
@app.route('/sitemap.xml')
def sitemap():
"""
Renders a sitemap.
"""
context = make_context()
context['pages'] = []
now = datetime.date.today().isoformat()
context['pages'].append(('/', now))
context['pages'].append(('/methodology/', now))
context['pages'].append(('/legislators/', now))
context['pages'].append(('/organizations/', now))
for legislator in Legislator.select():
context['pages'].append((url_for('_legislator', slug=legislator.slug), now))
for organization in Organization.select():
context['pages'].append((url_for('_organization', slug=organization.slug), now))
sitemap = render_template('sitemap.xml', **context)
return (sitemap, 200, { 'content-type': 'application/xml' })
@app.route('/promo.html')
def promo():
"""
Promo page.
"""
context = make_context()
return render_template('promo.html', **context)
@app.route('/legislators/<string:slug>/')
def _legislator(slug):
"""
Legislator detail page.
"""
context = make_context()
ago = get_ago()
legislators = Legislator.select()
legislator = Legislator.get(Legislator.slug==slug)
for l in legislators:
l.total_spending = l.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))
legislators_total_spending = sorted(legislators, key=lambda l: l.total_spending, reverse=True)
legislator_rank = None
for i, l in enumerate(legislators_total_spending):
if l.id == legislator.id:
legislator_rank = i + 1
org_spending = {}
for ex in legislator.expenditures:
if ex.organization.id in org_spending:
org_spending[ex.organization.id] += ex.cost
else:
org_spending[ex.organization.id] = ex.cost
top_organizations = []
top_categories = {}
for org_id, spending in org_spending.items():
org = Organization.get(Organization.id == org_id)
org.total_spending = spending
top_organizations.append(org)
if org.category in top_categories:
top_categories[org.category] += org.total_spending
else:
top_categories[org.category] = org.total_spending
top_organizations = sorted(top_organizations, key=lambda o: o.total_spending, reverse=True)[:10]
top_categories = sorted(top_categories.items(), key=lambda c: c[1], reverse=True)
context['legislator'] = legislator
context['expenditures_recent'] = legislator.expenditures.where(Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
context['total_spending'] = sum([e.cost for e in legislator.expenditures])
context['total_spending_recent'] = sum([e.cost for e in legislator.expenditures.where(Expenditure.report_period >= ago)])
context['total_expenditures'] = legislator.expenditures.count()
context['total_expenditures_recent'] = legislator.expenditures.where(Expenditure.report_period >= ago).count()
context['top_organizations'] = top_organizations
context['legislator_rank'] = legislator_rank
context['top_categories'] = top_categories
return render_template('legislator.html', **context)
@app.route('/organizations/<string:slug>/')
def _organization(slug):
"""
Organization detail page.
"""
context = make_context()
ago = get_ago()
organization = Organization.get(Organization.slug==slug)
organizations = Organization.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
for o in organizations:
o.total_spending = o.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))
organizations_total_spending = sorted(organizations, key=lambda o: o.total_spending, reverse=True)
organization_rank = None
for i, o in enumerate(organizations_total_spending):
if o.id == organization.id:
organization_rank = i + 1
legislator_spending = {}
for ex in organization.expenditures:
# Groups or old/non-attributable expenses
if not ex.legislator:
continue
if ex.legislator.id in legislator_spending:
legislator_spending[ex.legislator.id] += ex.cost
else:
legislator_spending[ex.legislator.id] = ex.cost
top_legislators = []
for legislator_id, spending in legislator_spending.items():
legislator = Legislator.get(Legislator.id == legislator_id)
legislator.total_spending = spending
top_legislators.append(legislator)
top_legislators = sorted(top_legislators, key=lambda o: o.total_spending, reverse=True)[:10]
context['organization'] = organization
context['expenditures_recent'] = organization.expenditures.where(Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
context['total_spending'] = sum([e.cost for e in organization.expenditures])
context['total_spending_recent'] = sum([e.cost for e in organization.expenditures.where(Expenditure.report_period >= ago)])
context['total_expenditures'] = organization.expenditures.count()
context['total_expenditures_recent'] = organization.expenditures.where(Expenditure.report_period >= ago).count()
context['top_legislators'] = top_legislators
context['organization_rank'] = organization_rank
return render_template('organization.html', **context)
# Render LESS files on-demand
@app.route('/less/<string:filename>')
def _less(filename):
try:
with open('less/%s' % filename) as f:
less = f.read()
except IOError:
abort(404)
r = envoy.run('node_modules/bin/lessc -', data=less)
return r.std_out, 200, { 'Content-Type': 'text/css' }
# Render JST templates on-demand
@app.route('/js/templates.js')
def _templates_js():
r = envoy.run('node_modules/bin/jst --template underscore jst')
return r.std_out, 200, { 'Content-Type': 'application/javascript' }
# Render application configuration
@app.route('/js/app_config.js')
def _app_config_js():
config = flatten_app_config()
js = 'window.APP_CONFIG = ' + json.dumps(config)
return js, 200, { 'Content-Type': 'application/javascript' }
# Render copytext
@app.route('/js/copy.js')
def _copy_js():
copy = 'window.COPY = ' + copytext.Copy().json()
return copy, 200, { 'Content-Type': 'application/javascript' }
# Server arbitrary static files on-demand
@app.route('/<path:path>')
def _static(path):
try:
with open('www/%s' % path) as f:
return f.read(), 200, { 'Content-Type': guess_type(path)[0] }
except IOError:
abort(404)
@app.template_filter('urlencode')
def urlencode_filter(s):
"""
Filter to urlencode strings.
"""
if type(s) == 'Markup':
s = s.unescape()
s = s.encode('utf8')
s = urllib.quote_plus(s)
return Markup(s)
@app.template_filter('format_currency')
def format_currency(value):
return "${:,.2f}".format(value)
@app.template_filter('format_currency_round')
def format_currency_round(value):
return "${:,.0f}".format(value)
@app.template_filter('apnumber')
def apnumber(value):
"""
Borrowed with love and adapted from django.contrib.humanize: https://github.com/django/django/blob/master/django/contrib/humanize/templatetags/humanize.py
For numbers 1-9, returns the number spelled out. Otherwise, returns the
number. This follows Associated Press style.
"""
try:
value = int(value)
except (TypeError, ValueError):
return value
if not 0 <= value < 10:
return value
return ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')[value]
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port')
args = parser.parse_args()
server_port = 8000
if args.port:
server_port = int(args.port)
app.run(host='0.0.0.0', port=server_port, debug=app_config.DEBUG)