44from fastapi .responses import JSONResponse
55
66import database
7- import elections
87import elections .crud
98import elections .tables
9+ import registrations .crud
1010from elections .models import (
1111 ElectionParams ,
1212 ElectionResponse ,
1313 ElectionTypeEnum ,
1414 ElectionUpdateParams ,
15- NomineeInfoModel ,
16- NomineeInfoUpdateParams ,
1715)
18- from elections .tables import Election , NomineeInfo
16+ from elections .tables import Election
1917from officers .constants import COUNCIL_REP_ELECTION_POSITIONS , GENERAL_ELECTION_POSITIONS , OfficerPositionEnum
2018from permission .types import ElectionOfficer , WebsiteAdmin
2119from utils .shared_models import DetailModel , SuccessResponse
22- from utils .urls import admin_or_raise , get_current_user , slugify
20+ from utils .urls import get_current_user , slugify
2321
2422router = APIRouter (
25- prefix = "/elections " ,
26- tags = ["elections " ],
23+ prefix = "/election " ,
24+ tags = ["election " ],
2725)
2826
2927async def get_user_permissions (
@@ -34,7 +32,7 @@ async def get_user_permissions(
3432 if not session_id or not computing_id :
3533 return False , None , None
3634
37- # where valid means elections officer or website admin
35+ # where valid means election officer or website admin
3836 has_permission = await ElectionOfficer .has_permission (db_session , computing_id )
3937 if not has_permission :
4038 has_permission = await WebsiteAdmin .has_permission (db_session , computing_id )
@@ -84,14 +82,14 @@ def _raise_if_bad_election_data(
8482 detail = f"election slug '{ slug } ' is too long" ,
8583 )
8684
87- # elections ------------------------------------------------------------- #
85+ # election ------------------------------------------------------------- #
8886
8987@router .get (
9088 "" ,
91- description = "Returns a list of all elections & their status" ,
89+ description = "Returns a list of all election & their status" ,
9290 response_model = list [ElectionResponse ],
9391 responses = {
94- 404 : { "description" : "No elections found" }
92+ 404 : { "description" : "No election found" }
9593 },
9694 operation_id = "get_all_elections"
9795)
@@ -104,7 +102,7 @@ async def list_elections(
104102 if election_list is None or len (election_list ) == 0 :
105103 raise HTTPException (
106104 status_code = status .HTTP_404_NOT_FOUND ,
107- detail = "no elections found"
105+ detail = "no election found"
108106 )
109107
110108 current_time = datetime .now ()
@@ -126,7 +124,7 @@ async def list_elections(
126124 description = """
127125 Retrieves the election data for an election by name.
128126 Returns private details when the time is allowed.
129- If user is an admin or elections officer, returns computing ids for each candidate as well.
127+ If user is an admin or election officer, returns computing ids for each candidate as well.
130128 """ ,
131129 response_model = ElectionResponse ,
132130 responses = {
@@ -152,7 +150,7 @@ async def get_election(
152150 if current_time >= election .datetime_start_voting or is_valid_user :
153151
154152 election_json = election .private_details (current_time )
155- all_nominations = await elections .crud .get_all_registrations_in_election (db_session , slugified_name )
153+ all_nominations = await registrations .crud .get_all_registrations_in_election (db_session , slugified_name )
156154 if not all_nominations :
157155 raise HTTPException (
158156 status_code = status .HTTP_404_NOT_FOUND ,
@@ -364,93 +362,3 @@ async def delete_election(
364362
365363 old_election = await elections .crud .get_election (db_session , slugified_name )
366364 return JSONResponse ({"success" : old_election is None })
367-
368- # nominee info ------------------------------------------------------------- #
369-
370- @router .get (
371- "/nominee/{computing_id:str}" ,
372- description = "Nominee info is always publically tied to elections, so be careful!" ,
373- response_model = NomineeInfoModel ,
374- responses = {
375- 404 : { "description" : "nominee doesn't exist" }
376- },
377- operation_id = "get_nominee"
378- )
379- async def get_nominee_info (
380- request : Request ,
381- db_session : database .DBSession ,
382- computing_id : str
383- ):
384- # Putting this one behind the admin wall since it has contact information
385- await admin_or_raise (request , db_session )
386- nominee_info = await elections .crud .get_nominee_info (db_session , computing_id )
387- if nominee_info is None :
388- raise HTTPException (
389- status_code = status .HTTP_404_NOT_FOUND ,
390- detail = "nominee doesn't exist"
391- )
392-
393- return JSONResponse (nominee_info .serialize ())
394-
395- @router .patch (
396- "/nominee/{computing_id:str}" ,
397- description = "Will create or update nominee info. Returns an updated copy of their nominee info." ,
398- response_model = NomineeInfoModel ,
399- responses = {
400- 500 : { "description" : "Failed to retrieve updated nominee." }
401- },
402- operation_id = "update_nominee"
403- )
404- async def provide_nominee_info (
405- request : Request ,
406- db_session : database .DBSession ,
407- body : NomineeInfoUpdateParams ,
408- computing_id : str
409- ):
410- # TODO: There needs to be a lot more validation here.
411- await admin_or_raise (request , db_session )
412-
413- updated_data = {}
414- # Only update fields that were provided
415- if body .full_name is not None :
416- updated_data ["full_name" ] = body .full_name
417- if body .linked_in is not None :
418- updated_data ["linked_in" ] = body .linked_in
419- if body .instagram is not None :
420- updated_data ["instagram" ] = body .instagram
421- if body .email is not None :
422- updated_data ["email" ] = body .email
423- if body .discord_username is not None :
424- updated_data ["discord_username" ] = body .discord_username
425-
426- existing_info = await elections .crud .get_nominee_info (db_session , computing_id )
427- # if not already existing, create it
428- if not existing_info :
429- # unpack dictionary and expand into NomineeInfo class
430- new_nominee_info = NomineeInfo (computing_id = computing_id , ** updated_data )
431- # create a new nominee
432- await elections .crud .create_nominee_info (db_session , new_nominee_info )
433- # else just update the partial data
434- else :
435- merged_data = {
436- "computing_id" : computing_id ,
437- "full_name" : existing_info .full_name ,
438- "linked_in" : existing_info .linked_in ,
439- "instagram" : existing_info .instagram ,
440- "email" : existing_info .email ,
441- "discord_username" : existing_info .discord_username ,
442- }
443- # update the dictionary with new data
444- merged_data .update (updated_data )
445- updated_nominee_info = NomineeInfo (** merged_data )
446- await elections .crud .update_nominee_info (db_session , updated_nominee_info )
447-
448- await db_session .commit ()
449-
450- nominee_info = await elections .crud .get_nominee_info (db_session , computing_id )
451- if not nominee_info :
452- raise HTTPException (
453- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
454- detail = "failed to get updated nominee"
455- )
456- return JSONResponse (nominee_info .serialize ())
0 commit comments