Skip to content

Commit 773d094

Browse files
committed
add back patch registration endpoint and uodate logic
1 parent 0b153f2 commit 773d094

2 files changed

Lines changed: 79 additions & 65 deletions

File tree

src/elections/urls.py

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import elections.tables
1111
from elections.tables import Election, NomineeApplication, NomineeInfo, election_types
1212
from officers.constants import OfficerPosition
13+
from officers.crud import get_active_officer_terms
1314
from permission.types import ElectionOfficer, WebsiteAdmin
1415
from utils.urls import is_logged_in
1516

@@ -430,55 +431,67 @@ async def register_in_election(
430431
))
431432
await db_session.commit()
432433

433-
# @router.patch(
434-
# "/registration/{election_name:str}",
435-
# description="update your speech for a specific position for an election"
436-
# )
437-
# async def update_registration(
438-
# request: Request,
439-
# db_session: database.DBSession,
440-
# election_name: str,
441-
# position: str,
442-
# speech: str | None,
443-
# ):
444-
# logged_in, _, computing_id = await is_logged_in(request, db_session)
445-
# if not logged_in:
446-
# raise HTTPException(
447-
# status_code=status.HTTP_401_UNAUTHORIZED,
448-
# detail="must be logged in to update election registration"
449-
# )
450-
# elif position not in OfficerPosition.position_list():
451-
# raise HTTPException(
452-
# status_code=status.HTTP_400_BAD_REQUEST,
453-
# detail=f"invalid position {position}"
454-
# )
455-
456-
# current_time = datetime.now()
457-
# slugified_name = _slugify(election_name)
458-
# election = await elections.crud.get_election(db_session, slugified_name)
459-
# if election is None:
460-
# raise HTTPException(
461-
# status_code=status.HTTP_404_NOT_FOUND,
462-
# detail=f"election with slug {slugified_name} does not exist"
463-
# )
464-
# elif election.status(current_time) != elections.tables.STATUS_NOMINATIONS:
465-
# raise HTTPException(
466-
# status_code=status.HTTP_400_BAD_REQUEST,
467-
# detail="speeches can only be updated during the nomination period"
468-
# )
469-
# elif not await elections.crud.get_all_registrations(db_session, computing_id, slugified_name):
470-
# raise HTTPException(
471-
# status_code=status.HTTP_404_NOT_FOUND,
472-
# detail="you are not yet registered in this election"
473-
# )
474-
475-
# await elections.crud.update_registration(db_session, NomineeApplication(
476-
# computing_id=computing_id,
477-
# nominee_election=slugified_name,
478-
# position=position,
479-
# speech=speech
480-
# ))
481-
# await db_session.commit()
434+
@router.patch(
435+
"/registration/{election_name:str}/{ccid_of_registrant}",
436+
description="update the application of a specific registrant"
437+
)
438+
async def update_registration(
439+
request: Request,
440+
db_session: database.DBSession,
441+
election_name: str,
442+
ccid_of_registrant: str,
443+
position: str,
444+
speech: str | None,
445+
):
446+
# check if logged in
447+
logged_in, _, computing_id = await is_logged_in(request, db_session)
448+
if not logged_in:
449+
raise HTTPException(
450+
status_code=status.HTTP_401_UNAUTHORIZED,
451+
detail="must be logged in to update election registration"
452+
)
453+
# Leave this for now, can remove self_updates if no longer needed.
454+
is_self_update = (computing_id == ccid_of_registrant)
455+
is_officer = await get_active_officer_terms(db_session, computing_id)
456+
# check if the computing_id is of a valid officer or the right applicant
457+
if not is_officer and not is_self_update: # returns [] if user is currently not an officer
458+
raise HTTPException(
459+
status_code=status.HTTP_401_UNAUTHORIZED,
460+
detail="only valid **current** officers or the applicant can update registrations"
461+
)
462+
463+
if position not in OfficerPosition.position_list():
464+
raise HTTPException(
465+
status_code=status.HTTP_400_BAD_REQUEST,
466+
detail=f"invalid position {position}"
467+
)
468+
469+
current_time = datetime.now()
470+
slugified_name = _slugify(election_name)
471+
election = await elections.crud.get_election(db_session, slugified_name)
472+
if election is None:
473+
raise HTTPException(
474+
status_code=status.HTTP_404_NOT_FOUND,
475+
detail=f"election with slug {slugified_name} does not exist"
476+
)
477+
elif election.status(current_time) != elections.tables.STATUS_NOMINATIONS:
478+
raise HTTPException(
479+
status_code=status.HTTP_400_BAD_REQUEST,
480+
detail="speeches can only be updated during the nomination period"
481+
)
482+
elif not await elections.crud.get_all_registrations(db_session, ccid_of_registrant, slugified_name):
483+
raise HTTPException(
484+
status_code=status.HTTP_404_NOT_FOUND,
485+
detail="applicant not yet registered in this election"
486+
)
487+
488+
await elections.crud.update_registration(db_session, NomineeApplication(
489+
computing_id=ccid_of_registrant,
490+
nominee_election=slugified_name,
491+
position=position,
492+
speech=speech
493+
))
494+
await db_session.commit()
482495

483496
@router.delete(
484497
"/registration/{election_name:str}/{position:str}",

tests/integration/test_elections.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ async def test_endpoints(client, database_setup):
149149
})
150150
assert response.status_code == 401
151151

152-
# response = await client.patch(f"/elections/registration/{election_name}", params={
153-
# "position": "president",
154-
# "speech": "I would like to run for president because I'm the best in Valorant at SFU."
155-
# })
156-
# assert response.status_code == 401
152+
response = await client.patch(f"/elections/registration/{election_name}/pkn4", params={
153+
"position": "president",
154+
"speech": "I would like to run for president because I'm the best in Valorant at SFU."
155+
})
156+
assert response.status_code == 401
157157

158158
response = await client.put("/elections/nominee/info", params={
159159
"full_name": "John Doe VI",
@@ -285,18 +285,19 @@ async def test_endpoints_admin(client, database_setup):
285285
})
286286
assert response.status_code == 200
287287

288-
# # update the registration
289-
# response = await client.patch(f"/elections/registration/{election_name}", params={
290-
# "position": "president",
291-
# "speech": "Vote for me as president"
292-
# })
293-
# assert response.status_code == 200
288+
# update the registration
289+
response = await client.patch(f"/elections/registration/{election_name}/pkn4", params={
290+
"position": "president",
291+
"speech": "Vote for me as treasurer"
292+
})
293+
assert response.status_code == 200
294+
294295
# try updating a non-registered election
295-
# response = await client.patch("/elections/registration/testElection4", params={
296-
# "position": "president",
297-
# "speech": "Vote for me as president, I am good at valorant."
298-
# })
299-
# assert response.status_code == 404
296+
response = await client.patch("/elections/registration/testElection4/pkn4", params={
297+
"position": "president",
298+
"speech": "Vote for me as president, I am good at valorant."
299+
})
300+
assert response.status_code == 404
300301

301302
# delete an election
302303
response = await client.delete("/elections/testElection4")

0 commit comments

Comments
 (0)