|
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 |
| 79 | + |
| 80 | + |
| 81 | +@app.route('/api/v1/packets', methods=['POST']) |
| 82 | +@packet_auth |
| 83 | +def create_packet(): |
| 84 | + """ |
| 85 | + Create a new packet. |
| 86 | +
|
| 87 | + Body parameters: { |
| 88 | + start_date: the start date of the packets in MM/DD/YYYY format |
| 89 | + freshmen: [ |
| 90 | + rit_username: string |
| 91 | + ] |
| 92 | + } |
| 93 | + """ |
| 94 | + |
| 95 | + # Only allow evals to create new packets |
| 96 | + username = str(session['userinfo'].get('preferred_username', '')) |
| 97 | + if not _ldap_is_member_of_group(ldap_get_member(username), 'eboard-evaluations'): |
| 98 | + return 'Forbidden: not Evaluations Director', 403 |
| 99 | + |
| 100 | + base_date = datetime.strptime(request.json['start_date'], '%m/%d/%Y').date() |
| 101 | + |
| 102 | + start = datetime.combine(base_date, packet_start_time) |
| 103 | + end = datetime.combine(base_date, packet_end_time) + timedelta(days=14) |
| 104 | + |
| 105 | + frosh = request.json['freshmen'] |
| 106 | + results = list() |
| 107 | + |
| 108 | + # Gather upperclassmen data from LDAP |
| 109 | + all_upper = list(filter( |
| 110 | + lambda member: not ldap_is_intromember(member) and not ldap_is_on_coop(member), ldap_get_active_members())) |
| 111 | + |
| 112 | + rtp = ldap_get_active_rtps() |
| 113 | + three_da = ldap_get_3das() |
| 114 | + webmaster = ldap_get_webmasters() |
| 115 | + c_m = ldap_get_constitutional_maintainers() |
| 116 | + drink = ldap_get_drink_admins() |
| 117 | + |
| 118 | + # Packet starting notifications |
| 119 | + packets_starting_notification(start) |
| 120 | + |
| 121 | + for frosh_rit_username in frosh: |
| 122 | + # Create the packet and signatures |
| 123 | + freshman = Freshman.query.filter_by(rit_username=frosh_rit_username).first() |
| 124 | + if freshman is None: |
| 125 | + results.append(f"Freshman '{frosh_rit_username}' not found") |
| 126 | + continue |
| 127 | + |
| 128 | + packet = Packet(freshman=freshman, start=start, end=end) |
| 129 | + db.session.add(packet) |
| 130 | + send_start_packet_mail(packet) |
| 131 | + packet_starting_notification(packet) |
| 132 | + |
| 133 | + for member in all_upper: |
| 134 | + sig = UpperSignature(packet=packet, member=member.uid) |
| 135 | + sig.eboard = ldap_get_eboard_role(member) |
| 136 | + sig.active_rtp = member.uid in rtp |
| 137 | + sig.three_da = member.uid in three_da |
| 138 | + sig.webmaster = member.uid in webmaster |
| 139 | + sig.c_m = member.uid in c_m |
| 140 | + sig.drink_admin = member.uid in drink |
| 141 | + db.session.add(sig) |
| 142 | + |
| 143 | + for onfloor_freshman in Freshman.query.filter_by(onfloor=True).filter(Freshman.rit_username != |
| 144 | + freshman.rit_username).all(): |
| 145 | + db.session.add(FreshSignature(packet=packet, freshman=onfloor_freshman)) |
| 146 | + |
| 147 | + results.append(f'Packet created for {frosh_rit_username}') |
| 148 | + |
| 149 | + db.session.commit() |
| 150 | + |
| 151 | + return dumps(results), 201 |
12 | 152 |
|
13 | 153 |
|
14 | 154 | @app.route('/api/v1/packets/<username>', methods=['GET'])
|
|
0 commit comments