Skip to content

Commit

Permalink
Add Reservation Unit persons allowed filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta authored and matti-lamppu committed Jan 13, 2025
1 parent 4a738b4 commit 23135f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/test_graphql_api/test_reservation_unit/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 23135f6

Please sign in to comment.