-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
150 lines (119 loc) · 4.21 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
# -*- coding: utf-8 -*-
from flask import Blueprint, render_template, g
from urlparse import urlparse
from titlecase import titlecase
from .inc.models import Case, Officer, Payment
from .inc.helpers import format_currency, total_for_payments
blueprint = Blueprint(
__name__,
__name__,
static_folder='assets',
static_url_path='/assets/%s' % __name__
)
"""
ROUTES
"""
@blueprint.route('/search/')
@blueprint.route('/search/index.html')
def search(init_view='cases'):
context = get_context('search')
cases = Case.objects
neighborhoods = {}
primary_causes = []
for case in cases:
try:
if (not neighborhoods.get(case.neighborhood_id, None)
and case.neighborhood and case.neighborhood.strip() != ''):
neighborhoods[case.neighborhood_id] = {
'neighborhood': case.neighborhood,
'neighborhood_id': case.neighborhood_id
}
except AttributeError:
pass
try:
primary_causes.append(case.primary_cause)
except IndexError:
pass
context.update({
'init_view': init_view,
'cases': sorted(
cases, key=lambda x: total_for_payments(x.payments, False), reverse=True),
'neighborhoods': sorted(
neighborhoods.values(), key=lambda x: x.get('neighborhood')),
'officers': Officer.objects,
'payments': Payment.objects
})
return render_template('templates/search.html', **context)
@blueprint.route('/search/cases/')
@blueprint.route('/search/cases/index.html')
def search_cases():
return search()
@blueprint.route('/search/officers/')
@blueprint.route('/search/officers/index.html')
def search_officers():
return search('officers')
@blueprint.route('/officer/<slug>/')
@blueprint.route('/officer/<slug>/index.html')
def officer(slug):
context = get_context('officer/%s' % slug)
try:
officer = filter(lambda x: x.get_slug() == slug, Officer.objects)[0]
context['officer'] = officer
context['title'] = "%s %s | Chicago Reporter" % (
titlecase(officer.first), titlecase(officer.last))
if officer.get('note', False) and officer.get('note') != 'NULL':
context['opengraph_description'] = officer.get('note')
except IndexError:
context['officer'] = False
return render_template('templates/officer.html', **context)
@blueprint.route('/case/<slug>/')
@blueprint.route('/case/<slug>/index.html')
def case(slug):
context = get_context('case/%s' % slug)
try:
case = filter(lambda x: x.get_slug() == slug, Case.objects)[0]
context['case'] = case
context['title'] = "Case %s | Chicago Reporter" % case.get('case_number')
if case.get('narrative', False) and case.get('narrative') != 'NULL':
context['opengraph_description'] = case.get('narrative')
except IndexError:
context['case'] = False
return render_template('templates/case.html', **context)
"""
Utility functions
"""
def get_context(route):
context = g.current_site.project.DEFAULT_CONTEXT.copy()
context['route'] = route
return context
def get_site_path():
if g.current_site.app.config.get('BUILD_PATH', False):
return '/settlements/'
else:
return '/'
def get_root_url():
return g.current_site.project.DEFAULT_CONTEXT.get('ROOT_URL')
@blueprint.app_context_processor
def context_processor():
"""
Add helper functions to context for all projects.
"""
return {
# Functions
'enumerate': enumerate,
'format_currency': format_currency,
'total_for_payments': total_for_payments,
# Variables
'BUILD_PATH': g.current_site.app.config.get('BUILD_PATH', None),
'GOOGLE_ANALYTICS_ID': 'UA-2350659-4',
'site_path': get_site_path(),
'title': 'Settling for Misconduct | Chicago Reporter',
'opengraph_image': (
'http://' + get_root_url() +
'/assets/cpd_settlements/images/02-IMG_9158.jpg'
),
'opengraph_description': (
'Between 2011 and 2016, the City of Chicago paid $280 million '
'in settlements in police misconduct cases.'
)
}