Skip to content

Commit 5db794d

Browse files
committed
Create sync freshman route
1 parent a2930c2 commit 5db794d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

packet/routes/api.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Shared API endpoints
33
"""
44
from datetime import datetime, timedelta
5+
from json import dumps
56

67
from flask import session, request
78

@@ -18,6 +19,65 @@
1819
packet_starting_notification, packets_starting_notification
1920

2021

22+
@app.route('/api/v1/freshmen', methods=['POST'])
23+
@packet_auth
24+
def sync_freshman():
25+
"""
26+
Create or update freshmen entries from a list
27+
28+
Body parameters: [
29+
{
30+
rit_username: string
31+
name: string
32+
onfloor: boolean
33+
}
34+
]
35+
"""
36+
37+
# Only allow evals to create new frosh
38+
username = str(session['userinfo'].get('preferred_username', ''))
39+
if not _ldap_is_member_of_group(ldap_get_member(username), 'eboard-evaluations'):
40+
return 'Forbidden: not Evaluations Director', 403
41+
42+
freshmen = request.json
43+
results = list()
44+
45+
packets = Packet.query.filter(Packet.end > datetime.now()).all()
46+
47+
for freshman in freshmen:
48+
rit_username = freshman['rit_username']
49+
name = freshman['name']
50+
onfloor = freshman['onfloor']
51+
52+
frosh = Freshman.query.filter_by(rit_username=rit_username).first()
53+
if frosh:
54+
if onfloor and not frosh.onfloor:
55+
# Add new onfloor signature
56+
for packet in packets:
57+
db.session.add(FreshSignature(packet=packet, freshman=frosh))
58+
elif not onfloor and frosh.onfloor:
59+
# Remove outdated onfloor signature
60+
for packet in packets:
61+
FreshSignature.query.filter_by(packet_id=packet.id, freshman_username=frosh.rit_username).delete()
62+
63+
frosh.name = name
64+
frosh.onfloor = onfloor
65+
66+
results.append(f"'{name} ({rit_username})' updated")
67+
else:
68+
frosh = Freshman(rit_username=rit_username, name=name, onfloor=onfloor)
69+
db.session.add(frosh)
70+
if onfloor:
71+
# Add onfloor signature
72+
for packet in packets:
73+
db.session.add(FreshSignature(packet=packet, freshman=frosh))
74+
75+
results.append(f"Freshman '{name} ({rit_username})' created")
76+
77+
db.session.commit()
78+
return dumps(results), 200
79+
80+
2181
@app.route('/api/v1/packet', methods=['POST'])
2282
@packet_auth
2383
def sync_freshman():

0 commit comments

Comments
 (0)