Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: option to retain child menu items when visible=False #143

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ MENU_HIDE_EMPTY
``MENU_HIDE_EMPTY`` controls if menu items without an explicit ``check`` callback
should be visible even if they have no children

MENU_TRIM_NON_VISIBLE_CHILD_ITEMS
-------------------
**Default: ``True``**

``MENU_TRIM_NON_VISIBLE_CHILD_ITEMS`` controls if children of menu items should be
removed from the ``item.children`` list if they are not visible. Retaining them
can be useful for breadcrumbs, but can also lead to unexpected results if you do not
check the ``visible`` property of the children in templates.

.. _Django settings file: https://docs.djangoproject.com/en/dev/topics/settings/
11 changes: 6 additions & 5 deletions simple_menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@ def process(self, request):
child.parent = self
child.process(request)

self.children = [
child
for child in children
if child.visible
]
if getattr(settings, 'MENU_TRIM_NON_VISIBLE_CHILD_ITEMS', True):
self.children = [
child
for child in children
if child.visible
]
self.children.sort(key=lambda child: child.weight)

# if we have no children and MENU_HIDE_EMPTY then we are not visible and should return
Expand Down
35 changes: 34 additions & 1 deletion simple_menu/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.conf import settings
from django.template import Template, Context
from django.test import TestCase
from django.test import TestCase, override_settings
from django.test.client import RequestFactory

from simple_menu import Menu, MenuItem
Expand Down Expand Up @@ -264,3 +264,36 @@ def test_kwargs(self):
self.assertTrue(item.arbitrary)
self.assertEqual(item.dictionary, {'a': 1})
self.assertRaises(AttributeError, lambda: item.nope)

@override_settings(MENU_TRIM_NON_VISIBLE_CHILD_ITEMS=False)
def test_trim_non_visible_child_items__disabled(self):
"""
Ensure that non-visible child items are NOT trimmed from the menu
"""
# Generate the menu
child_item = MenuItem("child", "/child", visible=False)
item = MenuItem("test", "/test", children=[child_item])

# Process the item
request = RequestFactory().get('/test')
item.process(request)

# Check that non-visible child items are still in the menu
self.assertIn(child_item, item.children)

def test_trim_non_visible_child_items__enabled(self):
"""
Ensure that non-visible child items are trimmed from the menu.

This is the default behaviour
"""
# Generate the menu
child_item = MenuItem("child", "/child", visible=False)
item = MenuItem("test", "/test", children=[child_item])

# Process the item
request = RequestFactory().get('/test')
item.process(request)

# Check that non-visible child items removed still in the menu
self.assertNotIn(child_item, item.children)