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

Avoid adding the same items multiple times into the same menu #21

Open
wants to merge 3 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
4 changes: 3 additions & 1 deletion menu_generator/templatetags/menu_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def get_menu(context, menu_name):
:param menu_name: String, name of the menu to be found
:return: Generated menu
"""
menu_list = getattr(settings, menu_name, defaults.MENU_NOT_FOUND)
# Instantiate a new list() or else the same instance of the menu_list gets used over and over
# (and possibly re-adding the menu_from_apps list on each request)
menu_list = list(getattr(settings, menu_name, defaults.MENU_NOT_FOUND))
menu_from_apps = get_menu_from_apps(menu_name)
# If there isn't a menu on settings but there is menu from apps we built menu from apps
if menu_list == defaults.MENU_NOT_FOUND and menu_from_apps:
Expand Down
10 changes: 10 additions & 0 deletions menu_generator/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,13 @@ def test_generate_menu_selected_related_urls_submenu(self):
self.assertEqual(nav[0]["selected"], True)
self.assertEqual(nav[0]["submenu"][0]["selected"], True)
self.assertEqual(nav[0]["submenu"][1]["selected"], False)

def test_subsequent_requests(self):
self.request.user = TestUser(authenticated=True)
ctx = {
'request': self.request
}
nav1 = get_menu(ctx, 'NAV_MENU')
nav2 = get_menu(ctx, 'NAV_MENU')
# Both menus should be equal
self.assertEqual(nav1, nav2)