Skip to content

Commit

Permalink
Take both people capacity fields into account in filtering
Browse files Browse the repository at this point in the history
Make sure that both `people_capacity_lower` and `people_capacity_upper`
are considered when filtering resources by people capacity.

Refs TTVA-217
  • Loading branch information
japauliina committed Jan 14, 2025
1 parent 48e83dc commit 951091a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions resources/api/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,7 @@ def __init__(self, *args, **kwargs):
type = django_filters.Filter(
field_name="type__id", lookup_expr="in", widget=django_filters.widgets.CSVWidget
)
people = django_filters.NumberFilter(
field_name="people_capacity_lower", lookup_expr="gte"
)
people = django_filters.NumberFilter(method='filter_people_capacity')
need_manual_confirmation = django_filters.BooleanFilter(
field_name="need_manual_confirmation", widget=DRFFilterBooleanWidget
)
Expand Down Expand Up @@ -727,6 +725,11 @@ def __init__(self, *args, **kwargs):
),
)

def filter_people_capacity(self, queryset, name, value):
return queryset.filter(
Q(people_capacity_lower__gte=value) | Q(people_capacity_upper__gte=value)
)

def filter_is_favorite(self, queryset, name, value):
if not self.user.is_authenticated:
if value:
Expand Down
27 changes: 27 additions & 0 deletions resources/tests/test_resource_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,3 +1645,30 @@ def test_query_counts(

with django_assert_max_num_queries(MAX_QUERIES):
staff_api_client.get(list_url)

@pytest.mark.django_db
def test_filter_people_capacity(api_client, resource_in_unit, resource_in_unit2, resource_in_unit3):
resource_in_unit.people_capacity_lower = 10
resource_in_unit.people_capacity_upper = 20
resource_in_unit.save()

resource_in_unit2.people_capacity_lower = 15
resource_in_unit2.people_capacity_upper = 25
resource_in_unit2.save()

resource_in_unit3.people_capacity_lower = 20
resource_in_unit3.people_capacity_upper = None
resource_in_unit3.save()

response = api_client.get(reverse("resource-list") + "?people=15")
assert response.status_code == 200
assert len(response.data["results"]) == 3

response = api_client.get(reverse("resource-list") + "?people=22")
assert response.status_code == 200
assert len(response.data["results"]) == 1
assert response.data["results"][0]["id"] == resource_in_unit2.id

response = api_client.get(reverse("resource-list") + "?people=50")
assert response.status_code == 200
assert len(response.data["results"]) == 0

0 comments on commit 951091a

Please sign in to comment.