-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
281 lines (246 loc) · 8.89 KB
/
main.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
from collections import defaultdict
from morepath import redirect
from onegov.ballot import Election
from onegov.core.security import Public
from onegov.core.utils import normalize_for_url
from onegov.election_day import ElectionDayApp
from onegov.election_day.layouts import ElectionLayout
from onegov.election_day.utils import add_cors_header
from onegov.election_day.utils import add_last_modified_header
from onegov.election_day.utils import get_last_notified
from onegov.election_day.utils import get_election_summary
from onegov.election_day.utils.election import get_candidates_results
from onegov.election_day.utils.election import get_connection_results
from onegov.election_day.utils.election.lists import get_list_results
from onegov.election_day.utils.parties import get_party_results
from sqlalchemy.orm import object_session
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.core.types import JSON_ro
from onegov.core.types import JSONObject
from onegov.core.types import RenderData
from onegov.election_day.request import ElectionDayRequest
from webob.response import Response
@ElectionDayApp.view(
model=Election,
request_method='HEAD',
permission=Public
)
def view_election_head(
self: Election,
request: 'ElectionDayRequest'
) -> None:
@request.after
def add_headers(response: 'Response') -> None:
add_cors_header(response)
add_last_modified_header(response, self.last_modified)
@ElectionDayApp.html(
model=Election,
permission=Public
)
def view_election(
self: Election,
request: 'ElectionDayRequest'
) -> 'Response':
"""" The main view. """
return redirect(ElectionLayout(self, request).main_view)
@ElectionDayApp.json(
model=Election,
name='json',
permission=Public
)
def view_election_json(
self: Election,
request: 'ElectionDayRequest'
) -> 'JSON_ro':
"""" The main view as JSON. """
last_modified = self.last_modified
assert last_modified is not None
@request.after
def add_headers(response: 'Response') -> None:
add_cors_header(response)
add_last_modified_header(response, last_modified)
embed = defaultdict(list)
charts: 'JSONObject' = {}
media: 'JSONObject' = {'charts': charts}
layout = ElectionLayout(self, request)
layout.last_modified = last_modified
if layout.pdf_path:
media['pdf'] = request.link(self, 'pdf')
for tab in (
'candidates',
'candidate-by-entity',
'candidate-by-district',
'lists',
'list-by-entity',
'list-by-district',
'connections',
'lists-panachage',
'party-strengths',
'parties-panachage',
):
layout = ElectionLayout(self, request, tab=tab)
layout.last_modified = last_modified
if layout.visible:
embed[tab].append(request.link(self, f'{tab}-chart'))
if layout.svg_path:
charts[tab] = request.link(self, f'{tab}-svg')
for tab in ElectionLayout.tabs_with_embedded_tables:
layout = ElectionLayout(self, request, tab=tab)
if layout.visible and (table_link := layout.table_link()):
embed[tab].append(table_link)
years, parties = get_party_results(self, json_serializable=True)
data: 'JSONObject' = {
'completed': self.completed,
'date': self.date.isoformat(),
'domain': self.domain,
'last_modified': last_modified.isoformat(),
'mandates': {
'allocated': self.allocated_mandates or 0,
'total': self.number_of_mandates or 0,
},
'progress': {
'counted': self.progress[0] or 0,
'total': self.progress[1] or 0
},
'related_link': self.related_link,
'title': self.title_translations, # type:ignore[dict-item]
'type': 'election',
'statistics': {
'total': {
'eligible_voters': self.eligible_voters,
'received_ballots': self.received_ballots,
'accounted_ballots': self.accounted_ballots,
'blank_ballots': self.blank_ballots,
'invalid_ballots': self.invalid_ballots,
'accounted_votes': self.accounted_votes,
'turnout': self.turnout,
},
'entitites': [
{
'eligible_voters': entity.eligible_voters,
'received_ballots': entity.received_ballots,
'accounted_ballots': entity.accounted_ballots,
'blank_ballots': entity.blank_ballots,
'invalid_ballots': entity.invalid_ballots,
'accounted_votes': entity.accounted_votes,
'turnout': entity.turnout,
'name': entity.name if entity.entity_id else 'Expats',
'district': entity.district if entity.entity_id else '',
'id': entity.entity_id,
} for entity in self.results
],
},
'election_type': self.type,
'url': request.link(self),
'embed': embed, # type:ignore[dict-item]
'media': media,
'data': {
'json': request.link(self, 'data-json'),
'csv': request.link(self, 'data-csv'),
}
}
session = object_session(self)
if self.type == 'majorz':
if self.majority_type == 'absolute':
data['absolute_majority'] = self.absolute_majority
data['candidates'] = [
{
'family_name': candidate.family_name,
'first_name': candidate.first_name,
'elected': candidate.elected,
'party': candidate.party,
'votes': candidate.votes,
} for candidate in get_candidates_results(self, session)
]
if self.type == 'proporz':
data['candidates'] = [
{
'family_name': candidate.family_name,
'first_name': candidate.first_name,
'elected': candidate.elected,
'party': candidate.party,
'votes': candidate.votes,
'list_name': candidate.list_name,
'list_list_id': candidate.list_id
} for candidate in get_candidates_results(self, session)
]
data['lists'] = [
{
'name': item[0],
'votes': item[1],
'id': item[2],
} for item in get_list_results(self)
]
data['list_connections'] = [
{
'id': connection[0],
'votes': connection[1],
'lists': [
{
'name': item[0],
'votes': item[1],
'id': item[2],
} for item in connection[2]
],
'subconnections': [
{
'id': subconnection[0],
'votes': subconnection[1],
'lists': [
{
'name': item[0],
'votes': item[1],
'id': item[2],
} for item in subconnection[2]
],
} for subconnection in connection[3]
],
} for connection in get_connection_results(self, session)
]
data['parties'] = parties
return data
@ElectionDayApp.json(
model=Election,
name='summary',
permission=Public
)
def view_election_summary(
self: Election,
request: 'ElectionDayRequest'
) -> 'JSON_ro':
""" View the summary of the election as JSON. """
@request.after
def add_headers(response: 'Response') -> None:
add_cors_header(response)
add_last_modified_header(response, self.last_modified)
return get_election_summary(self, request)
@ElectionDayApp.json(
model=Election,
name='last-notified',
permission=Public
)
def view_election_last_notified(
self: Election,
request: 'ElectionDayRequest'
) -> 'JSON_ro':
""" View the timestamp of the last notification. """
@request.after
def add_headers(response: 'Response') -> None:
add_cors_header(response)
add_last_modified_header(response, self.last_modified)
# FIXME: This is another issue due to a dynamic backref that only exists
# on Election if we use election_day alongside ballot, we should
# consider refactoring this.
return {'last-notified': get_last_notified(self)} # type:ignore[arg-type]
@ElectionDayApp.pdf_file(model=Election, name='pdf')
def view_election_pdf(
self: Election,
request: 'ElectionDayRequest'
) -> 'RenderData':
""" View the generated PDF. """
layout = ElectionLayout(self, request)
return {
'path': layout.pdf_path,
'name': normalize_for_url(self.title or '')
}