|
1 | 1 | """ |
2 | 2 | Shared API endpoints |
3 | 3 | """ |
4 | | -from flask import request |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from json import dumps |
| 6 | + |
| 7 | +from flask import session, request |
5 | 8 |
|
6 | 9 | from packet import app, db |
7 | 10 | from packet.context_processors import get_rit_name |
8 | | -from packet.mail import send_report_mail |
| 11 | +from packet.commands import packet_start_time, packet_end_time |
| 12 | +from packet.ldap import ldap_get_eboard_role, ldap_get_active_rtps, ldap_get_3das, ldap_get_webmasters, \ |
| 13 | + ldap_get_drink_admins, ldap_get_constitutional_maintainers, ldap_is_intromember, ldap_get_active_members, \ |
| 14 | + ldap_is_on_coop, _ldap_is_member_of_group, ldap_get_member |
| 15 | +from packet.mail import send_report_mail, send_start_packet_mail |
9 | 16 | from packet.utils import before_request, packet_auth, notify_slack |
10 | | -from packet.models import Packet, MiscSignature, NotificationSubscription, Freshman |
11 | | -from packet.notifications import packet_signed_notification, packet_100_percent_notification |
| 17 | +from packet.models import Packet, MiscSignature, NotificationSubscription, Freshman, FreshSignature, UpperSignature |
| 18 | +from packet.notifications import packet_signed_notification, packet_100_percent_notification, \ |
| 19 | + packet_starting_notification, packets_starting_notification |
| 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 |
12 | 79 |
|
13 | 80 |
|
14 | 81 | @app.route('/api/v1/packets/<username>', methods=['GET']) |
|
0 commit comments