@@ -167,51 +167,66 @@ async def get_officer_info(
167167
168168 return JSONResponse (officer_info .serializable_dict ())
169169
170+ # TODO: move this to types?
170171@dataclass
171172class InitialOfficerInfo :
172173 computing_id : str
173174 position : str
174175 start_date : date
175176
177+ def valid_or_raise (self ):
178+ if len (self .computing_id ) > COMPUTING_ID_MAX :
179+ raise HTTPException (status_code = 400 , detail = f"computing_id={ self .computing_id } is too large" )
180+ elif self .computing_id == "" :
181+ raise HTTPException (status_code = 400 , detail = "computing_id cannot be empty" )
182+ elif self .position not in OfficerPosition .position_list ():
183+ raise HTTPException (status_code = 400 , detail = f"invalid position={ self .position } " )
184+
176185@router .post (
177186 "/term" ,
178- description = "Only the sysadmin, president, or DoA can submit this request. It will usually be the DoA. Updates the system with a new officer, and enables the user to login to the system to input their information." ,
187+ description = """
188+ Only the sysadmin, president, or DoA can submit this request. It will usually be the DoA.
189+ Updates the system with a new officer, and enables the user to login to the system to input their information.
190+ """ ,
179191)
180192async def new_officer_term (
181193 request : Request ,
182194 db_session : database .DBSession ,
183- officer_info_list : list [InitialOfficerInfo ] = Body (), # noqa: B008
195+ officer_info_list : list [InitialOfficerInfo ] = Body (), # noqa: B008
184196):
185197 """
186198 If the current computing_id is not already an officer, officer_info will be created for them.
187199 """
188200 for officer_info in officer_info_list :
189- if len (officer_info .computing_id ) > COMPUTING_ID_MAX :
190- raise HTTPException (status_code = 400 , detail = f"computing_id={ officer_info .computing_id } is too large" )
191- elif officer_info .position not in OfficerPosition .position_list ():
192- raise HTTPException (status_code = 400 , detail = f"invalid position={ officer_info .position } " )
201+ officer_info .valid_or_raise ()
193202
194- WebsiteAdmin .validate_request (db_session , request )
203+ _ , session_computing_id = logged_in_or_raise (request , db_session )
204+ WebsiteAdmin .has_permission_or_raise (db_session , session_computing_id )
195205
196206 for officer_info in officer_info_list :
197- await officers .crud .create_new_officer_info (
198- db_session ,
199- # TODO: do I need this object atm?
200- OfficerInfoUpload (
201- # TODO: use sfu api to get legal name
202- legal_name = "default name" ,
203- ).to_officer_info (officer_info .computing_id , None , None ),
204- )
205- # TODO: update create_new_officer_term to be the same as create_new_officer_info
207+ await officers .crud .create_new_officer_info (db_session , OfficerInfo (
208+ computing_id = officer_info .computing_id ,
209+ # TODO: use sfu api to get legal name from officer_info.computing_id
210+ legal_name = "default name" ,
211+ phone_number = None ,
212+
213+ discord_id = None ,
214+ discord_name = None ,
215+ discord_nickname = None ,
216+
217+ google_drive_email = None ,
218+ github_username = None ,
219+ ))
206220 await officers .crud .create_new_officer_term (db_session , OfficerTerm (
207221 computing_id = officer_info .computing_id ,
208222 position = officer_info .position ,
209223 # TODO: remove the hours & seconds (etc.) from start_date
224+ # TODO: start_date should be a Date, not a Datetime
210225 start_date = officer_info .start_date ,
211226 ))
212227
213228 await db_session .commit ()
214- return PlainTextResponse ("ok " )
229+ return PlainTextResponse ("success " )
215230
216231@router .patch (
217232 "/info/{computing_id}" ,
@@ -227,9 +242,7 @@ async def update_info(
227242 computing_id : str ,
228243 officer_info_upload : OfficerInfoUpload = Body (), # noqa: B008
229244):
230- http_exception = officer_info_upload .validate ()
231- if http_exception is not None :
232- raise http_exception
245+ officer_info_upload .valid_or_raise ()
233246
234247 session_id = request .cookies .get ("session_id" , None )
235248 if session_id is None :
@@ -320,7 +333,7 @@ async def update_term(
320333 term_id : int ,
321334 officer_term_upload : OfficerTermUpload = Body (), # noqa: B008
322335):
323- officer_term_upload .validate ()
336+ officer_term_upload .valid_or_raise ()
324337
325338 # Refactor all of these gets & raises into small functions
326339 session_id = request .cookies .get ("session_id" , None )
@@ -370,9 +383,9 @@ async def update_term(
370383 "validation_failures" : [], # none for now, but may be important later
371384 })
372385
373- @router .post (
374- "/remove /{term_id}" ,
375- description = "Remove the specified officer term. Only website admins can run this endpoint. BE CAREFUL WITH THIS!"
386+ @router .delete (
387+ "/term /{term_id}" ,
388+ description = "Remove the specified officer term. Only website admins can run this endpoint. BE CAREFUL WITH THIS!" ,
376389)
377390async def remove_officer ():
378391 # TODO: this
0 commit comments