forked from fecgov/openFEC-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
414 lines (307 loc) · 10.7 KB
/
__init__.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
import re
import http
import json
import locale
import hashlib
import logging
import datetime
import furl
import jinja2
from webargs import fields
from webargs.flaskparser import use_kwargs
from dateutil.parser import parse as parse_date
from raven.contrib.flask import Sentry
from hmac_authentication import hmacauth
from flask import Flask, render_template, request, redirect, url_for, abort
from flask_sslify import SSLify
from flask.ext.basicauth import BasicAuth
from werkzeug.contrib.fixers import ProxyFix
from openfecwebapp import utils
from openfecwebapp import views
from openfecwebapp import config
from openfecwebapp.config import env
from openfecwebapp import constants
from openfecwebapp.api_caller import load_search_results, load_with_nested
locale.setlocale(locale.LC_ALL, '')
START_YEAR = 1979
app = Flask(__name__, static_path='/static', static_folder='dist')
# ===== configure logging =====
logger = logging.getLogger(__name__)
log_level = logging.DEBUG if config.debug else logging.WARN
logging.basicConfig(level=log_level)
@jinja2.contextfunction
def get_context(c):
return c
def get_absolute_url():
url = furl.furl(request.url)
if app.config['SERVER_NAME']:
url.host = app.config['SERVER_NAME']
url.scheme = app.config['PREFERRED_URL_SCHEME']
return url.url
def _get_default_cycles():
cycle = utils.current_cycle()
return list(range(cycle - 4, cycle + 2, 2))
def series_has_data(values, key):
return next(
(True for value in values if value.get(key) is not None),
False,
)
def group_has_data(value, keys):
return next(
(True for key in keys if value.get(key) is not None),
False,
)
def series_group_has_data(groups, keys):
return next(
(True for group in groups if group_has_data(group, keys)),
False,
)
def get_cycles():
return range(utils.current_cycle(), START_YEAR, -2)
def cycle_start(value):
return datetime.datetime(value - 1, 1, 1)
def cycle_end(value):
return datetime.datetime(value, 12, 31)
def nullify(value, *nulls):
return value if value not in nulls else None
def get_election_url(candidate, cycle, district=None):
return url_for(
'elections',
office=candidate['office_full'].lower(),
state=nullify(candidate['state'], 'US'),
district=nullify(district or candidate['district'], '00'),
cycle=cycle,
)
try:
assets = json.load(open('./rev-manifest.json'))
except OSError:
logger.error(
'Manifest "rev-manifest.json" not found. Did you remember to run '
'"npm run build"?'
)
raise
assets = {
key: value.replace('dist', '')
for key, value in assets.items()
}
def asset_for(path):
return url_for('static', filename=assets[path].lstrip('/'))
def get_base_path():
return request.headers.get('X-Script-Name', '')
app.jinja_env.globals.update({
'min': min,
'max': max,
'api_location': config.api_location_public,
'api_version': config.api_version,
'api_key': config.api_key_public,
'use_analytics': config.use_analytics,
'style_url': config.style_url,
'cms_url': config.cms_url,
'context': get_context,
'absolute_url': get_absolute_url,
'contact_email': '[email protected]',
'default_cycles': _get_default_cycles(),
'series_has_data': series_has_data,
'group_has_data': group_has_data,
'series_group_has_data': series_group_has_data,
'cycle_start': cycle_start,
'cycle_end': cycle_end,
'election_url': get_election_url,
'constants': constants,
'cycles': get_cycles(),
'assets': assets,
'asset_for': asset_for,
'base_path': get_base_path,
'environment': config.environment,
'today': datetime.date.today,
})
@app.route('/')
def search():
query = request.args.get('search')
if query:
result_type = request.args.get('search_type') or 'candidates'
results = load_search_results(query, result_type)
return views.render_search_results(results, query, result_type)
else:
return render_template('search.html', page='home', dates=utils.date_ranges())
@app.route('/api/')
def api():
"""Redirect to API as described at
https://18f.github.io/API-All-the-X/pages/developer_hub_kit.
"""
return redirect(config.api_location, http.client.MOVED_PERMANENTLY)
@app.route('/developers/')
def developers():
"""Redirect to developer portal as described at
https://18f.github.io/API-All-the-X/pages/developer_hub_kit.
"""
url = furl.furl(config.api_location)
url.path.add('developers')
return redirect(url.url, http.client.MOVED_PERMANENTLY)
@app.route('/candidate/<c_id>/')
@use_kwargs({
'cycle': fields.Int(),
})
def candidate_page(c_id, cycle=None):
"""Fetch and render data for candidate detail page.
:param int cycle: Optional cycle for associated committees and financials.
"""
candidate, committees, cycle = load_with_nested('candidate', c_id, 'committees', cycle)
return views.render_candidate(candidate, committees, cycle)
@app.route('/committee/<c_id>/')
@use_kwargs({
'cycle': fields.Int(),
})
def committee_page(c_id, cycle=None):
"""Fetch and render data for committee detail page.
:param int cycle: Optional cycle for financials.
"""
committee, candidates, cycle = load_with_nested('committee', c_id, 'candidates', cycle)
return views.render_committee(committee, candidates, cycle)
@app.route('/candidates/')
def candidates():
return render_template('candidates.html', result_type='candidates')
@app.route('/committees/')
def committees():
return render_template(
'committees.html',
result_type='committees',
dates=utils.date_ranges(),
)
@app.route('/receipts/')
def receipts():
return render_template('receipts.html', dates=utils.date_ranges())
@app.route('/disbursements/')
def disbursements():
return render_template('disbursements.html', dates=utils.date_ranges())
@app.route('/filings/')
def filings():
return render_template(
'filings.html',
dates=utils.date_ranges(),
result_type='committees',
)
@app.route('/elections/')
def election_lookup():
return render_template('election-lookup.html')
@app.route('/elections/<office>/<int:cycle>/')
@app.route('/elections/<office>/<state>/<int:cycle>/')
@app.route('/elections/<office>/<state>/<district>/<int:cycle>/')
def elections(office, cycle, state=None, district=None):
if office.lower() not in ['president', 'senate', 'house']:
abort(404)
if state and state.upper() not in constants.states:
abort(404)
cycles = get_cycles()
if office.lower() == 'president':
cycles = [each for each in cycles if each % 4 == 0]
return render_template(
'elections.html',
office=office,
cycle=cycle,
cycles=cycles,
state=state,
state_full=constants.states[state.upper()] if state else None,
district=district,
title=election_title(cycle, office, state, district),
)
app.add_url_rule('/issue/', view_func=views.GithubView.as_view('issue'))
def election_title(cycle, office, state=None, district=None):
base = ' '.join([str(cycle), 'Election', 'United States', office.capitalize()])
parts = [base]
if state:
parts.append(constants.states[state.upper()])
if district:
parts.append('District {0}'.format(district))
return ' - '.join(parts)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def server_error(e):
return render_template('500.html'), 500
@app.template_filter('currency')
def currency_filter(num, grouping=True):
if isinstance(num, (int, float)):
return locale.currency(num, grouping=grouping)
return None
def ensure_date(value):
if isinstance(value, (datetime.date, datetime.datetime)):
return value
return parse_date(value)
@app.template_filter('date')
def date_filter(value, fmt='%m/%d/%Y'):
if value is None:
return None
return ensure_date(value).strftime(fmt)
@app.template_filter('json')
def json_filter(value):
return json.dumps(value)
def _unique(values):
ret = []
for value in values:
if value not in ret:
ret.append(value)
return ret
def _fmt_chart_tick(value):
return parse_date(value).strftime('%m/%d/%y')
@app.template_filter('fmt_chart_ticks')
def fmt_chart_ticks(group, keys):
if not group or not keys:
return ''
if isinstance(keys, (list, tuple)):
values = [_fmt_chart_tick(group[key]) for key in keys]
values = _unique(values)
return ' – '.join(values)
return _fmt_chart_tick(group[keys])
@app.template_filter()
def fmt_year_range(year):
if type(year) == int:
return "{}–{}".format(year - 1, year)
return None
@app.template_filter()
def fmt_report_desc(report_full_description):
if report_full_description:
return re.sub('{.+}', '', report_full_description)
@app.template_filter()
def restrict_cycles(value, start_year=START_YEAR):
return [each for each in value if start_year <= each <= utils.current_cycle()]
@app.template_filter()
def fmt_state_full(value):
return constants.states[value.upper()]
# If HTTPS is on, apply full HSTS as well, to all subdomains.
# Only use when you're sure. 31536000 = 1 year.
if config.force_https:
sslify = SSLify(app, permanent=True, age=31536000, subdomains=True)
if config.environment in ['stage', 'prod']:
app.config['PREFERRED_URL_SCHEME'] = 'https'
# Note: Apply basic auth check after HTTPS redirect so that users aren't prompted
# for credentials over HTTP; h/t @noahkunin.
if config.username and config.password:
app.config['BASIC_AUTH_USERNAME'] = config.username
app.config['BASIC_AUTH_PASSWORD'] = config.password
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
if config.environment == 'prod':
auth = hmacauth.HmacAuth(
digest=hashlib.sha1,
secret_key=config.hmac_secret,
signature_header='X-Signature',
headers=config.hmac_headers,
)
app.wsgi_app = hmacauth.HmacMiddleware(app.wsgi_app, auth)
app.wsgi_app = utils.ReverseProxied(app.wsgi_app)
app.wsgi_app = ProxyFix(app.wsgi_app)
def initialize_newrelic():
license_key = env.get_credential('NEW_RELIC_LICENSE_KEY')
if license_key:
import newrelic.agent
settings = newrelic.agent.global_settings()
settings.license_key = license_key
newrelic.agent.initialize()
initialize_newrelic()
if config.sentry_dsn:
Sentry(app, dsn=config.sentry_dsn)
if __name__ == '__main__':
files = ['./rev-manifest.json']
app.run(host=config.host, port=int(config.port), debug=config.debug, extra_files=files)