|
2 | 2 | Shared API endpoints |
3 | 3 | """ |
4 | 4 | from datetime import datetime, timedelta |
| 5 | +from json import dumps |
5 | 6 |
|
6 | 7 | from flask import session, request |
7 | 8 |
|
|
18 | 19 | packet_starting_notification, packets_starting_notification |
19 | 20 |
|
20 | 21 |
|
| 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 | + |
21 | 81 | @app.route('/api/v1/packet', methods=['POST']) |
22 | 82 | @packet_auth |
23 | 83 | def sync_freshman(): |
|
0 commit comments