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 <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2024-12-25 12:24:29 +02:00
1 parent a29fb97dd9
commit 36c4bc30fb
3 files changed
+27 -44

No files matched your search

+8 -8
View File
@@ -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(),