/
ballotadmin.py
505 lines (407 loc) · 15.3 KB
/
ballotadmin.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
from datetime import datetime, timedelta
import csv
from io import BytesIO, StringIO
import json
import re
import decimal
from bottle import *
from sqlalchemy import func
from sqlalchemy.orm import joinedload, joinedload_all, load_only
from sqlalchemy.orm.strategy_options import Load
from utils import needs_auth, lookup_ldap, add_structure, url_for
import database.orm as m
def add_routes(app):
@app.route('/<ballot_id:int>/edit-prices', method=['POST', 'GET'])
@needs_auth('admin')
def show_ballot_price_edit(ballot_id, db):
ballot = db.query(m.BallotSeason).filter(m.BallotSeason.year == ballot_id).one()
# db.make_transient(ballot)
bands = db.query(m.RoomBand).all()
modifiers = db.query(m.RoomBandModifier).all()
if request.method == 'POST':
postdata = add_structure(request.forms)
band_prices = ballot.band_prices
modifier_prices = ballot.modifier_prices
def do_update():
for id, obj in postdata['bands'].items():
try:
rent = decimal.Decimal(obj['rent'])
except decimal.DecimalException:
rent = None
band = db.query(m.RoomBand).get(id)
if band:
price = next((p for p in band_prices if p.band == band), None)
if rent is not None:
if price:
price.rent = rent
else:
m.RoomBandPrice(band=band, season=ballot, rent=rent)
elif price:
band_prices.remove(price)
for id, obj in postdata['modifiers'].items():
try:
rent = decimal.Decimal(obj['discount'])
except (decimal.DecimalException, TypeError) as e:
rent = None
modifier = db.query(m.RoomBandModifier).get(id)
if modifier:
price = next((p for p in modifier_prices if p.modifier == modifier), None)
if rent is not None:
if price:
price.rent = rent
else:
m.RoomBandModifierPrice(modifier=modifier, season=ballot, discount=rent)
elif price:
modifier_prices.remove(price)
do_update()
return redirect(url_for(ballot))
else:
return template('ballot-edit-prices', ballot_season=ballot, bands=bands, modifiers=modifiers)
@app.route('/<ballot_id:int>/band-assignments')
@needs_auth('admin')
def show_ballot_band_edit(ballot_id, db):
opts = (
joinedload(m.BallotSeason.room_listings)
.joinedload(m.RoomListing.room)
.load_only(m.Room.id, m.Room.name, m.Room.parent_id),
joinedload(m.BallotSeason.room_listings)
.joinedload(m.RoomListing.room)
.joinedload(m.Room.parent)
)
ballot = db.query(m.BallotSeason).options(*opts).filter(m.BallotSeason.year == ballot_id).one()
# do some preloading
prev = db.query(m.BallotSeason).options(*opts).filter(m.BallotSeason.year == ballot.previous.year).one()
places = db.query(m.Place).all()
bands = db.query(m.RoomBand).all()
modifiers = db.query(m.RoomBandModifier).all()
return template('ballot-band-assignments', ballot_season=ballot, bands=bands, modifiers=modifiers)
@app.route('/<ballot_id:int>/band-assignments/edit')
@needs_auth('admin')
def show_ballot_band_edit(ballot_id, db):
ballot = db.query(m.BallotSeason).options(
joinedload(m.BallotSeason.room_listings)
.joinedload(m.RoomListing.room)
.load_only(m.Room.id, m.Room.name, m.Room.parent_id),
joinedload(m.BallotSeason.room_listings)
.joinedload(m.RoomListing.room)
.joinedload(m.Room.parent)
).filter(m.BallotSeason.year == ballot_id).one()
db.query(m.Place).all()
bands = db.query(m.RoomBand).all()
modifiers = db.query(m.RoomBandModifier).all()
return template('ballot-band-assignments-edit', ballot_season=ballot, bands=bands, modifiers=modifiers)
@app.post('/<ballot_id:int>/band-assignments/edit')
@needs_auth('admin')
def save_ballot_band_edit(ballot_id, db):
ballot = db.query(m.BallotSeason).filter(m.BallotSeason.year == ballot_id).one()
postdata = add_structure(request.forms)
if postdata['reset'] == '':
rooms = db.query(m.Room).options(
load_only(m.Room.id),
joinedload(m.Room.listing_for)
)
for r in rooms:
list_new = r.listing_for.get(ballot)
list_old = _first_listing_before(r, ballot)
if list_new and list_old:
list_new.band = list_old.band
list_new.modifiers = list_old.modifiers
else:
listings = ballot.room_listings
for id, obj in postdata['listings'].items():
listing = db.query(m.RoomListing).get(id)
if not listing or listing.ballot_season != ballot:
raise HTTPError(400, 'Invalid listing id')
if obj['band'] != '':
band_id = int(obj['band'])
band = db.query(m.RoomBand).get(band_id)
if not band:
raise HTTPError(400, 'Invalid band id')
listing.band = band
else:
listing.band = None
modifier_ids = map(int, obj['modifiers'])
modifiers = {db.query(m.RoomBandModifier).get(m_id) for m_id in modifier_ids}
if not all(modifiers):
raise HTTPError(400, 'Invalid modifier id')
listing.modifiers = modifiers
db.commit()
redirect(url_for(ballot))
@app.route('/<ballot_id:int>/add-event')
@needs_auth('admin')
def show_ballot_event_add(ballot_id, db):
ballot = db.query(m.BallotSeason).options(
joinedload(m.BallotSeason.events)
).filter(m.BallotSeason.year == ballot_id).one()
event_types = (db
.query(m.BallotType, m.BallotType.events.any(m.BallotEvent.season == ballot))
.outerjoin(m.BallotType.events)
)
return template('ballot-add-event', ballot_season=ballot, event_types=event_types)
@app.post('/<ballot_id:int>/add-event')
@needs_auth('admin')
def process_ballot_event_add(ballot_id, db):
ballot = db.query(m.BallotSeason).filter(m.BallotSeason.year == ballot_id).one()
event_type = db.query(m.BallotType).filter(m.BallotType.id == int(request.forms.type)).one()
e = m.BallotEvent(
season=ballot,
type=event_type,
opens_at=datetime.strptime(request.forms.opens_at, "%Y-%m-%d").date(),
closes_at=datetime.strptime(request.forms.closes_at, "%Y-%m-%d").date()
)
db.add(e)
return redirect(app.get_url('show-ballot', ballot_id=ballot_id))
@app.route('/<ballot_id:int>/<ballot_type_name>/edit', name="edit-ballot-event")
@needs_auth('admin')
def show_ballot_control_panel(ballot_id, ballot_type_name, db):
if ballot_type_name.lower() != ballot_type_name:
raise redirect(app.get_url(
'edit-ballot-event',
ballot_id=ballot_id, ballot_type_name=ballot_type_name.lower()
))
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_event = (db
.query(m.BallotEvent)
.join(m.BallotSeason)
.filter(m.BallotEvent.type == ballot_type)
.filter(m.BallotSeason.year == ballot_id)
.options(
Load(m.BallotEvent)
.joinedload(m.BallotEvent.season)
.joinedload(m.BallotSeason.room_listings)
.joinedload(m.RoomListing.audience_types)
)
).one()
return template('ballot-event-edit', ballot_event=ballot_event)
@app.post('/<ballot_id:int>/<ballot_type_name>/edit', name="edit-ballot-event")
@needs_auth('admin')
def save_ballot_times(ballot_id, ballot_type_name, db):
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_event = (db
.query(m.BallotEvent)
.join(m.BallotSeason)
.filter(m.BallotEvent.type == ballot_type)
.filter(m.BallotSeason.year == ballot_id)
).one()
ballot_event.opens_at = datetime.strptime(request.forms.opens_at, "%Y-%m-%d").date()
ballot_event.closes_at = datetime.strptime(request.forms.closes_at, "%Y-%m-%d").date()
return template('ballot-event-edit', ballot_event=ballot_event)
@app.route('/<ballot_id:int>/<ballot_type_name>/edit-rooms')
@needs_auth('admin')
def show_ballot_room_editor(ballot_id, ballot_type_name, db):
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_eventsq = (db
.query(m.BallotEvent)
.join(m.BallotSeason)
.filter(m.BallotEvent.type == ballot_type)
.filter(m.BallotSeason.year <= ballot_id)
.order_by(m.BallotSeason.year.desc())
.limit(2)
)
ballot_events = ballot_eventsq.all()
if ballot_events[0].season.year != ballot_id:
raise HTTPError(404, "No {} ballot for the {} season {} {}".format(ballot_type.name, ballot_id, ballot_eventsq, ballot_events))
else:
ballot = ballot_events[0]
if len(ballot_events) == 2:
last_ballot = ballot_events[1]
else:
last_ballot = None
root = db.query(m.Place).options(
joinedload_all('children.rooms.listing_for'),
joinedload_all('children.children.rooms.listing_for'),
joinedload_all('children.children.children.rooms.listing_for'),
joinedload_all('children.children.children.children.rooms.listing_for'),
).filter(m.Place.parent == None).one()
return template('ballot-event-edit-rooms',
ballot_event=ballot,
last_ballot_event=last_ballot,
root=root)
@app.post('/<ballot_id:int>/<ballot_type_name>/edit-rooms')
@needs_auth('admin')
def save_ballot_rooms(ballot_id, ballot_type_name, db):
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_season = db.query(m.BallotSeason).filter(m.BallotSeason.year == ballot_id).one()
# collect all the checked rooms from the form
room_ids = set()
for name, value in request.forms.items():
match = re.match(r'rooms\[(\d+)\]', name)
if match and value == 'on':
room_ids.add(int(match.group(1)))
# for existing listings, update the audiences
not_visible = set()
for l in ballot_season.room_listings:
if l.room_id in room_ids:
l.audience_types |= {ballot_type}
room_ids.remove(l.room_id)
else:
l.audience_types -= {ballot_type}
if not l.audience_types and not l.occupancies:
not_visible.add(l)
# now find rooms with no listing
for i in room_ids:
r = db.query(m.Room).get(i)
if r:
r.listing_for[ballot_season] = m.RoomListing(
room=r,
ballot_season=ballot_season,
audience_types={ballot_type}
)
return redirect(request.url)
@app.route('/<ballot_id:int>/<ballot_type_name>/edit-slots', method=('GET', 'POST'))
@needs_auth('admin')
def edit_ballot_slots(ballot_id, ballot_type_name, db):
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_event = (db
.query(m.BallotEvent)
.join(m.BallotSeason)
.filter(m.BallotEvent.type == ballot_type)
.filter(m.BallotSeason.year == ballot_id)
).one()
step2 = None
if request.method == "POST" and request.files.slot_csv:
with StringIO(request.files.slot_csv.file.read().decode('utf8'), newline=None) as f:
raw_data, parse_errors = parse_csv(f)
data, data_errors = process_slot_tuples(db, raw_data)
# check for times which changed
for u, ts in data.items():
s = u.slot_for.get(ballot_event)
if s and s.choice and s.time != ts:
data[u] = s.time
data_errors += [('slot-used-move', s, ts)]
# check for deleted items
for s in ballot_event.slots:
if s.choice and s.person not in data:
data[s.person] = s.time
data_errors += [('slot-used-delete', s)]
step2 = data, data_errors + parse_errors
elif request.method == "POST" and request.forms.slot_json:
raw_data = parse_json(request.forms.slot_json)
data, data_errors = process_slot_tuples(db, raw_data)
if data_errors:
step2 = data, data_errors
else:
ballot_event.slots[:] = [
m.BallotSlot(person=person, time=ts, event=ballot_event)
for person, ts in data.items()
]
db.commit()
raise redirect(app.get_url('show-ballot', ballot_id=ballot_id))
return template('ballot-event-edit-slots',
ballot_event=ballot_event,
step2=step2
)
@app.route('/<ballot_id:int>/<ballot_type_name>/slots.csv')
@needs_auth('admin')
def csv_ballot_slots(ballot_id, ballot_type_name, db):
ballot_type = db.query(m.BallotType).filter(func.lower(m.BallotType.name) == ballot_type_name.lower()).one()
ballot_event = (db
.query(m.BallotEvent)
.join(m.BallotSeason)
.filter(m.BallotEvent.type == ballot_type)
.filter(m.BallotSeason.year == ballot_id)
).one()
sfile = BytesIO()
o = csv.writer(sfile)
o.writerow(["date", "time", "crsid", "name (ignored)"])
last_date = None
for s in sorted(ballot_event.slots, key=lambda s: s.time):
if s.time.date() != last_date:
o.writerow([str(s.time.date()) + "@", s.time.time(), s.person.crsid + "@", s.person.name.encode('utf8')])
last_date = s.time.date()
else:
o.writerow(["", s.time.time(), s.person.crsid + "@", s.person.name.encode('utf8')])
if not ballot_event.slots:
n = datetime.now().replace(second=0, microsecond=0)
o.writerow([str(n.date()) + "@", n.time(), "crsid123@", 'example student (delete!)'])
response.content_type = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename="slots-{}-{}.csv"'.format(ballot_id, ballot_type.name)
return sfile.getvalue()
def _first_listing_before(room, ballot):
while True:
ballot = ballot.previous
if not ballot:
break
list_old = room.listing_for.get(ballot)
if list_old:
return list_old
def parse_json(j):
return [
(datetime.strptime(date, "%Y-%m-%dT%H:%M:%S"), crsid)
for date, crsid in json.loads(j)
]
def parse_csv(f):
"""
Read in a sparse CSV, and produce tuples of (datetime ts, str crsid)
Also returns a list of parse errors
"""
reader = csv.reader(f)
errors = []
data = []
headers = next(reader)
if headers[:3] != ["date", "time", "crsid"]:
errors += [('bad-header',)]
return [], errors
last_date = None
last_time = None
for l in reader:
if len(l) < 3:
errors += [('bad row {}'.format(l))]
continue
date, time, crsid = l[:3]
# either parse the date, or reuse the last one
if date:
try:
date = datetime.strptime(date, "%Y-%m-%d@")
except ValueError:
errors += [('bad-date', date)]
continue
last_date = date
elif last_date:
date = last_date
else:
errors += [('no-date',)]
continue
# either parse the time, or reuse the last one + 3 minutes
if time:
try:
time = datetime.strptime(time, "%H:%M:%S")
except ValueError:
errors += [('bad-time', time)]
continue
last_time = time
elif last_time:
time = last_time + timedelta(minutes=3)
last_time = time
else:
errors += [('no-time',)]
continue
# save the (ts, crsid) tuple
data += [(
datetime.combine(date.date(), time.time()),
crsid.rstrip('@')
)]
return data, errors
def process_slot_tuples(db, data):
errors = []
# get all crsids
crsids = [crsid for _, crsid in data]
# find existing users attached to them
db_users = [db.query(m.Person).get(c) for c in crsids]
users = {u.crsid: u for u in db_users if u}
new_users = set(crsids) - set(users.keys())
# lookup the rest
lookup = lookup_ldap(new_users)
for crsid in new_users:
d = lookup.get(crsid)
if d:
name = d.get('visibleName')
users[crsid] = m.Person(crsid=crsid, name=name)
else:
errors += [('bad-crsid', crsid)]
data = {
users[crsid]: ts
for ts, crsid in data
if crsid in users
}
return data, errors