Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/vote/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
EventDetails,
EventCreation,
EventStatusUpdateBody,
EventCloseResponse,
)
from .models import Event, Ballot
from django.shortcuts import aget_object_or_404
Expand All @@ -26,6 +27,8 @@ async def create_event(request, payload: EventCreation):
name=payload.name,
choices=payload.choices,
electoral_system=payload.electoral_system,
allow_registration=payload.allow_registration,
allow_voting=payload.allow_voting,
)
await event.asave()

Expand All @@ -48,8 +51,12 @@ async def read_event(
return event


@router.patch("/event/{event_id}/update-status", tags=["event"])
async def update_event_status(
@router.patch(
"/event/{event_id}/update",
response={200: EventCreationResponse},
tags=["event"],
)
async def update_event(
request,
event_id: str,
body: EventStatusUpdateBody,
Expand All @@ -60,17 +67,19 @@ async def update_event_status(
if token != event.host_token:
raise AuthorizationError

event.status = body.status
if body.allow_registration is not None:
event.allow_registration = body.allow_registration

if event.status == event.STATUS_CHOICES.CLOSED:
event.closed = datetime.now(tz=UTC)
else:
event.closed = None
if body.allow_voting is not None:
event.allow_voting = body.allow_voting

await event.asave()
return event


@router.post("/event/{event_id}/close", tags=["event"])
@router.post(
"/event/{event_id}/close", response={200: EventCloseResponse}, tags=["event"]
)
async def close_event(
request, event_id: str, token: uuid.UUID = Header(alias="X-API-Key")
):
Expand All @@ -80,9 +89,12 @@ async def close_event(
raise AuthorizationError

event.closed = datetime.now(tz=UTC)
event.status = event.STATUS_CHOICES.CLOSED
event.allow_registration = False
event.allow_voting = False
await event.asave()

return EventCloseResponse(closed=event.closed)


@router.post("/event/{event_id}/open", tags=["event"])
async def open_event(
Expand All @@ -94,7 +106,6 @@ async def open_event(
raise AuthorizationError

event.closed = None
event.status = event.STATUS_CHOICES.VOTING
await event.asave()


Expand Down Expand Up @@ -137,7 +148,7 @@ async def list_ballots(
raise AuthorizationError

if token != event.host_token and (
event.status != "CL" or (event.status == "CL" and event.show_results is False)
event.closed is None or (event.closed and event.show_results is False)
):
raise AuthorizationError

Expand All @@ -156,7 +167,7 @@ async def create_ballot(
if share_token != event.share_token:
raise AuthorizationError

if event.status != "RE":
if not event.allow_registration:
raise HttpError(409, "Cannot create ballot at this time")

ballot = Ballot(event=event, voter_name=voter_name)
Expand All @@ -183,7 +194,7 @@ async def submit_ballot(
Ballot.objects.prefetch_related("event"), pk=ballot_id
)

if ballot.event.status != "VO":
if ballot.event.allow_voting is False:
raise HttpError(409, "Event is not accepting ballots.")

if token != ballot.token:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.2.7 on 2026-05-18 04:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('vote', '0007_alter_event_status'),
]

operations = [
migrations.AddField(
model_name='event',
name='allow_registration',
field=models.BooleanField(default=True),
),
migrations.AddField(
model_name='event',
name='allow_voting',
field=models.BooleanField(default=False),
),
]
48 changes: 48 additions & 0 deletions src/vote/migrations/0009_migrate_event_status_to_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 5.2.7 on 2026-05-18 04:34

from django.db import migrations


def split_event_status(apps, schema_editor):
Event = apps.get_model("vote", "Event")
for event in Event.objects.all():
if event.status == "RE":
event.allow_registration = True
event.allow_voting = False
elif event.status == "VO":
event.allow_registration = False
event.allow_voting = True
elif event.status == "CL":
event.allow_registration = False
event.allow_voting = False
else:
raise ValueError(f"Unknown status: {event.status}")
event.save()


def reverse_split_event_status(apps, schema_editor):
Event = apps.get_model("vote", "Event")
for event in Event.objects.all():
if event.allow_registration and not event.allow_voting:
event.status = "RE"
elif not event.allow_registration and event.allow_voting:
event.status = "VO"
elif not event.allow_registration and not event.allow_voting:
event.status = "CL"
else:
raise ValueError(
f"Invalid combination of permissions: {event.allow_registration}, {event.allow_voting}"
)
event.save()


class Migration(migrations.Migration):
dependencies = [
("vote", "0008_event_allow_registration_event_allow_voting"),
]

operations = [
migrations.RunPython(
split_event_status, reverse_code=reverse_split_event_status
)
]
17 changes: 17 additions & 0 deletions src/vote/migrations/0010_remove_event_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.2.7 on 2026-05-18 04:41

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('vote', '0009_migrate_event_status_to_permissions'),
]

operations = [
migrations.RemoveField(
model_name='event',
name='status',
),
]
18 changes: 18 additions & 0 deletions src/vote/migrations/0011_alter_event_allow_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.2.7 on 2026-05-18 05:31

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('vote', '0010_remove_event_status'),
]

operations = [
migrations.AlterField(
model_name='event',
name='allow_registration',
field=models.BooleanField(default=False),
),
]
8 changes: 2 additions & 6 deletions src/vote/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@


class Event(models.Model):
class STATUS_CHOICES(models.TextChoices):
REGISTERING = "RE", "Registering"
VOTING = "VO", "Voting"
CLOSED = "CL", "Closed"

share_token = models.UUIDField(default=uuid.uuid4, editable=False)
host_token = models.UUIDField(default=uuid.uuid4, editable=False)
name = models.CharField()
Expand All @@ -17,7 +12,8 @@ class STATUS_CHOICES(models.TextChoices):
show_results = models.BooleanField(default=False)
closed = models.DateTimeField(null=True)
electoral_system = models.CharField(max_length=2)
status = models.CharField(max_length=2, choices=STATUS_CHOICES, default="RE")
allow_registration = models.BooleanField(default=False)
allow_voting = models.BooleanField(default=False)


class Ballot(models.Model):
Expand Down
12 changes: 10 additions & 2 deletions src/vote/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@


class EventStatusUpdateBody(Schema):
status: EventStatus
allow_registration: bool | None = None
allow_voting: bool | None = None


class EventCreation(Schema):
name: str
choices: List[str]
electoral_system: str
allow_registration: bool = False
allow_voting: bool = False


class EventDetails(EventCreation):
id: int
closed: datetime | None
status: EventStatus
allow_registration: bool
allow_voting: bool
share_token: uuid.UUID
show_results: bool


class EventCloseResponse(Schema):
closed: datetime


class EventCreationResponse(EventDetails):
host_token: uuid.UUID

Expand Down
Loading