Skip to content

Commit

Permalink
Avoid adding the same items multiple times into the same menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Spawin committed Jul 13, 2024
1 parent 13b64cf commit c0825ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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
12 changes: 11 additions & 1 deletion menu_generator/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,14 @@ def test_generate_menu_selected_related_views_submenu(self):
self.assertEqual(len(nav), 1)
self.assertEqual(nav[0]["selected"], True)
self.assertEqual(nav[0]["submenu"][0]["selected"], True)
self.assertEqual(nav[0]["submenu"][1]["selected"], False)
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)

0 comments on commit c0825ff

Please sign in to comment.