Skip to content

Commit dd464a8

Browse files
committed
refactor: move Nominee Info into its own directory
1 parent 80a4f93 commit dd464a8

File tree

8 files changed

+50
-45
lines changed

8 files changed

+50
-45
lines changed

src/elections/crud.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sqlalchemy
44
from sqlalchemy.ext.asyncio import AsyncSession
55

6-
from elections.tables import Election, NomineeInfo
6+
from elections.tables import Election
77

88

99
async def get_all_elections(db_session: AsyncSession) -> Sequence[Election]:
@@ -47,32 +47,3 @@ async def delete_election(db_session: AsyncSession, slug: str) -> None:
4747
.delete(Election)
4848
.where(Election.slug == slug)
4949
)
50-
51-
# ------------------------------------------------------- #
52-
53-
async def get_nominee_info(
54-
db_session: AsyncSession,
55-
computing_id: str,
56-
) -> NomineeInfo | None:
57-
return await db_session.scalar(
58-
sqlalchemy
59-
.select(NomineeInfo)
60-
.where(NomineeInfo.computing_id == computing_id)
61-
)
62-
63-
async def create_nominee_info(
64-
db_session: AsyncSession,
65-
info: NomineeInfo,
66-
):
67-
db_session.add(info)
68-
69-
async def update_nominee_info(
70-
db_session: AsyncSession,
71-
info: NomineeInfo,
72-
):
73-
await db_session.execute(
74-
sqlalchemy
75-
.update(NomineeInfo)
76-
.where(NomineeInfo.computing_id == info.computing_id)
77-
.values(info.to_update_dict())
78-
)

