Skip to content

Commit

Permalink
Add UUID fields to reservation models for future "keyless entry"
Browse files Browse the repository at this point in the history
- Rename recurring reservation `uuid` -> `ext_id`
- Add `ext_id` field to reservation
- Add `reservation_uuid` field to statistics
- Add `ext_id` fields to graphql schema
  • Loading branch information
matti-lamppu committed Nov 28, 2024
1 parent 563e472 commit 00ac2cb
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/test_models/test_reservation_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_statistics__create__reservation_creation_creates_statistics(settings):
assert stat.purpose_name == reservation.purpose.name
assert stat.recurrence_begin_date == recurring.begin_date
assert stat.recurrence_end_date == recurring.end_date
assert stat.recurrence_uuid == str(recurring.uuid)
assert stat.recurrence_uuid == str(recurring.ext_id)
assert stat.reservation == reservation
assert stat.reservation_confirmed_at == reservation.confirmed_at
assert stat.reservation_created_at == reservation.created_at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Meta:
model = RecurringReservation
fields = [
"pk",
"ext_id",
"user",
"age_group",
"ability_group",
Expand Down
1 change: 1 addition & 0 deletions tilavarauspalvelu/api/graphql/types/reservation/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Meta:
fields = [
#
"pk",
"ext_id",
"name",
"description",
"num_persons",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-11-28 07:23
from __future__ import annotations

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0040_alter_reservationstatisticsreservationunit_unit_tprek_id"),
]

operations = [
migrations.RenameField(
model_name="recurringreservation",
old_name="uuid",
new_name="ext_id",
),
]
20 changes: 20 additions & 0 deletions tilavarauspalvelu/migrations/0042_add_reservation_uuids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.3 on 2024-11-28 07:23
from __future__ import annotations

import uuid

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0041_rename_recurring_reservation_uuid_to_ext_id"),
]

operations = [
migrations.AddField(
model_name="reservation",
name="ext_id",
field=models.UUIDField(default=uuid.uuid4, null=True),
),
]
22 changes: 22 additions & 0 deletions tilavarauspalvelu/migrations/0043_populate_uuid_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.1.3 on 2024-11-28 07:30
from __future__ import annotations

import uuid

from django.db import migrations


def gen_uuid(apps, schema_editor) -> None:
Reservation = apps.get_model("tilavarauspalvelu", "Reservation")
rows = list(Reservation.objects.all())
for row in rows:
row.ext_id = uuid.uuid4()
Reservation.objects.bulk_update(rows, ["ext_id"])


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0042_add_reservation_uuids"),
]

operations = [migrations.RunPython(gen_uuid, migrations.RunPython.noop)]
20 changes: 20 additions & 0 deletions tilavarauspalvelu/migrations/0044_change_uuid_to_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.3 on 2024-11-28 07:31
from __future__ import annotations

import uuid

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0043_populate_uuid_values"),
]

operations = [
migrations.AlterField(
model_name="reservation",
name="ext_id",
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-11-28 07:48
from __future__ import annotations

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0044_change_uuid_to_unique"),
]

operations = [
migrations.AddField(
model_name="reservationstatistic",
name="reservation_uuid",
field=models.CharField(blank=True, default="", max_length=255),
),
]
4 changes: 2 additions & 2 deletions tilavarauspalvelu/models/recurring_reservation/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

import uuid as uuid_
import uuid
from functools import cached_property
from typing import TYPE_CHECKING

Expand All @@ -27,9 +27,9 @@


class RecurringReservation(models.Model):
ext_id: uuid.UUID = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) # ID for external systems
name: str = models.CharField(max_length=255, blank=True, default="")
description: str = models.CharField(max_length=500, blank=True, default="")
uuid: uuid_.UUID = models.UUIDField(default=uuid_.uuid4, editable=False, unique=True)
created: datetime.datetime = models.DateTimeField(auto_now_add=True)

begin_date: datetime.date | None = models.DateField(null=True)
Expand Down
2 changes: 2 additions & 0 deletions tilavarauspalvelu/models/reservation/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
import uuid
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -46,6 +47,7 @@

class Reservation(SerializableMixin, models.Model):
# Basic information
ext_id: uuid.UUID = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) # ID for external systems
sku: str = models.CharField(max_length=255, blank=True, default="")
name: str = models.CharField(max_length=255, blank=True, default="")
description: str = models.CharField(max_length=255, blank=True, default="")
Expand Down
4 changes: 3 additions & 1 deletion tilavarauspalvelu/models/reservation_statistic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class ReservationStatistic(models.Model):
recurrence_begin_date: datetime.date | None = models.DateField(null=True)
recurrence_end_date: datetime.date | None = models.DateField(null=True)
recurrence_uuid: str = models.CharField(max_length=255, default="", blank=True)
reservation_uuid: str = models.CharField(max_length=255, default="", blank=True)
reservee_uuid: str = models.CharField(max_length=255, default="", blank=True)
reservee_used_ad_login: bool = models.BooleanField(default=False, blank=True)
is_applied: bool = models.BooleanField(default=False, blank=True)
Expand Down Expand Up @@ -204,7 +205,8 @@ def for_reservation(cls, reservation: Reservation, *, save: bool = True) -> Rese
statistic.purpose_name = reservation.purpose.name if reservation.purpose else ""
statistic.recurrence_begin_date = getattr(recurring_reservation, "begin_date", None)
statistic.recurrence_end_date = getattr(recurring_reservation, "end_date", None)
statistic.recurrence_uuid = getattr(recurring_reservation, "uuid", "")
statistic.recurrence_uuid = str(getattr(recurring_reservation, "ext_id", ""))
statistic.reservation_uuid = str(reservation.ext_id)
statistic.reservation = reservation
statistic.reservation_confirmed_at = reservation.confirmed_at
statistic.reservation_created_at = reservation.created_at
Expand Down

0 comments on commit 00ac2cb

Please sign in to comment.