Skip to content

Commit e23cc4a

Browse files
committed
feat: add election model for better documentation
1 parent 7f07b77 commit e23cc4a

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

src/elections/crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def delete_election(db_session: AsyncSession, slug: str) -> None:
5050
# ------------------------------------------------------- #
5151

5252
# TODO: switch to only using one of application or registration
53-
async def get_all_registrations(
53+
async def get_all_registrations_of_user(
5454
db_session: AsyncSession,
5555
computing_id: str,
5656
election_slug: str

src/elections/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from enum import Enum
2+
3+
from pydantic import BaseModel
4+
5+
6+
class ElectionTypeEnum(str, Enum):
7+
GENERAL = "general_election"
8+
BY_ELECTION = "by_election"
9+
COUNCIL_REP = "council_rep_election"
10+
11+
class ElectionModel(BaseModel):
12+
slug: str
13+
name: str
14+
type: ElectionTypeEnum
15+
datetime_start_nominations: str
16+
datetime_start_voting: str
17+
datetime_end_voting: str
18+
available_positions: str
19+
survey_link: str | None
20+

src/elections/urls.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import elections
99
import elections.crud
1010
import elections.tables
11+
from elections.models import ElectionModel
1112
from elections.tables import Election, NomineeApplication, NomineeInfo, election_types
1213
from officers.constants import OfficerPosition
1314
from permission.types import ElectionOfficer, WebsiteAdmin
@@ -41,7 +42,8 @@ async def _validate_user(
4142

4243
@router.get(
4344
"/list",
44-
description="Returns a list of all elections & their status"
45+
description="Returns a list of all elections & their status",
46+
response_model=list[ElectionModel]
4547
)
4648
async def list_elections(
4749
_: Request,
@@ -68,7 +70,8 @@ async def list_elections(
6870
Retrieves the election data for an election by name.
6971
Returns private details when the time is allowed.
7072
If user is an admin or elections officer, returns computing ids for each candidate as well.
71-
"""
73+
""",
74+
response_model=ElectionModel
7275
)
7376
async def get_election(
7477
request: Request,
@@ -168,6 +171,7 @@ def _raise_if_bad_election_data(
168171
@router.post(
169172
"/{election_name:str}",
170173
description="Creates an election and places it in the database. Returns election json on success",
174+
response_model=ElectionModel
171175
)
172176
async def create_election(
173177
request: Request,
@@ -253,7 +257,8 @@ async def create_election(
253257
name produces the same slug.
254258
255259
Returns election json on success.
256-
"""
260+
""",
261+
response_model=ElectionModel
257262
)
258263
async def update_election(
259264
request: Request,
@@ -312,7 +317,8 @@ async def update_election(
312317

313318
@router.delete(
314319
"/{election_name:str}",
315-
description="Deletes an election from the database. Returns whether the election exists after deletion."
320+
description="Deletes an election from the database. Returns whether the election exists after deletion.",
321+
response_model=SuccessFailModel
316322
)
317323
async def delete_election(
318324
request: Request,
@@ -360,7 +366,7 @@ async def get_election_registrations(
360366
detail=f"election with slug {slugified_name} does not exist"
361367
)
362368

363-
registration_list = await elections.crud.get_all_registrations(db_session, computing_id, slugified_name)
369+
registration_list = await elections.crud.get_all_registrations_of_user(db_session, computing_id, slugified_name)
364370
if registration_list is None:
365371
return JSONResponse([])
366372
return JSONResponse([
@@ -416,7 +422,7 @@ async def register_in_election(
416422
status_code=status.HTTP_400_BAD_REQUEST,
417423
detail="registrations can only be made during the nomination period"
418424
)
419-
elif await elections.crud.get_all_registrations(db_session, computing_id, slugified_name):
425+
elif await elections.crud.get_all_registrations_of_user(db_session, computing_id, slugified_name):
420426
raise HTTPException(
421427
status_code=status.HTTP_400_BAD_REQUEST,
422428
detail="you are already registered in this election"
@@ -484,7 +490,7 @@ async def update_registration(
484490
detail="speeches can only be updated during the nomination period"
485491
)
486492

487-
elif not await elections.crud.get_all_registrations(db_session, ccid_of_registrant, slugified_name):
493+
elif not await elections.crud.get_all_registrations_of_user(db_session, ccid_of_registrant, slugified_name):
488494
raise HTTPException(
489495
status_code=status.HTTP_404_NOT_FOUND,
490496
detail="applicant not yet registered in this election"
@@ -533,7 +539,7 @@ async def delete_registration(
533539
status_code=status.HTTP_400_BAD_REQUEST,
534540
detail="registration can only be revoked during the nomination period"
535541
)
536-
elif not await elections.crud.get_all_registrations(db_session, computing_id, slugified_name):
542+
elif not await elections.crud.get_all_registrations_of_user(db_session, computing_id, slugified_name):
537543
raise HTTPException(
538544
status_code=status.HTTP_404_NOT_FOUND,
539545
detail="you are not yet registered in this election"

src/utils/shared_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pydantic import BaseModel
2+
3+
4+
class SuccessFailModel(BaseModel):
5+
success: bool

0 commit comments

Comments
 (0)