Skip to content

Commit

Permalink
Merge pull request #4129 from GeotrekCE/fix_hide_menus_for_mobile_pla…
Browse files Browse the repository at this point in the history
…tform_on_api

MenuItems with platform "Mobile" are now hidden on API (#4127)
  • Loading branch information
marcantoinedupre authored Sep 3, 2024
2 parents 4e3bed9 + 86bf5dd commit 241e628
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG

- Fix swapped plural and singular translations for Annotation Categories (#4032)
- Filter out deleted services in API responses (#4284)
- MenuItems with platform "Mobile" are now hidden on public API (#4127)

**Bug fixes**

Expand Down
21 changes: 21 additions & 0 deletions geotrek/api/tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3557,6 +3557,27 @@ def test_tree_child_menu_item_not_exposed_if_target_page_not_published(self):
parent_repr = data[0]
self.assertEqual(len(parent_repr["children"]), 0)

def test_tree_menuitem_for_mobile_platform_not_exposed(self):
parent1 = self.published_menu_item_factory(platform=MenuItem.PLATFORM_CHOICES.ALL)
child1 = self.published_menu_item_factory(platform=MenuItem.PLATFORM_CHOICES.MOBILE)
self.add_child(parent1, child1)
child2 = self.published_menu_item_factory(platform=MenuItem.PLATFORM_CHOICES.WEB)
self.add_child(parent1, child2)
parent2 = self.published_menu_item_factory(platform=MenuItem.PLATFORM_CHOICES.MOBILE)
child3 = self.published_menu_item_factory(platform=MenuItem.PLATFORM_CHOICES.ALL)
self.add_child(parent2, child3)

response = self.client.get('/api/v2/menu_item/')

self.assertEqual(response.status_code, 200)
menu_items = response.data
self.assertEqual(len(menu_items), 1)
menu_item = menu_items[0]
self.assertEqual(menu_item["id"], parent1.id)
self.assertEqual(len(menu_item["children"]), 1)
child = menu_item["children"][0]
self.assertEqual(child["id"], child2.id)

def test_detail(self):
menu_item = self.published_menu_item_factory(title_en="Hello!", title_fr="Bonjour !")

Expand Down
12 changes: 10 additions & 2 deletions geotrek/api/v2/views/flatpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from geotrek.api.v2 import utils as api_utils
from geotrek.common.models import Attachment
from geotrek.flatpages import models as flatpages_models
from geotrek.flatpages.models import MenuItem


class FlatPageViewSet(api_viewsets.GeotrekViewSet):
Expand Down Expand Up @@ -40,7 +41,11 @@ class MenuItemRetrieveView(RetrieveAPIView):
class MenuItemTreeView(GenericAPIView):
# from https://stackoverflow.com/questions/21112302/how-to-serialize-hierarchical-relationship-in-django-rest
serializer_class = api_serializers.MenuItemSerializer
queryset = flatpages_models.MenuItem.objects.filter(depth=1)
queryset = (
flatpages_models.MenuItem.objects
.filter(depth=1)
.exclude(platform=MenuItem.PLATFORM_CHOICES.MOBILE)
)
filter_backends = (
GeotrekPublishedFilter,
api_filters.MenuItemFilter,
Expand Down Expand Up @@ -69,6 +74,9 @@ def _check_page_published(self, node):

def _recursive_node_to_dict(self, node):
result = self.get_serializer(instance=node).data
children = [self._recursive_node_to_dict(c) for c in self.filter_queryset(node.get_children()) if self._check_page_published(c)]
children_qs = self.filter_queryset(
node.get_children().exclude(platform=MenuItem.PLATFORM_CHOICES.MOBILE)
)
children = [self._recursive_node_to_dict(c) for c in children_qs if self._check_page_published(c)]
result["children"] = children
return result

0 comments on commit 241e628

Please sign in to comment.