From 7cdeb908a18c8fdad68d124bb0076dc6ea3300c9 Mon Sep 17 00:00:00 2001 From: Alex Green Date: Fri, 29 Nov 2024 00:45:45 +0000 Subject: [PATCH 1/2] Update changelog and adds endpoint_from_url --- changes/8492.feature | 4 ++- ckan/lib/helpers.py | 26 +++++++++++++++++-- ckan/templates-midnight-blue/header.html | 8 +++--- .../macros/nav_link.html | 11 ++++---- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/changes/8492.feature b/changes/8492.feature index 937a7819a6e..585da7e9807 100644 --- a/changes/8492.feature +++ b/changes/8492.feature @@ -1 +1,3 @@ -Adds new logo and updates UI header in `midnight-blue` theme. \ No newline at end of file +Adds new logo and updates UI header in `midnight-blue` theme. +Adds macro for creating navigation links. +Adds helpers `endpoint_from_url` and `page_is_active`. \ No newline at end of file diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index f7841634a0f..766ba5075fb 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -30,7 +30,7 @@ from markdown import markdown from bleach import clean as bleach_clean, ALLOWED_TAGS, ALLOWED_ATTRIBUTES from ckan.common import asbool, config, current_user -from flask import flash, has_request_context +from flask import flash, has_request_context, current_app from flask import get_flashed_messages as _flask_get_flashed_messages from flask import redirect as _flask_redirect from flask import url_for as _flask_default_url_for @@ -736,10 +736,32 @@ def get_flashed_messages(**kwargs: Any): return _flask_get_flashed_messages(**kwargs) +@core_helper +def endpoint_from_url(url: str) -> str: + try: + urls = current_app.url_map.bind("") + match = urls.match(url) + endpoint = match[0] + except RuntimeError: + endpoint = "" + return endpoint + + @core_helper def page_is_active( menu_item: str, active_blueprints: Optional[list[str]] = None) -> bool: - '''Returns whether the current link is the active page or not''' + ''' + Returns whether the current link is the active page or not. + + `menu_item` + Accepts a route (e.g. 'group.index') or a URL (e.g. '/group') + `active_blueprints` + contains a list of additional blueprints that should be considered + active besides the one in `menu_item` + ''' + if menu_item.startswith("/"): + menu_item = endpoint_from_url(menu_item) + blueprint, endpoint = menu_item.split('.') item = { diff --git a/ckan/templates-midnight-blue/header.html b/ckan/templates-midnight-blue/header.html index 2be41ba8eb1..faf0f65fbc3 100644 --- a/ckan/templates-midnight-blue/header.html +++ b/ckan/templates-midnight-blue/header.html @@ -94,10 +94,10 @@ {% set org_type = h.default_group_type('organization') %} {% set group_type = h.default_group_type('group') %} - {{ nav.link(dataset_type ~ '.search', h.humanize_entity_type('package', dataset_type, 'main nav') or _('Datasets'), active_blueprints=[dataset_type, dataset_type + '_resource']) }} - {{ nav.link(org_type ~ '.index', h.humanize_entity_type('organization', org_type, 'main nav') or _('Organizations'), active_blueprints=[org_type]) }} - {{ nav.link(group_type ~ '.index', h.humanize_entity_type('group', group_type, 'main nav') or _('Groups'), active_blueprints=[group_type]) }} - {{ nav.link('home.about', _('About')) }} + {{ nav.link(h.url_for(dataset_type ~ '.search'), h.humanize_entity_type('package', dataset_type, 'main nav') or _('Datasets'), active_blueprints=[dataset_type, dataset_type + '_resource']) }} + {{ nav.link(h.url_for(org_type ~ '.index'), h.humanize_entity_type('organization', org_type, 'main nav') or _('Organizations'), active_blueprints=[org_type]) }} + {{ nav.link(h.url_for(group_type ~ '.index'), h.humanize_entity_type('group', group_type, 'main nav') or _('Groups'), active_blueprints=[group_type]) }} + {{ nav.link(h.url_for('home.about'), _('About')) }} {% endblock %} {% endblock %} diff --git a/ckan/templates-midnight-blue/macros/nav_link.html b/ckan/templates-midnight-blue/macros/nav_link.html index ede5d6ca33e..1aea7a8ec83 100644 --- a/ckan/templates-midnight-blue/macros/nav_link.html +++ b/ckan/templates-midnight-blue/macros/nav_link.html @@ -1,24 +1,23 @@ {# Builds links for navigation - nav_item - the name of the defined nav item defined in config/routing as the named route of the same name + nav_item - the URL for the route or + the named route of the nav item as defined in config/routing title - text used for the link icon - icon name used for link Example: {% import 'macros/nav_link.html' as nav %} - {{ nav.link('home.about', _('About')") }} + {{ nav.link(h.url_for('home.about'), _('About')") }} #} -{% macro link(nav_item, title, active_blueprints=[], icon="", url="") %} +{% macro link(nav_item, title, active_blueprints=[], icon="") %} {% set active_page = h.page_is_active(nav_item, active_blueprints=active_blueprints) %} -{% set url = url if url else h.url_for(nav_item, id) %} -
  • - + {% if icon %} {% endif %} From 73a53acfcb89291de560e679e46b24dc01cce57a Mon Sep 17 00:00:00 2001 From: Alex Green Date: Fri, 29 Nov 2024 00:48:46 +0000 Subject: [PATCH 2/2] fix lint error --- ckan/lib/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 766ba5075fb..975718cfeb8 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -755,7 +755,7 @@ def page_is_active( `menu_item` Accepts a route (e.g. 'group.index') or a URL (e.g. '/group') - `active_blueprints` + `active_blueprints` contains a list of additional blueprints that should be considered active besides the one in `menu_item` '''