11"""
22Shared API endpoints
33"""
4- from datetime import datetime
4+ from datetime import datetime , date
55from json import dumps
6+ from typing import Dict , Any , Union , Tuple , List
67
78from flask import session , request
89
1819
1920
2021class POSTFreshman :
21- def __init__ (self , freshman ) :
22- self .name = freshman ['name' ].strip ()
23- self .rit_username = freshman ['rit_username' ].strip ()
24- self .onfloor = freshman ['onfloor' ].strip () == 'TRUE'
22+ def __init__ (self , freshman : Dict [ str , Any ]) -> None :
23+ self .name : str = freshman ['name' ].strip ()
24+ self .rit_username : str = freshman ['rit_username' ].strip ()
25+ self .onfloor : bool = freshman ['onfloor' ].strip () == 'TRUE'
2526
2627
2728@app .route ('/api/v1/freshmen' , methods = ['POST' ])
2829@packet_auth
29- def sync_freshman ():
30+ def sync_freshman () -> Tuple [ str , int ] :
3031 """
3132 Create or update freshmen entries from a list
3233
@@ -40,19 +41,19 @@ def sync_freshman():
4041 """
4142
4243 # Only allow evals to create new frosh
43- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
44+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
4445 if not ldap .is_evals (ldap .get_member (username )):
4546 return 'Forbidden: not Evaluations Director' , 403
4647
47- freshmen_in_post = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json )}
48+ freshmen_in_post : Dict [ str , POSTFreshman ] = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json )}
4849 sync_freshman_list (freshmen_in_post )
4950 return dumps ('Done' ), 200
5051
5152
5253@app .route ('/api/v1/packets' , methods = ['POST' ])
5354@packet_auth
5455@log_time
55- def create_packet ():
56+ def create_packet () -> Tuple [ str , int ] :
5657 """
5758 Create a new packet.
5859
@@ -69,13 +70,13 @@ def create_packet():
6970 """
7071
7172 # Only allow evals to create new packets
72- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
73+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
7374 if not ldap .is_evals (ldap .get_member (username )):
7475 return 'Forbidden: not Evaluations Director' , 403
7576
76- base_date = datetime .strptime (request .json ['start_date' ], '%m/%d/%Y' ).date ()
77+ base_date : date = datetime .strptime (request .json ['start_date' ], '%m/%d/%Y' ).date ()
7778
78- freshmen_in_post = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json ['freshmen' ])}
79+ freshmen_in_post : Dict [ str , POSTFreshman ] = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json ['freshmen' ])}
7980
8081 create_new_packets (base_date , freshmen_in_post )
8182
@@ -85,9 +86,9 @@ def create_packet():
8586@app .route ('/api/v1/sync' , methods = ['POST' ])
8687@packet_auth
8788@log_time
88- def sync_ldap ():
89+ def sync_ldap () -> Tuple [ str , int ] :
8990 # Only allow evals to sync ldap
90- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
91+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
9192 if not ldap .is_evals (ldap .get_member (username )):
9293 return 'Forbidden: not Evaluations Director' , 403
9394 sync_with_ldap ()
@@ -97,14 +98,14 @@ def sync_ldap():
9798@app .route ('/api/v1/packets/<username>' , methods = ['GET' ])
9899@packet_auth
99100@before_request
100- def get_packets_by_user (username : str , info = None ) -> dict :
101+ def get_packets_by_user (username : str , info : Dict [ str , Any ] ) -> Union [ Dict [ int , Dict [ str , Any ]], Tuple [ str , int ]] :
101102 """
102103 Return a dictionary of packets for a freshman by username, giving packet start and end date by packet id
103104 """
104105
105106 if info ['ritdn' ] != username :
106107 return 'Forbidden - not your packet' , 403
107- frosh = Freshman .by_username (username )
108+ frosh : Freshman = Freshman .by_username (username )
108109
109110 return {packet .id : {
110111 'start' : packet .start ,
@@ -115,17 +116,17 @@ def get_packets_by_user(username: str, info=None) -> dict:
115116@app .route ('/api/v1/packets/<username>/newest' , methods = ['GET' ])
116117@packet_auth
117118@before_request
118- def get_newest_packet_by_user (username : str , info = None ) -> dict :
119+ def get_newest_packet_by_user (username : str , info : Dict [ str , Any ] ) -> Union [ Dict [ int , Dict [ str , Any ]], Tuple [ str , int ]] :
119120 """
120121 Return a user's newest packet
121122 """
122123
123124 if not info ['is_upper' ] and info ['ritdn' ] != username :
124125 return 'Forbidden - not your packet' , 403
125126
126- frosh = Freshman .by_username (username )
127+ frosh : Freshman = Freshman .by_username (username )
127128
128- packet = frosh .packets [- 1 ]
129+ packet : Packet = frosh .packets [- 1 ]
129130
130131 return {
131132 packet .id : {
@@ -137,15 +138,15 @@ def get_newest_packet_by_user(username: str, info=None) -> dict:
137138 }
138139
139140
140- @app .route ('/api/v1/packet/<packet_id>' , methods = ['GET' ])
141+ @app .route ('/api/v1/packet/<int: packet_id>' , methods = ['GET' ])
141142@packet_auth
142143@before_request
143- def get_packet_by_id (packet_id : int , info = None ) -> dict :
144+ def get_packet_by_id (packet_id : int , info : Dict [ str , Any ] ) -> Union [ Dict [ str , Dict [ str , Any ]], Tuple [ str , int ]] :
144145 """
145146 Return the scores of the packet in question
146147 """
147148
148- packet = Packet .by_id (packet_id )
149+ packet : Packet = Packet .by_id (packet_id )
149150
150151 if not info ['is_upper' ] and info ['ritdn' ] != packet .freshman .rit_username :
151152 return 'Forbidden - not your packet' , 403
@@ -156,14 +157,14 @@ def get_packet_by_id(packet_id: int, info=None) -> dict:
156157 }
157158
158159
159- @app .route ('/api/v1/sign/<packet_id>/' , methods = ['POST' ])
160+ @app .route ('/api/v1/sign/<int: packet_id>/' , methods = ['POST' ])
160161@packet_auth
161162@before_request
162- def sign (packet_id , info ) :
163- packet = Packet .by_id (packet_id )
163+ def sign (packet_id : int , info : Dict [ str , Any ]) -> str :
164+ packet : Packet = Packet .by_id (packet_id )
164165
165166 if packet is not None and packet .is_open ():
166- was_100 = packet .is_100 ()
167+ was_100 : bool = packet .is_100 ()
167168 if app .config ['REALM' ] == 'csh' :
168169 # Check if the CSHer is an upperclassman and if so, sign that row
169170 for sig in filter (lambda sig : sig .member == info ['uid' ], packet .upper_signatures ):
@@ -189,8 +190,9 @@ def sign(packet_id, info):
189190@app .route ('/api/v1/subscribe/' , methods = ['POST' ])
190191@packet_auth
191192@before_request
192- def subscribe (info ) :
193+ def subscribe (info : Dict [ str , Any ]) -> str :
193194 data = request .form
195+ subscription : NotificationSubscription
194196 if app .config ['REALM' ] == 'csh' :
195197 subscription = NotificationSubscription (token = data ['token' ], member = info ['uid' ])
196198 else :
@@ -203,16 +205,16 @@ def subscribe(info):
203205@app .route ('/api/v1/report/' , methods = ['POST' ])
204206@packet_auth
205207@before_request
206- def report (info ) :
208+ def report (info : Dict [ str , Any ]) -> str :
207209 form_results = request .form
208210 send_report_mail (form_results , get_rit_name (info ['uid' ]))
209211 return 'Success: ' + get_rit_name (info ['uid' ]) + ' sent a report'
210212
211213
212- @app .route ('/api/v1/stats/packet/<packet_id>' )
214+ @app .route ('/api/v1/stats/packet/<int: packet_id>' )
213215@packet_auth
214216@before_request
215- def packet_stats (packet_id , info = None ) :
217+ def packet_stats (packet_id : int , info : Dict [ str , Any ]) -> Union [ stats . PacketStats , Tuple [ str , int ]] :
216218 if not info ['is_upper' ] and info ['ritdn' ] != Packet .by_id (packet_id ).freshman .rit_username :
217219 return 'Forbidden - not your packet' , 403
218220 return stats .packet_stats (packet_id )
@@ -221,20 +223,20 @@ def packet_stats(packet_id, info=None):
221223@app .route ('/api/v1/stats/upperclassman/<uid>' )
222224@packet_auth
223225@before_request
224- def upperclassman_stats (uid , info = None ) :
226+ def upperclassman_stats (uid : str , info : Dict [ str , Any ]) -> Union [ stats . UpperStats , Tuple [ str , int ]] :
225227 if not info ['is_upper' ]:
226228 return 'Forbidden' , 403
227229
228230 return stats .upperclassman_stats (uid )
229231
230232
231233@app .route ('/readiness' )
232- def readiness () -> tuple [str , int ]:
234+ def readiness () -> Tuple [str , int ]:
233235 """A basic healthcheck. Returns 200 to indicate flask is running"""
234236 return 'ready' , 200
235237
236238
237- def commit_sig (packet , was_100 , uid ) :
239+ def commit_sig (packet : Packet , was_100 : bool , uid : str ) -> str :
238240 packet_signed_notification (packet , uid )
239241 db .session .commit ()
240242 if not was_100 and packet .is_100 ():
0 commit comments