Skip to content

Commit

Permalink
Merge pull request #4221 from GeotrekCE/trek_network_filter
Browse files Browse the repository at this point in the history
💫 Add trek network filter in API v2 (refs #4216)
  • Loading branch information
juggler31 authored Jul 12, 2024
2 parents 36355c5 + a9bad3c commit 2568cd5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 7 deletions.
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 filter by network in API v2 (#4216)

**Documentation**

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -483,6 +483,9 @@ msgstr ""
msgid "Filter by one or more ratings id, comma-separated."
msgstr ""

msgid "Filter by one or more networks id, comma-separated."
msgstr ""

msgid "Root sites only"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -483,6 +483,9 @@ msgstr ""
msgid "Filter by one or more ratings id, comma-separated."
msgstr ""

msgid "Filter by one or more networks id, comma-separated."
msgstr ""

msgid "Root sites only"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -483,6 +483,9 @@ msgstr ""
msgid "Filter by one or more ratings id, comma-separated."
msgstr ""

msgid "Filter by one or more networks id, comma-separated."
msgstr ""

msgid "Root sites only"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: 2020-10-20 16:33+0000\n"
"Last-Translator: Bastien Potiron <[email protected]>\n"
"Language-Team: French <https://weblate.makina-corpus.net/projects/geotrek-"
Expand Down Expand Up @@ -524,6 +524,9 @@ msgstr "Cotations"
msgid "Filter by one or more ratings id, comma-separated."
msgstr "Filtrer par un ou plusieurs id de cotation, séparés par des virgules."

msgid "Filter by one or more networks id, comma-separated."
msgstr "Filtrer par un ou plusieurs réseaux d'itinéraire, séparés par des virgules."

msgid "Root sites only"
msgstr "Sites racine seulement"

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -483,6 +483,9 @@ msgstr ""
msgid "Filter by one or more ratings id, comma-separated."
msgstr ""

msgid "Filter by one or more networks id, comma-separated."
msgstr ""

msgid "Root sites only"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-12 12:45+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -483,6 +483,9 @@ msgstr ""
msgid "Filter by one or more ratings id, comma-separated."
msgstr ""

msgid "Filter by one or more networks id, comma-separated."
msgstr ""

msgid "Root sites only"
msgstr ""

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[1].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})

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})

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.String(
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 2568cd5

Please sign in to comment.