11import logging
22from dataclasses import dataclass
3- from datetime import date , datetime
3+ from datetime import date
44
55import sqlalchemy
66from fastapi import APIRouter , Body , HTTPException , Request
2525 tags = ["officers" ],
2626)
2727
28- # TODO: combine the following two endpoints
28+ async def has_officer_private_info_access (
29+ request : Request ,
30+ db_session : database .DBSession ,
31+ ) -> tuple [None | str , None | str , bool ]:
32+ # determine if the user has access to this private data
33+ session_id = request .cookies .get ("session_id" , None )
34+ if session_id is None :
35+ return None , None , False
36+
37+ computing_id = await auth .crud .get_computing_id (db_session , session_id )
38+ if computing_id is None :
39+ return session_id , None , False
40+
41+ has_private_access = await OfficerPrivateInfo .has_permission (db_session , computing_id )
42+ return session_id , computing_id , has_private_access
43+
2944@router .get (
3045 "/current" ,
3146 description = "Get information about all the officers. More information is given if you're authenticated & have access to private executive data." ,
@@ -35,13 +50,7 @@ async def current_officers(
3550 request : Request ,
3651 db_session : database .DBSession ,
3752):
38- # determine if the user has access to this private data
39- session_id = request .cookies .get ("session_id" , None )
40- if session_id is None :
41- has_private_access = False
42- else :
43- computing_id = await auth .crud .get_computing_id (db_session , session_id )
44- has_private_access = await OfficerPrivateInfo .has_permission (db_session , computing_id )
53+ _ , _ , has_private_access = await has_officer_private_info_access (request , db_session )
4554
4655 current_executives = await officers .crud .current_executive_team (db_session , has_private_access )
4756 json_current_executives = {
@@ -54,36 +63,26 @@ async def current_officers(
5463
5564@router .get (
5665 "/all" ,
57- description = "Information from all exec terms. If year is not included, all years will be returned. If semester is not included, all semesters that year will be returned. If semester is given, but year is not, return all years and all semesters. " ,
66+ description = "Information for all execs from all exec terms " ,
5867)
5968async def all_officers (
6069 request : Request ,
6170 db_session : database .DBSession ,
62- view_only_filled_in : bool = True ,
71+ # Officer terms for officers which have not yet started their term yet are considered private,
72+ # and may only be accessed by that officer and executives.
73+ view_not_started_officer_terms : bool = False ,
6374):
64- async def has_access (db_session : database .DBSession , request : Request ) -> bool :
65- # determine if user has access to this private data
66- session_id = request .cookies .get ("session_id" , None )
67- if session_id is None :
68- return False
69-
70- computing_id = await auth .crud .get_computing_id (db_session , session_id )
71- if computing_id is None :
72- return False
73- else :
74- has_private_access = await OfficerPrivateInfo .has_permission (db_session , computing_id )
75- is_website_admin = await WebsiteAdmin .has_permission (db_session , computing_id )
76-
77- if not view_only_filled_in and (session_id is None or not is_website_admin ):
78- raise HTTPException (status_code = 401 , detail = "must have private access to view not filled in terms" )
79-
80- return has_private_access
81-
82- has_private_access = await has_access (db_session , request )
83-
84- all_officer_data = await officers .crud .all_officer_data (db_session , has_private_access , view_only_filled_in )
85- all_officer_data = [officer_data .serializable_dict () for officer_data in all_officer_data ]
86- return JSONResponse (all_officer_data )
75+ _ , computing_id , has_private_access = await has_officer_private_info_access (request , db_session )
76+ if view_not_started_officer_terms :
77+ is_website_admin = (computing_id is not None ) and (await WebsiteAdmin .has_permission (db_session , computing_id ))
78+ if not is_website_admin :
79+ raise HTTPException (status_code = 401 , detail = "only website admins can view all executive terms that have not started yet" )
80+
81+ all_officer_data = await officers .crud .all_officer_data (db_session , has_private_access , not view_not_started_officer_terms )
82+ return JSONResponse ([
83+ officer_data .serializable_dict ()
84+ for officer_data in all_officer_data
85+ ])
8786
8887@router .get (
8988 "/terms/{computing_id}" ,
0 commit comments