src/elections/urls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import database
77
import elections.crud
88
import elections.tables
9+
import nominees.crud
910
import registrations.crud
1011
from elections.models import (
1112
ElectionParams,
@@ -82,8 +83,6 @@ def _raise_if_bad_election_data(
8283
detail=f"election slug '{slug}' is too long",
8384
)
8485

85-
# election ------------------------------------------------------------- #
86-
8786
@router.get(
8887
"",
8988
description="Returns a list of all election & their status",
@@ -165,7 +164,7 @@ async def get_election(
165164
continue
166165

167166
# NOTE: if a nominee does not input their legal name, they are not considered a nominee
168-
nominee_info = await elections.crud.get_nominee_info(db_session, nomination.computing_id)
167+
nominee_info = await nominees.crud.get_nominee_info(db_session, nomination.computing_id)
169168
if nominee_info is None:
170169
continue
171170

src/load_test_db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# tables, or the current python context will not be able to find them & they won't be loaded
1313
from auth.crud import create_user_session, update_site_user
1414
from database import SQLALCHEMY_TEST_DATABASE_URL, Base, DatabaseSessionManager
15-
from elections.crud import create_election, create_nominee_info, update_election
15+
from elections.crud import create_election, update_election
1616
from elections.tables import Election, NomineeInfo
17+
from nominees.crud import create_nominee_info
1718
from officers.constants import OfficerPositionEnum
1819
from officers.crud import (
1920
create_new_officer_info,

src/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import auth.urls
1010
import database
1111
import elections.urls
12-
import nominee.urls
12+
import nominees.urls
1313
import officers.urls
1414
import permission.urls
1515
import registrations.urls
@@ -58,7 +58,7 @@
5858
app.include_router(auth.urls.router)
5959
app.include_router(elections.urls.router)
6060
app.include_router(registrations.urls.router)
61-
app.include_router(nominee.urls.router)
61+
app.include_router(nominees.urls.router)
6262
app.include_router(officers.urls.router)
6363
app.include_router(permission.urls.router)
6464

src/nominees/crud.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sqlalchemy
2+
from sqlalchemy.ext.asyncio import AsyncSession
3+
4+
from elections.tables import NomineeInfo
5+
6+
7+
async def get_nominee_info(
8+
db_session: AsyncSession,
9+
computing_id: str,
10+
) -> NomineeInfo | None:
11+
return await db_session.scalar(
12+
sqlalchemy
13+
.select(NomineeInfo)
14+
.where(NomineeInfo.computing_id == computing_id)
15+
)
16+
17+
async def create_nominee_info(
18+
db_session: AsyncSession,
19+
info: NomineeInfo,
20+
):
21+
db_session.add(info)
22+
23+
async def update_nominee_info(
24+
db_session: AsyncSession,
25+
info: NomineeInfo,
26+
):
27+
await db_session.execute(
28+
sqlalchemy
29+
.update(NomineeInfo)
30+
.where(NomineeInfo.computing_id == info.computing_id)
31+
.values(info.to_update_dict())
32+
)

src/nominee/urls.py renamed to src/nominees/urls.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from fastapi.responses import JSONResponse
33

44
import database
5-
import elections
6-
import elections.crud
5+
import nominees.crud
76
from elections.models import (
87
NomineeInfoModel,
98
NomineeInfoUpdateParams,
@@ -32,7 +31,7 @@ async def get_nominee_info(
3231
):
3332
# Putting this one behind the admin wall since it has contact information
3433
await admin_or_raise(request, db_session)
35-
nominee_info = await elections.crud.get_nominee_info(db_session, computing_id)
34+
nominee_info = await nominees.crud.get_nominee_info(db_session, computing_id)
3635
if nominee_info is None:
3736
raise HTTPException(
3837
status_code=status.HTTP_404_NOT_FOUND,
@@ -72,13 +71,13 @@ async def provide_nominee_info(
7271
if body.discord_username is not None:
7372
updated_data["discord_username"] = body.discord_username
7473

75-
existing_info = await elections.crud.get_nominee_info(db_session, computing_id)
74+
existing_info = await nominees.crud.get_nominee_info(db_session, computing_id)
7675
# if not already existing, create it
7776
if not existing_info:
7877
# unpack dictionary and expand into NomineeInfo class
7978
new_nominee_info = NomineeInfo(computing_id=computing_id, **updated_data)
8079
# create a new nominee
81-
await elections.crud.create_nominee_info(db_session, new_nominee_info)
80+
await nominees.crud.create_nominee_info(db_session, new_nominee_info)
8281
# else just update the partial data
8382
else:
8483
merged_data = {
@@ -92,11 +91,11 @@ async def provide_nominee_info(
9291
# update the dictionary with new data
9392
merged_data.update(updated_data)
9493
updated_nominee_info = NomineeInfo(**merged_data)
95-
await elections.crud.update_nominee_info(db_session, updated_nominee_info)
94+
await nominees.crud.update_nominee_info(db_session, updated_nominee_info)
9695

9796
await db_session.commit()
9897

99-
nominee_info = await elections.crud.get_nominee_info(db_session, computing_id)
98+
nominee_info = await nominees.crud.get_nominee_info(db_session, computing_id)
10099
if not nominee_info:
101100
raise HTTPException(
102101
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,

src/registrations/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import database
77
import elections.crud
8+
import nominees.crud
89
import registrations.crud
910
from elections.models import (
1011
ElectionStatusEnum,
@@ -81,7 +82,7 @@ async def register_in_election(
8182
detail=f"invalid position {body.position}"
8283
)
8384

84-
if await elections.crud.get_nominee_info(db_session, body.computing_id) is None:
85+
if await nominees.crud.get_nominee_info(db_session, body.computing_id) is None:
8586
# ensure that the user has a nominee info entry before allowing registration to occur.
8687
raise HTTPException(
8788
status_code=status.HTTP_400_BAD_REQUEST,

tests/integration/test_elections.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
from src.elections.crud import (
1111
get_all_elections,
1212
get_election,
13-
get_nominee_info,
1413
)
1514
from src.main import app
15+
from src.nominees.crud import (
16+
get_nominee_info,
17+
)
1618
from src.registrations.crud import (
1719
get_all_registrations_in_election,
1820
)

0 commit comments

Comments
 (0)