@@ -276,38 +276,33 @@ async def update_term(
276276 officer_term_upload : OfficerTermUpload = Body (), # noqa: B008
277277):
278278 officer_term_upload .valid_or_raise ()
279-
280- # Refactor all of these gets & raises into small functions
281- session_id = request .cookies .get ("session_id" , None )
282- if session_id is None :
283- raise HTTPException (status_code = 401 , detail = "must be logged in" )
284-
285- session_computing_id = await auth .crud .get_computing_id (db_session , session_id )
286- if session_computing_id is None :
287- raise HTTPException (status_code = 401 )
279+ _ , session_computing_id = logged_in_or_raise (request , db_session )
288280
289281 old_officer_term = await officers .crud .get_officer_term_by_id (db_session , term_id )
282+ if old_officer_term .computing_id != session_computing_id :
283+ await WebsiteAdmin .has_permission_or_raise (
284+ db_session , session_computing_id ,
285+ errmsg = "must have website admin permissions to update another user"
286+ )
287+ elif utils .is_past_term (old_officer_term ):
288+ await WebsiteAdmin .has_permission_or_raise (
289+ db_session , session_computing_id ,
290+ errmsg = "only website admins can update past terms"
291+ )
290292
291- if (
292- old_officer_term .computing_id != session_computing_id
293- and not await WebsiteAdmin .has_permission (db_session , session_computing_id )
294- ):
295- # the current user can only input the info for another user if they have permissions
296- raise HTTPException (status_code = 401 , detail = "must have website admin permissions to update another user" )
297-
298- if (
299- utils .is_past_term (old_officer_term )
300- and not await WebsiteAdmin .has_permission (db_session , session_computing_id )
301- ):
302- raise HTTPException (status_code = 401 , detail = "only website admins can update past terms" )
303-
304- # NOTE: Only admins can write new versions of position, start_date, and end_date.
305293 if (
306294 officer_term_upload .position != old_officer_term .position
307295 or officer_term_upload .start_date != old_officer_term .start_date .date ()
308296 or officer_term_upload .end_date != old_officer_term .end_date .date ()
309- ) and not await WebsiteAdmin .has_permission (db_session , session_computing_id ):
310- raise HTTPException (status_code = 401 , detail = "Non-admins cannot modify position, start_date, or end_date." )
297+ ):
298+ await WebsiteAdmin .has_permission_or_raise (
299+ db_session , session_computing_id ,
300+ errmsg = "only admins can write new versions of position, start_date, and end_date"
301+ )
302+
303+ if officer_term_upload .position != old_officer_term .position :
304+ # TODO: update the end_date here
305+ pass
311306
312307 # TODO: log all important changes to a .log file
313308 success = await officers .crud .update_officer_term (
0 commit comments