1
1
"""
2
2
Shared API endpoints
3
3
"""
4
- from datetime import datetime
4
+ from datetime import datetime , date
5
5
from json import dumps
6
+ from typing import Dict , Any , Union , Tuple
6
7
7
8
from flask import session , request
8
9
18
19
19
20
20
21
class 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'
25
26
26
27
27
28
@app .route ('/api/v1/freshmen' , methods = ['POST' ])
28
29
@packet_auth
29
- def sync_freshman ():
30
+ def sync_freshman () -> Tuple [ str , int ] :
30
31
"""
31
32
Create or update freshmen entries from a list
32
33
@@ -40,19 +41,21 @@ def sync_freshman():
40
41
"""
41
42
42
43
# Only allow evals to create new frosh
43
- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
44
+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
44
45
if not ldap .is_evals (ldap .get_member (username )):
45
46
return 'Forbidden: not Evaluations Director' , 403
46
47
47
- freshmen_in_post = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json )}
48
+ freshmen_in_post : Dict [str , POSTFreshman ] = {
49
+ freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json )
50
+ }
48
51
sync_freshman_list (freshmen_in_post )
49
52
return dumps ('Done' ), 200
50
53
51
54
52
55
@app .route ('/api/v1/packets' , methods = ['POST' ])
53
56
@packet_auth
54
57
@log_time
55
- def create_packet ():
58
+ def create_packet () -> Tuple [ str , int ] :
56
59
"""
57
60
Create a new packet.
58
61
@@ -69,13 +72,15 @@ def create_packet():
69
72
"""
70
73
71
74
# Only allow evals to create new packets
72
- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
75
+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
73
76
if not ldap .is_evals (ldap .get_member (username )):
74
77
return 'Forbidden: not Evaluations Director' , 403
75
78
76
- base_date = datetime .strptime (request .json ['start_date' ], '%m/%d/%Y' ).date ()
79
+ base_date : date = datetime .strptime (request .json ['start_date' ], '%m/%d/%Y' ).date ()
77
80
78
- freshmen_in_post = {freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json ['freshmen' ])}
81
+ freshmen_in_post : Dict [str , POSTFreshman ] = {
82
+ freshman .rit_username : freshman for freshman in map (POSTFreshman , request .json ['freshmen' ])
83
+ }
79
84
80
85
create_new_packets (base_date , freshmen_in_post )
81
86
@@ -85,9 +90,9 @@ def create_packet():
85
90
@app .route ('/api/v1/sync' , methods = ['POST' ])
86
91
@packet_auth
87
92
@log_time
88
- def sync_ldap ():
93
+ def sync_ldap () -> Tuple [ str , int ] :
89
94
# Only allow evals to sync ldap
90
- username = str (session ['userinfo' ].get ('preferred_username' , '' ))
95
+ username : str = str (session ['userinfo' ].get ('preferred_username' , '' ))
91
96
if not ldap .is_evals (ldap .get_member (username )):
92
97
return 'Forbidden: not Evaluations Director' , 403
93
98
sync_with_ldap ()
@@ -97,14 +102,14 @@ def sync_ldap():
97
102
@app .route ('/api/v1/packets/<username>' , methods = ['GET' ])
98
103
@packet_auth
99
104
@before_request
100
- def get_packets_by_user (username : str , info = None ) -> dict :
105
+ def get_packets_by_user (username : str , info : Dict [ str , Any ] ) -> Union [ Dict [ int , Dict [ str , Any ]], Tuple [ str , int ]] :
101
106
"""
102
107
Return a dictionary of packets for a freshman by username, giving packet start and end date by packet id
103
108
"""
104
109
105
110
if info ['ritdn' ] != username :
106
111
return 'Forbidden - not your packet' , 403
107
- frosh = Freshman .by_username (username )
112
+ frosh : Freshman = Freshman .by_username (username )
108
113
109
114
return {packet .id : {
110
115
'start' : packet .start ,
@@ -115,17 +120,17 @@ def get_packets_by_user(username: str, info=None) -> dict:
115
120
@app .route ('/api/v1/packets/<username>/newest' , methods = ['GET' ])
116
121
@packet_auth
117
122
@before_request
118
- def get_newest_packet_by_user (username : str , info = None ) -> dict :
123
+ def get_newest_packet_by_user (username : str , info : Dict [ str , Any ] ) -> Union [ Dict [ int , Dict [ str , Any ]], Tuple [ str , int ]] :
119
124
"""
120
125
Return a user's newest packet
121
126
"""
122
127
123
128
if not info ['is_upper' ] and info ['ritdn' ] != username :
124
129
return 'Forbidden - not your packet' , 403
125
130
126
- frosh = Freshman .by_username (username )
131
+ frosh : Freshman = Freshman .by_username (username )
127
132
128
- packet = frosh .packets [- 1 ]
133
+ packet : Packet = frosh .packets [- 1 ]
129
134
130
135
return {
131
136
packet .id : {
@@ -137,15 +142,15 @@ def get_newest_packet_by_user(username: str, info=None) -> dict:
137
142
}
138
143
139
144
140
- @app .route ('/api/v1/packet/<packet_id>' , methods = ['GET' ])
145
+ @app .route ('/api/v1/packet/<int: packet_id>' , methods = ['GET' ])
141
146
@packet_auth
142
147
@before_request
143
- def get_packet_by_id (packet_id : int , info = None ) -> dict :
148
+ def get_packet_by_id (packet_id : int , info : Dict [ str , Any ] ) -> Union [ Dict [ str , Dict [ str , Any ]], Tuple [ str , int ]] :
144
149
"""
145
150
Return the scores of the packet in question
146
151
"""
147
152
148
- packet = Packet .by_id (packet_id )
153
+ packet : Packet = Packet .by_id (packet_id )
149
154
150
155
if not info ['is_upper' ] and info ['ritdn' ] != packet .freshman .rit_username :
151
156
return 'Forbidden - not your packet' , 403
@@ -156,14 +161,14 @@ def get_packet_by_id(packet_id: int, info=None) -> dict:
156
161
}
157
162
158
163
159
- @app .route ('/api/v1/sign/<packet_id>/' , methods = ['POST' ])
164
+ @app .route ('/api/v1/sign/<int: packet_id>/' , methods = ['POST' ])
160
165
@packet_auth
161
166
@before_request
162
- def sign (packet_id , info ) :
163
- packet = Packet .by_id (packet_id )
167
+ def sign (packet_id : int , info : Dict [ str , Any ]) -> str :
168
+ packet : Packet = Packet .by_id (packet_id )
164
169
165
170
if packet is not None and packet .is_open ():
166
- was_100 = packet .is_100 ()
171
+ was_100 : bool = packet .is_100 ()
167
172
if app .config ['REALM' ] == 'csh' :
168
173
# Check if the CSHer is an upperclassman and if so, sign that row
169
174
for sig in filter (lambda sig : sig .member == info ['uid' ], packet .upper_signatures ):
@@ -189,8 +194,9 @@ def sign(packet_id, info):
189
194
@app .route ('/api/v1/subscribe/' , methods = ['POST' ])
190
195
@packet_auth
191
196
@before_request
192
- def subscribe (info ) :
197
+ def subscribe (info : Dict [ str , Any ]) -> str :
193
198
data = request .form
199
+ subscription : NotificationSubscription
194
200
if app .config ['REALM' ] == 'csh' :
195
201
subscription = NotificationSubscription (token = data ['token' ], member = info ['uid' ])
196
202
else :
@@ -203,16 +209,16 @@ def subscribe(info):
203
209
@app .route ('/api/v1/report/' , methods = ['POST' ])
204
210
@packet_auth
205
211
@before_request
206
- def report (info ) :
212
+ def report (info : Dict [ str , Any ]) -> str :
207
213
form_results = request .form
208
214
send_report_mail (form_results , get_rit_name (info ['uid' ]))
209
215
return 'Success: ' + get_rit_name (info ['uid' ]) + ' sent a report'
210
216
211
217
212
- @app .route ('/api/v1/stats/packet/<packet_id>' )
218
+ @app .route ('/api/v1/stats/packet/<int: packet_id>' )
213
219
@packet_auth
214
220
@before_request
215
- def packet_stats (packet_id , info = None ) :
221
+ def packet_stats (packet_id : int , info : Dict [ str , Any ]) -> Union [ stats . PacketStats , Tuple [ str , int ]] :
216
222
if not info ['is_upper' ] and info ['ritdn' ] != Packet .by_id (packet_id ).freshman .rit_username :
217
223
return 'Forbidden - not your packet' , 403
218
224
return stats .packet_stats (packet_id )
@@ -221,20 +227,20 @@ def packet_stats(packet_id, info=None):
221
227
@app .route ('/api/v1/stats/upperclassman/<uid>' )
222
228
@packet_auth
223
229
@before_request
224
- def upperclassman_stats (uid , info = None ) :
230
+ def upperclassman_stats (uid : str , info : Dict [ str , Any ]) -> Union [ stats . UpperStats , Tuple [ str , int ]] :
225
231
if not info ['is_upper' ]:
226
232
return 'Forbidden' , 403
227
233
228
234
return stats .upperclassman_stats (uid )
229
235
230
236
231
237
@app .route ('/readiness' )
232
- def readiness () -> tuple [str , int ]:
238
+ def readiness () -> Tuple [str , int ]:
233
239
"""A basic healthcheck. Returns 200 to indicate flask is running"""
234
240
return 'ready' , 200
235
241
236
242
237
- def commit_sig (packet , was_100 , uid ) :
243
+ def commit_sig (packet : Packet , was_100 : bool , uid : str ) -> str :
238
244
packet_signed_notification (packet , uid )
239
245
db .session .commit ()
240
246
if not was_100 and packet .is_100 ():
0 commit comments