From ab9c0ce42e0405ef68e08e693807b69a957c7d42 Mon Sep 17 00:00:00 2001 From: Michael Collins <15347726+michaeljcollinsuk@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:06:53 +0100 Subject: [PATCH] fix: safely check for MenuContent in result tree (#156) * fix: safely check for MenuContent in result tree * fix: update changelog Co-authored-by: Michael Collins --- CHANGELOG.rst | 2 ++ .../templatetags/navigation_admin_tree.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3bd45fef4..b210670b3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,8 @@ Changelog Unreleased ========== +* fix: safely checks for a MenuContent object in the context provided to the results tree templatetag, rather than +assume there is always one, to guard against 500 errors 1.8.0 (2022-10-13) ================== diff --git a/djangocms_navigation/templatetags/navigation_admin_tree.py b/djangocms_navigation/templatetags/navigation_admin_tree.py index 8b48b3f16..e13f0191d 100644 --- a/djangocms_navigation/templatetags/navigation_admin_tree.py +++ b/djangocms_navigation/templatetags/navigation_admin_tree.py @@ -76,6 +76,13 @@ def result_tree(context, cl, request): # Message defined here so that it is translatable. The title of the menu item is appended to the message in the JS move_node_message = _("Are you sure you want to move menu item") + # The ID is used when storing the menu tree state in the client session object, but as this templatetag can be used + # outside of djangocms-navigation, check safely if a MenuContent object is in the current context object + try: + menu_content_id = context["menu_content"].pk + except KeyError: + menu_content_id = "" + return { 'filtered': not check_empty_dict(request.GET), 'result_hidden_fields': list(result_hidden_fields(cl)), @@ -83,7 +90,7 @@ def result_tree(context, cl, request): 'results': list(results(cl)), 'disable_drag_drop': disable_drag_drop, 'move_node_message': move_node_message, - 'menu_content_id': context["menu_content"].pk + 'menu_content_id': menu_content_id, }