From 23135f6f522b85b9624bea12a68b17407a1eb4cf Mon Sep 17 00:00:00 2001 From: Eemeli Ranta Date: Wed, 8 Jan 2025 20:50:39 +0200 Subject: [PATCH] Add Reservation Unit persons allowed filter --- .../test_reservation_unit/test_filtering.py | 19 +++++++++++++++++++ .../types/reservation_unit/filtersets.py | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/test_graphql_api/test_reservation_unit/test_filtering.py b/tests/test_graphql_api/test_reservation_unit/test_filtering.py index 972713623..e219b15f7 100644 --- a/tests/test_graphql_api/test_reservation_unit/test_filtering.py +++ b/tests/test_graphql_api/test_reservation_unit/test_filtering.py @@ -339,6 +339,25 @@ def test_reservation_unit__filter__by_min_persons_lte(graphql): assert response.node(2) == {"pk": reservation_unit_3.pk} +def test_reservation_unit__filter__by_persons_allowed(graphql): + reservation_unit_1 = ReservationUnitFactory.create(min_persons=None, max_persons=None) + reservation_unit_2 = ReservationUnitFactory.create(min_persons=None, max_persons=201) + reservation_unit_3 = ReservationUnitFactory.create(min_persons=200, max_persons=None) + reservation_unit_4 = ReservationUnitFactory.create(min_persons=199, max_persons=200) + ReservationUnitFactory.create(min_persons=198, max_persons=199) # Filtered out by max_persons + ReservationUnitFactory.create(min_persons=201, max_persons=None) # Filtered out by min_persons + + query = reservation_units_query(personsAllowed=200) + response = graphql(query) + + assert response.has_errors is False, response.errors + assert len(response.edges) == 4 + assert response.node(0) == {"pk": reservation_unit_1.pk} + assert response.node(1) == {"pk": reservation_unit_2.pk} + assert response.node(2) == {"pk": reservation_unit_3.pk} + assert response.node(3) == {"pk": reservation_unit_4.pk} + + def test_reservation_unit__filter__by_name_fi(graphql): reservation_unit = ReservationUnitFactory.create(name_fi="foo") ReservationUnitFactory.create(name_fi="bar") diff --git a/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py b/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py index 28a9e29b0..0d4997160 100644 --- a/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py +++ b/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py @@ -63,6 +63,7 @@ class ReservationUnitFilterSet(ModelFilterSet, ReservationUnitFilterSetMixin): min_persons_lte = django_filters.NumberFilter(field_name="min_persons", method="get_min_persons_lte") max_persons_gte = django_filters.NumberFilter(field_name="max_persons", method="get_max_persons_gte") max_persons_lte = django_filters.NumberFilter(field_name="max_persons", method="get_max_persons_lte") + persons_allowed = django_filters.NumberFilter(method="get_persons_allowed") text_search = django_filters.CharFilter(method="get_text_search") @@ -171,6 +172,13 @@ def get_min_persons_gte(qs: ReservationUnitQuerySet, name: str, value: int) -> Q def get_min_persons_lte(qs: ReservationUnitQuerySet, name: str, value: int) -> QuerySet: return qs.filter(Q(min_persons__lte=value) | Q(min_persons__isnull=True)) + @staticmethod + def get_persons_allowed(qs: ReservationUnitQuerySet, name: str, value: int) -> QuerySet: + return qs.filter( + Q(Q(min_persons__lte=value) | Q(min_persons__isnull=True)) + & Q(Q(max_persons__gte=value) | Q(max_persons__isnull=True)) + ) + @staticmethod def get_is_visible(qs: ReservationUnitQuerySet, name: str, value: bool) -> QuerySet: return qs.visible() if value else qs.hidden()