forked from nprapps/elections14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.py
445 lines (322 loc) · 13.8 KB
/
slides.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python
from datetime import datetime, timedelta
import json
from dateutil.parser import parse
from flask import render_template
import pytz
from app_utils import *
from render_utils import make_context
def senate_big_board():
"""
Senate big board
"""
from models import Race
races = Race.select().where(Race.office_name == 'U.S. Senate').order_by(Race.poll_closing_time, Race.state_postal)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
context['page_title'] = 'Senate'
context['page_class'] = 'senate'
context['poll_groups'] = columnize_races(races, 19)
context['bop'] = calculate_bop(races, SENATE_INITIAL_BOP)
context['not_called'] = calculate_seats_left(races)
return render_template('slides/race_results.html', **context)
def house_big_board(page):
"""
House big board
"""
from models import Race
all_races = Race.select().where(Race.office_name == 'U.S. House')
all_featured_races = Race.select().where((Race.office_name == 'U.S. House') & (Race.featured_race == True)).order_by(Race.poll_closing_time, Race.state_postal, Race.seat_number)
timestamp = get_last_updated(all_races)
context = make_context(timestamp=timestamp)
context['page_title'] = 'House'
context['current_page'] = page
context['page_class'] = 'house'
if page == 2:
featured_races = all_featured_races[HOUSE_PAGE_LIMIT:]
else:
featured_races = all_featured_races[:HOUSE_PAGE_LIMIT]
context['poll_groups'] = columnize_races(featured_races)
context['bop'] = calculate_bop(all_races, HOUSE_INITIAL_BOP)
context['not_called'] = calculate_seats_left(all_races)
context['seat_number'] = ".seat_number"
return render_template('slides/race_results.html', **context)
def house_big_board_one():
"""
First page of house results.
"""
return house_big_board(1)
def house_big_board_two():
"""
Second page of house results.
"""
return house_big_board(2)
def governor_big_board():
"""
Governor big board
"""
from models import Race
races = Race.select().where(Race.office_name == 'Governor').order_by(Race.poll_closing_time, Race.state_postal)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
context['page_title'] = 'Governors'
context['page_class'] = 'governor'
context['poll_groups'] = columnize_races(races, 17)
return render_template('slides/race_results.html', **context)
def ballot_measures_big_board():
"""
Governor big board
"""
from models import Race
races = Race.select().where((Race.office_id == 'I') & (Race.featured_race == True))\
.order_by(Race.poll_closing_time, Race.state_postal)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
context['page_title'] = 'Ballot Measures'
context['page_class'] = 'ballot-measures'
context['poll_groups'] = columnize_races(races, 9)
return render_template('slides/ballot_measure_results.html', **context)
def _format_tumblr_date(post):
# Parse GMT date from API
post_date = parse(post['date'])
# Convert to Eastern time (EDT or EST)
eastern = pytz.timezone('US/Eastern')
return post_date.astimezone(eastern).strftime('%I:%M %p EST')
def tumblr_text(data):
post = json.loads(data)
context = make_context()
context['post'] = post
context['formatted_date'] = _format_tumblr_date(post)
return render_template('slides/tumblr_text.html', **context)
def tumblr_photo(data):
post = json.loads(data)
context = make_context()
context['post'] = post
context['formatted_date'] = _format_tumblr_date(post)
image = None
for size in post['photos'][0]['alt_sizes']:
if not image or size['width'] > image['width']:
if size['width'] < 960:
image = size
context['image'] = image
return render_template('slides/tumblr_photo.html', **context)
def tumblr_quote(data):
post = json.loads(data)
context = make_context()
context['post'] = post
context['formatted_date'] = _format_tumblr_date(post)
return render_template('slides/tumblr_quote.html', **context)
def _get_recently_called(office_name):
"""
Get recently called races for a given office.
"""
from models import Race
now = datetime.utcnow()
then = now - timedelta(minutes=15)
recently_called = []
for race in Race.select():
if race.office_name != office_name:
continue
if not race.is_called():
continue
if not race.get_called_time() >= then:
continue
recently_called.append(race)
return recently_called
def recent_senate_calls():
"""
Get the most recent called Senate races
"""
from models import Race
context = make_context()
races = Race.recently_called().where(Race.office_name == 'U.S. Senate')
context['races'] = races
context['label'] = 'Senate'
return render_template('slides/recent-calls.html', **context)
def recent_governor_calls():
"""
Get the most recent called Governor races
"""
from models import Race
context = make_context()
races = Race.recently_called().where(Race.office_name == 'Governor')
context['races'] = races
context['label'] = 'Governor'
return render_template('slides/recent-calls.html', **context)
def balance_of_power():
"""
Serve up the balance of power graph
"""
from models import Race
house_races = Race.select().where(Race.office_name == 'U.S. House').order_by(Race.state_postal)
senate_races = Race.select().where(Race.office_name == 'U.S. Senate').order_by(Race.state_postal)
senate_updated = get_last_updated(senate_races)
house_updated = get_last_updated(house_races)
if senate_updated > house_updated:
last_updated = senate_updated
else:
last_updated = house_updated
context = make_context(timestamp=last_updated)
context['page_title'] = 'Balance of Power'
context['page_class'] = 'balance-of-power'
context['house_bop'] = calculate_bop(house_races, HOUSE_INITIAL_BOP)
context['senate_bop'] = calculate_bop(senate_races, SENATE_INITIAL_BOP)
context['house_not_called'] = calculate_seats_left(house_races)
context['senate_not_called'] = calculate_seats_left(senate_races)
return render_template('slides/balance-of-power.html', **context)
def house_freshmen():
"""
Ongoing list of how representatives elected in 2012 are faring
"""
from models import Race
races = Race.select().where(Race.freshmen == True)\
.order_by(Race.state_postal, Race.seat_number)
timestamp = get_last_updated(races)
context = make_context()
won = [race for race in races if race.is_called() and not race.is_runoff() and not race.party_changed()]
lost = [race for race in races if race.is_called() and not race.is_runoff() and race.party_changed()]
not_called = [race for race in races if not race.is_called() or race.is_runoff()]
context['races_won'] = columnize_card(won, 6)
context['races_lost'] = columnize_card(lost, 6)
context['races_not_called'] = columnize_card(not_called, 6)
context['races_won_count'] = len(won)
context['races_lost_count'] = len(lost)
context['races_not_called_count'] = len(not_called)
context['races_count'] = races.count()
return render_template('slides/house-freshmen.html', **context)
def incumbents_lost():
"""
Ongoing list of which incumbents lost their elections
"""
from models import Race
called_senate_races = Race.select().where(
(Race.office_name == 'U.S. Senate') &
(((Race.ap_called == True) & (Race.accept_ap_call == True)) |
(Race.npr_called == True))
).order_by(Race.state_postal, Race.seat_number)
called_house_races = Race.select().where(
(Race.office_name == 'U.S. House') &
(((Race.ap_called == True) & (Race.accept_ap_call == True)) |
(Race.npr_called == True))
).order_by(Race.state_postal, Race.seat_number)
senate_incumbents_lost = []
house_incumbents_lost = []
senate_updated = get_last_updated(called_senate_races)
house_updated = get_last_updated(called_house_races)
if senate_updated > house_updated:
last_updated = senate_updated
else:
last_updated = house_updated
context = make_context(timestamp=last_updated)
for race in called_senate_races:
if not race.is_runoff():
for candidate in race.candidates:
if candidate.incumbent and not candidate.is_winner():
senate_incumbents_lost.append(race)
for race in called_house_races:
if not race.is_runoff():
for candidate in race.candidates:
if candidate.incumbent and not candidate.is_winner():
house_incumbents_lost.append(race)
context['senate_incumbents_lost_count'] = len(senate_incumbents_lost)
context['house_incumbents_lost_count'] = len(house_incumbents_lost)
context['senate_incumbents_lost'] = columnize_card(senate_incumbents_lost, 6)
context['house_incumbents_lost'] = columnize_card(house_incumbents_lost, 6)
return render_template('slides/incumbents-lost.html', **context)
def obama_reps():
"""
Ongoing list of Incumbent Republicans In Districts Barack Obama Won In 2012
"""
from models import Race
races = Race.select().where(Race.obama_gop == True).order_by(Race.state_postal, Race.seat_number)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
won = [race for race in races if race.is_called() and not race.is_runoff() and not race.party_changed()]
lost = [race for race in races if race.is_called() and not race.is_runoff() and race.party_changed()]
not_called = [race for race in races if not race.is_called() or race.is_runoff()]
context['races_won'] = columnize_card(won)
context['races_lost'] = columnize_card(lost)
context['races_not_called'] = columnize_card(not_called)
context['races_won_count'] = len(won)
context['races_lost_count'] = len(lost)
context['races_not_called_count'] = len(not_called)
context['races_count'] = races.count()
return render_template('slides/obama-reps.html', **context)
def poll_closing():
"""
Serve up poll closing information
"""
from models import Race
# get featured house/ballot measures + all senate and governors
featured_races = Race.select().where(
(Race.featured_race == True) |
(Race.office_name == 'U.S. Senate') |
(Race.office_name == 'Governor')
).order_by(Race.poll_closing_time, Race.state_postal)
timestamp = get_last_updated(featured_races)
context = make_context(timestamp=timestamp)
poll_groups = group_races_by_closing_time(featured_races)
now = datetime.now()
for closing_time, races in poll_groups:
if now < closing_time:
nearest_closing_time = closing_time
nearest_poll_group = races
break
states_closing = []
for race in nearest_poll_group:
states_closing.append(race.state_postal)
states_closing = set(states_closing)
context['num_states_closing'] = len(states_closing)
context['closing_time'] = nearest_closing_time.strftime('%H:%M %p ET')
context['races'] = nearest_poll_group
return render_template('slides/poll-closing.html', **context)
def rematches():
"""
List of elections with candidates who have faced off before
"""
context = make_context()
return render_template('slides/rematches.html', **context)
def romney_dems():
"""
Ongoing list of Incumbent Democrats In Districts Mitt Romney Won In 2012
"""
from models import Race
races = Race.select().where(
(Race.romney_dem == True) &
(Race.office_name == 'U.S. House')
).order_by(Race.state_postal, Race.seat_number)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
won = [race for race in races if race.is_called() and not race.is_runoff() and not race.party_changed()]
lost = [race for race in races if race.is_called() and not race.is_runoff() and race.party_changed()]
not_called = [race for race in races if not race.is_called() or race.is_runoff()]
context['races_won'] = columnize_card(won)
context['races_lost'] = columnize_card(lost)
context['races_not_called'] = columnize_card(not_called)
context['races_won_count'] = len(won)
context['races_lost_count'] = len(lost)
context['races_not_called_count'] = len(not_called)
context['races_count'] = races.count()
return render_template('slides/romney-dems.html', **context)
def romney_senate_dems():
"""
Ongoing list of Democratically-held seats in states Mitt Romney Won In 2012
"""
from models import Race
races = Race.select().where(
(Race.romney_dem == True) &
(Race.office_name == 'U.S. Senate')
).order_by(Race.state_postal, Race.seat_number)
timestamp = get_last_updated(races)
context = make_context(timestamp=timestamp)
won = [race for race in races if race.is_called() and not race.is_runoff() and not race.party_changed()]
lost = [race for race in races if race.is_called() and not race.is_runoff() and race.party_changed()]
not_called = [race for race in races if not race.is_called() or race.is_runoff()]
context['races_won'] = columnize_card(won)
context['races_lost'] = columnize_card(lost)
context['races_not_called'] = columnize_card(not_called)
context['races_won_count'] = len(won)
context['races_lost_count'] = len(lost)
context['races_not_called_count'] = len(not_called)
context['races_count'] = races.count()
return render_template('slides/romney-senate-dems.html', **context)