From 00ac2cbd5ddc100ce5e349b6706776662ae507ba Mon Sep 17 00:00:00 2001 From: Matti Lamppu Date: Thu, 28 Nov 2024 09:52:33 +0200 Subject: [PATCH] Add UUID fields to reservation models for future "keyless entry" - 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 --- .../test_reservation_statistics.py | 2 +- .../types/recurring_reservation/types.py | 1 + .../api/graphql/types/reservation/types.py | 1 + ...me_recurring_reservation_uuid_to_ext_id.py | 18 +++++++++++++++ .../migrations/0042_add_reservation_uuids.py | 20 +++++++++++++++++ .../migrations/0043_populate_uuid_values.py | 22 +++++++++++++++++++ .../migrations/0044_change_uuid_to_unique.py | 20 +++++++++++++++++ ...5_reservationstatistic_reservation_uuid.py | 18 +++++++++++++++ .../models/recurring_reservation/model.py | 4 ++-- tilavarauspalvelu/models/reservation/model.py | 2 ++ .../models/reservation_statistic/model.py | 4 +++- 11 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tilavarauspalvelu/migrations/0041_rename_recurring_reservation_uuid_to_ext_id.py create mode 100644 tilavarauspalvelu/migrations/0042_add_reservation_uuids.py create mode 100644 tilavarauspalvelu/migrations/0043_populate_uuid_values.py create mode 100644 tilavarauspalvelu/migrations/0044_change_uuid_to_unique.py create mode 100644 tilavarauspalvelu/migrations/0045_reservationstatistic_reservation_uuid.py diff --git a/tests/test_models/test_reservation_statistics.py b/tests/test_models/test_reservation_statistics.py index 8863ab5ee..000dc3011 100644 --- a/tests/test_models/test_reservation_statistics.py +++ b/tests/test_models/test_reservation_statistics.py @@ -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 diff --git a/tilavarauspalvelu/api/graphql/types/recurring_reservation/types.py b/tilavarauspalvelu/api/graphql/types/recurring_reservation/types.py index 15589a2a4..2c8730b72 100644 --- a/tilavarauspalvelu/api/graphql/types/recurring_reservation/types.py +++ b/tilavarauspalvelu/api/graphql/types/recurring_reservation/types.py @@ -27,6 +27,7 @@ class Meta: model = RecurringReservation fields = [ "pk", + "ext_id", "user", "age_group", "ability_group", diff --git a/tilavarauspalvelu/api/graphql/types/reservation/types.py b/tilavarauspalvelu/api/graphql/types/reservation/types.py index 835380892..ffa581a0c 100644 --- a/tilavarauspalvelu/api/graphql/types/reservation/types.py +++ b/tilavarauspalvelu/api/graphql/types/reservation/types.py @@ -116,6 +116,7 @@ class Meta: fields = [ # "pk", + "ext_id", "name", "description", "num_persons", diff --git a/tilavarauspalvelu/migrations/0041_rename_recurring_reservation_uuid_to_ext_id.py b/tilavarauspalvelu/migrations/0041_rename_recurring_reservation_uuid_to_ext_id.py new file mode 100644 index 000000000..fb1509905 --- /dev/null +++ b/tilavarauspalvelu/migrations/0041_rename_recurring_reservation_uuid_to_ext_id.py @@ -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", + ), + ] diff --git a/tilavarauspalvelu/migrations/0042_add_reservation_uuids.py b/tilavarauspalvelu/migrations/0042_add_reservation_uuids.py new file mode 100644 index 000000000..c07a3522e --- /dev/null +++ b/tilavarauspalvelu/migrations/0042_add_reservation_uuids.py @@ -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), + ), + ] diff --git a/tilavarauspalvelu/migrations/0043_populate_uuid_values.py b/tilavarauspalvelu/migrations/0043_populate_uuid_values.py new file mode 100644 index 000000000..7ac0cdce2 --- /dev/null +++ b/tilavarauspalvelu/migrations/0043_populate_uuid_values.py @@ -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)] diff --git a/tilavarauspalvelu/migrations/0044_change_uuid_to_unique.py b/tilavarauspalvelu/migrations/0044_change_uuid_to_unique.py new file mode 100644 index 000000000..0e3307963 --- /dev/null +++ b/tilavarauspalvelu/migrations/0044_change_uuid_to_unique.py @@ -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), + ), + ] diff --git a/tilavarauspalvelu/migrations/0045_reservationstatistic_reservation_uuid.py b/tilavarauspalvelu/migrations/0045_reservationstatistic_reservation_uuid.py new file mode 100644 index 000000000..a78d864d2 --- /dev/null +++ b/tilavarauspalvelu/migrations/0045_reservationstatistic_reservation_uuid.py @@ -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), + ), + ] diff --git a/tilavarauspalvelu/models/recurring_reservation/model.py b/tilavarauspalvelu/models/recurring_reservation/model.py index 25b0f26dd..7747e0b63 100644 --- a/tilavarauspalvelu/models/recurring_reservation/model.py +++ b/tilavarauspalvelu/models/recurring_reservation/model.py @@ -1,6 +1,6 @@ from __future__ import annotations -import uuid as uuid_ +import uuid from functools import cached_property from typing import TYPE_CHECKING @@ -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) diff --git a/tilavarauspalvelu/models/reservation/model.py b/tilavarauspalvelu/models/reservation/model.py index 2d3699c6e..cd2adf381 100644 --- a/tilavarauspalvelu/models/reservation/model.py +++ b/tilavarauspalvelu/models/reservation/model.py @@ -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 @@ -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="") diff --git a/tilavarauspalvelu/models/reservation_statistic/model.py b/tilavarauspalvelu/models/reservation_statistic/model.py index 0f3ace0eb..2c4531ad1 100644 --- a/tilavarauspalvelu/models/reservation_statistic/model.py +++ b/tilavarauspalvelu/models/reservation_statistic/model.py @@ -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) @@ -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