Skip to content

Commit 45ca097

Browse files
committed
registrations can only be made during the nomination period, and check if position is active
1 parent a32f905 commit 45ca097

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/elections/urls.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ async def update_election(
251251
detail=f"election with slug {_slugify(name)} does not exist",
252252
)
253253

254+
# NOTE: If you update avaliable positions, people will still *technically* be able to update their
255+
# registrations, however they will not be returned in the results.
254256
await elections.crud.update_election(
255257
db_session,
256258
Election(
@@ -312,7 +314,7 @@ async def get_election_registrations(
312314
)
313315

314316
election_slug = _slugify(election_name)
315-
if await get_election(db_session, election_slug) is None:
317+
if await elections.crud.get_election(db_session, election_slug) is None:
316318
raise HTTPException(
317319
status_code=status.HTTP_400_BAD_REQUEST,
318320
detail=f"election with slug {election_slug} does not exist"
@@ -351,12 +353,26 @@ async def register_in_election(
351353
detail=f"invalid position {position}"
352354
)
353355

356+
current_time = datetime.now()
354357
election_slug = _slugify(election_name)
355-
if await get_election(db_session, election_slug) is None:
358+
election = await elections.crud.get_election(db_session, election_slug)
359+
if election is None:
356360
raise HTTPException(
357361
status_code=status.HTTP_400_BAD_REQUEST,
358362
detail=f"election with slug {election_slug} does not exist"
359363
)
364+
elif position not in election.avaliable_positions.split(","):
365+
# NOTE: We only restrict creating a registration for a position that doesn't exist,
366+
# not updating or deleting one
367+
raise HTTPException(
368+
status_code=status.HTTP_400_BAD_REQUEST,
369+
detail=f"{position} is not avaliable to register for in this election"
370+
)
371+
elif election.status(current_time) != elections.tables.STATUS_NOMINATIONS:
372+
raise HTTPException(
373+
status_code=status.HTTP_400_BAD_REQUEST,
374+
detail="registrations can only be made during the nomination period"
375+
)
360376
elif await elections.crud.get_all_registrations(db_session, computing_id, election_slug) is not None:
361377
raise HTTPException(
362378
status_code=status.HTTP_400_BAD_REQUEST,
@@ -396,12 +412,19 @@ async def update_registration(
396412
detail=f"invalid position {position}"
397413
)
398414

415+
current_time = datetime.now()
399416
election_slug = _slugify(election_name)
400-
if await get_election(db_session, election_slug) is None:
417+
election = await elections.crud.get_election(db_session, election_slug)
418+
if election is None:
401419
raise HTTPException(
402420
status_code=status.HTTP_400_BAD_REQUEST,
403421
detail=f"election with slug {election_slug} does not exist"
404422
)
423+
elif election.status(current_time) != elections.tables.STATUS_NOMINATIONS:
424+
raise HTTPException(
425+
status_code=status.HTTP_400_BAD_REQUEST,
426+
detail="speeches can only be updated during the nomination period"
427+
)
405428
elif await elections.crud.get_all_registrations(db_session, computing_id, election_slug) is None:
406429
raise HTTPException(
407430
status_code=status.HTTP_400_BAD_REQUEST,
@@ -437,12 +460,19 @@ async def delete_registration(
437460
detail=f"invalid position {position}"
438461
)
439462

463+
current_time = datetime.now()
440464
election_slug = _slugify(election_name)
441-
if await get_election(db_session, election_slug) is None:
465+
election = await elections.crud.get_election(db_session, election_slug)
466+
if election is None:
442467
raise HTTPException(
443468
status_code=status.HTTP_400_BAD_REQUEST,
444469
detail=f"election with slug {election_slug} does not exist"
445470
)
471+
elif election.status(current_time) != elections.tables.STATUS_NOMINATIONS:
472+
raise HTTPException(
473+
status_code=status.HTTP_400_BAD_REQUEST,
474+
detail="registration can only be revoked during the nomination period"
475+
)
446476
elif await elections.crud.get_all_registrations(db_session, computing_id, election_slug) is None:
447477
raise HTTPException(
448478
status_code=status.HTTP_400_BAD_REQUEST,

0 commit comments

Comments
 (0)