From 36c4bc30fb8012444bc3f7901243cd3e204c7bc4 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 22 Dec 2024 19:50:10 -0800 Subject: [PATCH] context_processors: Use breadcrumbs to highlight current section - We were using hacky logic of assuming that if a page using the URL /plinth/sys/..., then it belongs to 'system' section based on the URL match. This won't work when the URL does not follow this pattern for any reason. - Instead use the breadcrumbs mechanism which uses menu items and URL names to determine the section a page belongs to. Tests: - Visit page, apps page, system page, help pages, an app page in apps sections, an app page in system section, backups -> create backup page and notice that the correct section is highlighted. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/context_processors.py | 16 ++++----- plinth/templates/base.html | 11 ++++--- plinth/tests/test_context_processors.py | 44 ++++++++----------------- 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/plinth/context_processors.py b/plinth/context_processors.py index 1518afa78..3afc1e2c6 100644 --- a/plinth/context_processors.py +++ b/plinth/context_processors.py @@ -3,12 +3,10 @@ Django context processors to provide common data to templates. """ -import re - from django.utils.translation import gettext as _ from django.utils.translation import gettext_noop -from plinth import cfg, web_server +from plinth import cfg, views, web_server from plinth.utils import is_user_admin @@ -26,13 +24,15 @@ def common(request): notifications_context = Notification.get_display_context( request, user=request.user) - slash_indices = [match.start() for match in re.finditer('/', request.path)] - active_menu_urls = [ - request.path[:index + 1] for index in slash_indices[2:] - ] # Ignore the first two slashes '/plinth/apps/' + breadcrumbs = views.get_breadcrumbs(request) + active_section_url = [ + key for key, value in breadcrumbs.items() + if value.get('is_active_section') + ][0] return { 'cfg': cfg, - 'active_menu_urls': active_menu_urls, + 'breadcrumbs': breadcrumbs, + 'active_section_url': active_section_url, 'box_name': _(cfg.box_name), 'user_is_admin': is_user_admin(request, True), 'user_css': web_server.get_user_css(), diff --git a/plinth/templates/base.html b/plinth/templates/base.html index 91a0645a6..c1b274648 100644 --- a/plinth/templates/base.html +++ b/plinth/templates/base.html @@ -88,8 +88,9 @@