Skip to content

Commit

Permalink
💫 [FEAT] Add trek network filter (refs #4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
juggler31 committed Jul 12, 2024
1 parent af0cf09 commit 9d57de0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
- Allows user to edit flatpage WYSIWYG button-link and suggestion block
- Display years in Report filter in descending order (#4085)
- Change CSS overflow in filters popup, to display full select dropdowns (with mapentity update)
- Add trek network filter (#4216)

**Documentation**

Expand Down
19 changes: 19 additions & 0 deletions geotrek/api/tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def setUpTestData(cls):
cls.organism = common_factory.OrganismFactory.create()
cls.theme = common_factory.ThemeFactory.create()
cls.network = trek_factory.TrekNetworkFactory.create()
cls.network2 = trek_factory.TrekNetworkFactory.create()
cls.rating = trek_factory.RatingFactory()
cls.rating2 = trek_factory.RatingFactory()
cls.label = common_factory.LabelFactory(id=23)
Expand All @@ -320,6 +321,7 @@ def setUpTestData(cls):
cls.treks[0].save()
cls.treks[0].themes.add(cls.theme)
cls.treks[0].networks.add(cls.network)
cls.treks[0].networks.add(cls.network2)
cls.treks[0].labels.add(cls.label)
cls.treks[0].ratings.add(cls.rating)
cls.treks[1].ratings.add(cls.rating2)
Expand Down Expand Up @@ -1132,6 +1134,19 @@ def test_trek_ratings_filter(self):
json_response = response.json()
self.assertEqual(len(json_response.get('results')), 0)

def test_trek_networks_filter(self):
response = self.get_trek_list({'networks': self.network.pk})
# test response code
self.assertEqual(response.status_code, 200)
json_response = response.json()
self.assertEqual(len(json_response.get('results')), 1)

response = self.get_trek_list({'networks': 0})
# test response code
self.assertEqual(response.status_code, 200)
json_response = response.json()
self.assertEqual(len(json_response.get('results')), 0)

def test_version_route(self):
response = self.client.get("/api/v2/version")
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -1286,6 +1301,10 @@ def test_trek_ratings(self):
response = self.get_trek_list({'ratings': f"{self.rating.pk},{self.rating2.pk}"})
self.assertEqual(len(response.json()['results']), 2)

def test_trek_networks(self):
response = self.get_trek_list({'networks': f"{self.network.pk},{self.network2.pk}"})
self.assertEqual(len(response.json()['results']), 2)

def test_trek_child_not_published_detail_view_ok_if_ancestor_published(self):
response = self.get_trek_detail(self.child1.pk)
self.assertEqual(response.status_code, 200)
Expand Down
18 changes: 18 additions & 0 deletions geotrek/api/v2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,24 @@ def get_schema_fields(self, view):
)


class GeotrekNetworksFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
networks = request.GET.get('networks')
if networks:
queryset = queryset.filter(networks__in=networks.split(','))
return queryset

def get_schema_fields(self, view):
return (
Field(
name='networks', required=False, location='query', schema=coreschema.Integer(
title=_("Networks"),
description=_('Filter by one or more networks id, comma-separated.')
)
),
)


class GeotrekSiteFilter(GeotrekZoningAndThemeFilter):
def filter_queryset(self, request, queryset, view):
root_sites_only = request.GET.get('root_sites_only')
Expand Down
3 changes: 2 additions & 1 deletion geotrek/api/v2/views/trekking.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class TrekViewSet(api_viewsets.GeotrekGeometricViewset):
api_filters.GeotrekTrekQueryParamsFilter,
api_filters.NearbyContentFilter,
api_filters.UpdateOrCreateDateFilter,
api_filters.GeotrekRatingsFilter
api_filters.GeotrekRatingsFilter,
api_filters.GeotrekNetworksFilter
)
serializer_class = api_serializers.TrekSerializer

Expand Down

0 comments on commit 9d57de0

Please sign in to comment.