Skip to content

Commit 0a32b0d

Browse files
committed
Add Upperclassman stats route
An initial pass at the router, very messy (and written late at night)
1 parent 6206d1c commit 0a32b0d

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

packet/routes/api.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def report(info):
252252
return 'Success: ' + get_rit_name(info['uid']) + ' sent a report'
253253

254254

255-
@app.route("/api/v1/stats/packet/<packet_id>")
255+
@app.route('/api/v1/stats/packet/<packet_id>')
256256
@packet_auth
257257
def packet_stats(packet_id):
258258
packet = Packet.by_id(packet_id)
@@ -289,6 +289,47 @@ def packet_stats(packet_id):
289289
}
290290

291291

292+
def sig2dict(sig):
293+
"""
294+
A utility function for upperclassman stats.
295+
Converts an UpperSignature to a dictionary with the date and the packet.
296+
"""
297+
packet = Packet.by_id(sig.packet_id)
298+
return {
299+
'date': sig.updated.date(),
300+
'packet': {
301+
'id': packet.id,
302+
'freshman_username': packet.freshman_username,
303+
},
304+
}
305+
306+
307+
@app.route('/api/v1/stats/upperclassman/<uid>')
308+
@packet_auth
309+
def upperclassman_stats(uid):
310+
311+
sigs = UpperSignature.query.filter(
312+
UpperSignature.signed,
313+
UpperSignature.member == uid
314+
).all() + MiscSignature.query.filter(MiscSignature.member == uid).all()
315+
316+
sig_dicts = list(map(sig2dict, sigs))
317+
318+
dates = set(map(lambda sd: sd['date'], sig_dicts))
319+
320+
return {
321+
'member': uid,
322+
'signatures': {
323+
date.isoformat() : list(
324+
map(lambda sd: sd['packet'],
325+
filter(lambda sig, d=date: sig['date'] == d,
326+
sig_dicts
327+
)
328+
)
329+
) for date in dates
330+
}
331+
}
332+
292333
def commit_sig(packet, was_100, uid):
293334
packet_signed_notification(packet, uid)
294335
db.session.commit()

0 commit comments

Comments
 (0)