|
10 | 10 | import elections.tables |
11 | 11 | from elections.tables import Election, NomineeApplication, NomineeInfo, election_types |
12 | 12 | from officers.constants import OfficerPosition |
| 13 | +from officers.crud import get_active_officer_terms |
13 | 14 | from permission.types import ElectionOfficer, WebsiteAdmin |
14 | 15 | from utils.urls import is_logged_in |
15 | 16 |
|
@@ -430,55 +431,67 @@ async def register_in_election( |
430 | 431 | )) |
431 | 432 | await db_session.commit() |
432 | 433 |
|
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() |
482 | 495 |
|
483 | 496 | @router.delete( |
484 | 497 | "/registration/{election_name:str}/{position:str}", |
|
0 commit comments