Skip to content

Commit

Permalink
Move service model to the new app
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Sep 23, 2024
1 parent 3ba9d5d commit f017638
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from reservation_units.models import Equipment, EquipmentCategory, Purpose, Qualifier
from resources.enums import ResourceLocationType
from resources.models import Resource
from services.models import Service
from spaces.models import Space, Unit
from tilavarauspalvelu.enums import ServiceTypeChoices
from tilavarauspalvelu.models import Service

from .utils import weighted_choice, with_logs

Expand Down Expand Up @@ -102,7 +103,7 @@ def _create_services(*, number: int = 10) -> list[Service]:
name_fi=f"Service {i}",
name_sv=f"Service {i}",
name_en=f"Service {i}",
service_type=random.choice(Service.SERVICE_TYPES)[0],
service_type=random.choice(ServiceTypeChoices.values),
buffer_time_after=timedelta(hours=buffer_after),
buffer_time_before=timedelta(hours=buffer_before),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
)
from reservations.models import ReservationMetadataSet
from resources.models import Resource
from services.models import Service
from spaces.models import Unit
from terms_of_use.models import TermsOfUse
from tilavarauspalvelu.models import Service

from .create_seasonal_booking import _create_application_round_time_slots
from .utils import (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.1.1 on 2024-09-18 09:02

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("reservation_units", "0104_alter_reservationunit_reservation_kind"),
("tilavarauspalvelu", "0001_initial"),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AlterField(
model_name="reservationunit",
name="services",
field=models.ManyToManyField(
blank=True,
related_name="reservation_units",
to="tilavarauspalvelu.service",
),
),
],
# We just moved the model, so no need to modify foreign keys
database_operations=[],
),
]
2 changes: 1 addition & 1 deletion reservation_units/models/reservation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class ReservationUnit(SearchDocumentMixin, models.Model):
blank=True,
)
services = models.ManyToManyField(
"services.Service",
"tilavarauspalvelu.Service",
related_name="reservation_units",
blank=True,
)
Expand Down
20 changes: 0 additions & 20 deletions services/admin.py

This file was deleted.

5 changes: 0 additions & 5 deletions services/apps.py

This file was deleted.

22 changes: 22 additions & 0 deletions services/migrations/0006_delete_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.1.1 on 2024-09-18 09:02

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("reservation_units", "0105_alter_reservationunit_services"),
("services", "0005_alter_service_options"),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name="Service",
),
],
# We want to reuse the table, so don't drop it
database_operations=[],
)
]
45 changes: 0 additions & 45 deletions services/models.py

This file was deleted.

8 changes: 0 additions & 8 deletions services/translation.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/factories/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from factory import fuzzy

from services.models import Service
from tilavarauspalvelu.models import Service

from ._base import GenericDjangoModelFactory

Expand Down
5 changes: 5 additions & 0 deletions tilavarauspalvelu/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .service.admin import ServiceAdmin

__all__ = [
"ServiceAdmin",
]
16 changes: 16 additions & 0 deletions tilavarauspalvelu/admin/service/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin
from modeltranslation.admin import TranslationAdmin

from tilavarauspalvelu.models import Service


@admin.register(Service)
class ServiceAdmin(TranslationAdmin):
# List
list_display = [
"name",
"service_type",
"buffer_time_before",
"buffer_time_after",
]
list_filter = ["service_type"]
2 changes: 1 addition & 1 deletion tilavarauspalvelu/api/graphql/types/service/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from graphene_django_extensions import DjangoNode

from services.models import Service
from tilavarauspalvelu.models import Service

from .permissions import ServicePermission

Expand Down
12 changes: 12 additions & 0 deletions tilavarauspalvelu/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

__all__ = [
"ServiceTypeChoices",
]


class ServiceTypeChoices(models.TextChoices):
INTRODUCTION = "introduction", _("Introduction")
CATERING = "catering", _("Catering")
CONFIGURATION = "configuration", _("Configuration")
55 changes: 55 additions & 0 deletions tilavarauspalvelu/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 5.1.1 on 2024-09-18 09:02

from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.CreateModel(
name="Service",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
("name_fi", models.CharField(max_length=255, null=True)),
("name_en", models.CharField(max_length=255, null=True)),
("name_sv", models.CharField(max_length=255, null=True)),
(
"service_type",
models.CharField(
choices=[
("introduction", "Introduction"),
("catering", "Catering"),
("configuration", "Configuration"),
],
default="introduction",
max_length=50,
),
),
("buffer_time_before", models.DurationField(blank=True, null=True)),
("buffer_time_after", models.DurationField(blank=True, null=True)),
],
options={
"db_table": "service",
"ordering": ["pk"],
"base_manager_name": "objects",
},
),
],
# We want to reuse the table, so don't create a new one.
database_operations=[],
),
]
5 changes: 5 additions & 0 deletions tilavarauspalvelu/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .service.model import Service

__all__ = [
"Service",
]
9 changes: 9 additions & 0 deletions tilavarauspalvelu/models/service/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .model import Service


class ServiceActions:
def __init__(self, service: "Service") -> None:
self.service = service
55 changes: 55 additions & 0 deletions tilavarauspalvelu/models/service/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING

from django.db import models

from tilavarauspalvelu.enums import ServiceTypeChoices

from .queryset import ServiceQuerySet

if TYPE_CHECKING:
import datetime

from .actions import ServiceActions


__all__ = [
"Service",
]


class Service(models.Model):
name: str = models.CharField(max_length=255)
service_type: str = models.CharField(
max_length=50,
choices=ServiceTypeChoices.choices,
default=ServiceTypeChoices.INTRODUCTION,
)

buffer_time_before: datetime.timedelta | None = models.DurationField(blank=True, null=True)
buffer_time_after: datetime.timedelta | None = models.DurationField(blank=True, null=True)

# Translated field hints
name_fi: str | None
name_sv: str | None
name_en: str | None

objects = ServiceQuerySet.as_manager()

class Meta:
db_table = "service"
base_manager_name = "objects"
ordering = ["pk"]

def __str__(self) -> str:
return f"{self.name} ({self.service_type})"

@cached_property
def actions(self) -> ServiceActions:
# Import actions inline to defer loading them.
# This allows us to avoid circular imports.
from .actions import ServiceActions

return ServiceActions(self)
10 changes: 10 additions & 0 deletions tilavarauspalvelu/models/service/queryset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from django.db import models

__all__ = [
"ServiceQuerySet",
]


class ServiceQuerySet(models.QuerySet): ...
9 changes: 9 additions & 0 deletions tilavarauspalvelu/translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from modeltranslation.decorators import register
from modeltranslation.translator import TranslationOptions

from .models import Service


@register(Service)
class ServiceTranslationOptions(TranslationOptions):
fields = ["name"]

0 comments on commit f017638

Please sign in to comment.