Skip to content

Commit

Permalink
Add "has_reservations" field to application section model
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Dec 16, 2024
1 parent b9fef96 commit 5392709
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tilavarauspalvelu/api/graphql/types/application_section/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from django.db import models
from graphene_django_extensions import DjangoNode
from lookup_property import L
from query_optimizer import AnnotatedField
from query_optimizer import AnnotatedField, ManuallyOptimizedField
from query_optimizer.optimizer import QueryOptimizer

from tilavarauspalvelu.enums import ApplicationSectionStatusChoice, UserRoleChoice
from tilavarauspalvelu.models import Application, ApplicationSection, User
from tilavarauspalvelu.models import Application, ApplicationSection, Reservation, User

from .filtersets import ApplicationSectionFilterSet
from .permissions import ApplicationSectionPermission
Expand All @@ -25,6 +25,8 @@ class ApplicationSectionNode(DjangoNode):
status = AnnotatedField(graphene.Enum.from_enum(ApplicationSectionStatusChoice), expression=L("status"))
allocations = AnnotatedField(graphene.Int, expression=L("allocations"))

has_reservations = ManuallyOptimizedField(graphene.Boolean, required=True)

class Meta:
model = ApplicationSection
fields = [
Expand Down Expand Up @@ -101,3 +103,15 @@ def pre_optimization_hook(cls, queryset: ApplicationSectionQuerySet, optimizer:
@classmethod
def _add_units_for_permissions(cls, queryset: ApplicationQuerySet, *args: Any) -> models.QuerySet:
return queryset.with_permissions()

@staticmethod
def optimize_has_reservations(queryset: models.QuerySet, optimizer: QueryOptimizer) -> models.QuerySet:
optimizer.annotations["has_reservations"] = models.Exists(
Reservation.objects.filter(
recurring_reservation__allocated_time_slot__reservation_unit_option__application_section=(
models.OuterRef("pk")
),
)
)

return queryset

0 comments on commit 5392709

Please sign in to comment.