From 51a5468faecfc9e010363aaf602c0b836ca5a5d1 Mon Sep 17 00:00:00 2001 From: steveoni Date: Wed, 18 May 2022 17:18:34 +0100 Subject: [PATCH 1/3] move recurcive from jinja into python --- ckanext/hierarchy/helpers.py | 42 +++++++++++++++++++ ckanext/hierarchy/plugin.py | 1 + ckanext/hierarchy/templates/group/about.html | 4 +- .../templates/group/snippets/group_list.html | 10 ++--- .../templates/group/snippets/info.html | 4 +- .../templates/organization/about.html | 5 ++- .../snippets/organization_list.html | 10 ++--- .../templates/snippets/organization.html | 4 +- 8 files changed, 61 insertions(+), 19 deletions(-) diff --git a/ckanext/hierarchy/helpers.py b/ckanext/hierarchy/helpers.py index 1a43e383f..7865b0dff 100644 --- a/ckanext/hierarchy/helpers.py +++ b/ckanext/hierarchy/helpers.py @@ -98,3 +98,45 @@ def is_include_children_selected(fields): if request.params.get('include_children'): include_children_selected = True return include_children_selected + + +def render_tree(top_nodes=None, group_type='organization'): + '''Returns HTML for a hierarchy of all publishers''' + if not top_nodes: + from ckan.logic import get_action + from ckan import model + context = {'model': model, 'session': model.Session} + top_nodes = get_action('group_tree')(context=context, + data_dict={'type': group_type}) + + + return _render_tree(top_nodes, group_type) + +def _render_tree(top_nodes, group_type): + '''Renders a tree of nodes. 10x faster than Jinja/organization_tree.html + Note: avoids the slow url_for routine. + ''' + html = '' + +def _render_tree_node(node, group_type): + body = '' + node_name = node['name'] + if node['highlighted']: + body += f'
  • ' + else: + body += f'
  • ' + + if group_type == "organization": + body += '%s' % (node['name'], node['title']) + else: + body += '%s' % (node['name'], node['title']) + if node['children']: + body+= '' + body += "
  • " + return body diff --git a/ckanext/hierarchy/plugin.py b/ckanext/hierarchy/plugin.py index 6cb1f6a28..75c5555d9 100644 --- a/ckanext/hierarchy/plugin.py +++ b/ckanext/hierarchy/plugin.py @@ -77,6 +77,7 @@ def get_helpers(self): helpers.get_allowable_parent_groups, 'is_include_children_selected': helpers.is_include_children_selected, + 'render_tree': helpers.render_tree } # IPackageController diff --git a/ckanext/hierarchy/templates/group/about.html b/ckanext/hierarchy/templates/group/about.html index 9cb419ed1..d9acb6df0 100644 --- a/ckanext/hierarchy/templates/group/about.html +++ b/ckanext/hierarchy/templates/group/about.html @@ -14,8 +14,10 @@ {% block group_description %} {{ super() }} + {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} + {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% snippet 'group/snippets/group_tree.html', top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type)], use_longnames=True %} + {{ h.render_tree(top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type)],group_type="group") | safe }}
    {% endblock %} diff --git a/ckanext/hierarchy/templates/group/snippets/group_list.html b/ckanext/hierarchy/templates/group/snippets/group_list.html index 47f358cbb..db85bb545 100644 --- a/ckanext/hierarchy/templates/group/snippets/group_list.html +++ b/ckanext/hierarchy/templates/group/snippets/group_list.html @@ -9,12 +9,8 @@ #} {% if q is not defined %}{% set q = c.q %}{% endif %} - +{% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} +{% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% if q %} - {% set top_nodes = h.group_tree_highlight(groups, h.group_tree(type_='group')) %} - {% else %} - {% set top_nodes = h.group_tree(organizations=groups, type_='group') %} - {% endif %} - {% snippet 'group/snippets/group_tree.html', top_nodes=top_nodes, use_longnames=True %} + {{ h.render_tree(group_type="group") | safe }}
    diff --git a/ckanext/hierarchy/templates/group/snippets/info.html b/ckanext/hierarchy/templates/group/snippets/info.html index b1d31a992..4aab46873 100644 --- a/ckanext/hierarchy/templates/group/snippets/info.html +++ b/ckanext/hierarchy/templates/group/snippets/info.html @@ -18,7 +18,9 @@

    {% block description %} {{ super() }} {% if group_dict %} + {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} + {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% snippet 'group/snippets/group_tree.html', top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type, include_siblings=False)], use_shortnames=True %} + {{ h.render_tree(top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type, include_siblings=False)],group_type="group") | safe }} {% endif %} {% endblock %} diff --git a/ckanext/hierarchy/templates/organization/about.html b/ckanext/hierarchy/templates/organization/about.html index d85152b03..74ab2887f 100644 --- a/ckanext/hierarchy/templates/organization/about.html +++ b/ckanext/hierarchy/templates/organization/about.html @@ -14,9 +14,10 @@ {% block organization_description %} {{ super() }} - + {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} + {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% snippet 'organization/snippets/organization_tree.html', top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type)], use_longnames=True %} + {{ h.render_tree(top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type)]) | safe }}
    {% endblock %} diff --git a/ckanext/hierarchy/templates/organization/snippets/organization_list.html b/ckanext/hierarchy/templates/organization/snippets/organization_list.html index 4e24264ea..123399929 100644 --- a/ckanext/hierarchy/templates/organization/snippets/organization_list.html +++ b/ckanext/hierarchy/templates/organization/snippets/organization_list.html @@ -9,12 +9,8 @@ #} {% if q is not defined %}{% set q = c.q %}{% endif %} - +{% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} +{% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% if q %} - {% set top_nodes = h.group_tree_highlight(organizations, h.group_tree(type_='organization')) %} - {% else %} - {% set top_nodes = h.group_tree(organizations=organizations, type_='organization') %} - {% endif %} - {% snippet 'organization/snippets/organization_tree.html', top_nodes=top_nodes, use_longnames=True %} + {{ h.render_tree() | safe }}
    diff --git a/ckanext/hierarchy/templates/snippets/organization.html b/ckanext/hierarchy/templates/snippets/organization.html index 80ce313b4..3d2a4fae4 100644 --- a/ckanext/hierarchy/templates/snippets/organization.html +++ b/ckanext/hierarchy/templates/snippets/organization.html @@ -18,7 +18,9 @@

    {% block description %} {{ super() }} {% if group_dict %} + {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} + {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {% snippet 'organization/snippets/organization_tree.html', top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type, include_siblings=False)], use_shortnames=True %} + {{ h.render_tree(top_nodes=[h.group_tree_section(id_=group_dict.id, type_=group_dict.type, include_siblings=False)]) | safe }} {% endif %} {% endblock %} From d80e0a3036d4144e0db466d7b61961e99e9d6a5c Mon Sep 17 00:00:00 2001 From: steveoni Date: Thu, 19 May 2022 11:51:41 +0100 Subject: [PATCH 2/3] enable pagination --- ckanext/hierarchy/templates/group/snippets/group_list.html | 7 ++++++- .../templates/organization/snippets/organization_list.html | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ckanext/hierarchy/templates/group/snippets/group_list.html b/ckanext/hierarchy/templates/group/snippets/group_list.html index db85bb545..33763ee44 100644 --- a/ckanext/hierarchy/templates/group/snippets/group_list.html +++ b/ckanext/hierarchy/templates/group/snippets/group_list.html @@ -12,5 +12,10 @@ {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {{ h.render_tree(group_type="group") | safe }} + {% if q %} + {% set top_nodes = h.group_tree_highlight(groups, h.group_tree(type_='group')) %} + {% else %} + {% set top_nodes = h.group_tree(organizations=groups, type_='group') %} + {% endif %} + {{ h.render_tree(top_nodes=top_nodes,group_type="group") | safe }}
    diff --git a/ckanext/hierarchy/templates/organization/snippets/organization_list.html b/ckanext/hierarchy/templates/organization/snippets/organization_list.html index 123399929..56c390cf9 100644 --- a/ckanext/hierarchy/templates/organization/snippets/organization_list.html +++ b/ckanext/hierarchy/templates/organization/snippets/organization_list.html @@ -12,5 +12,10 @@ {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    - {{ h.render_tree() | safe }} + {% if q %} + {% set top_nodes = h.group_tree_highlight(organizations, h.group_tree(type_='organization')) %} + {% else %} + {% set top_nodes = h.group_tree(organizations=organizations, type_='organization') %} + {% endif %} + {{ h.render_tree(top_nodes=top_nodes) | safe }}
    From aada3d7c9955034735645b6bf0d54bc1414a600d Mon Sep 17 00:00:00 2001 From: steveoni Date: Thu, 19 May 2022 18:34:34 +0100 Subject: [PATCH 3/3] prevent fetching of group and org list if top_node is empty --- ckanext/hierarchy/helpers.py | 8 ++++++++ ckanext/hierarchy/plugin.py | 3 ++- .../hierarchy/templates/group/snippets/group_list.html | 2 +- .../organization/snippets/organization_list.html | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ckanext/hierarchy/helpers.py b/ckanext/hierarchy/helpers.py index 7865b0dff..879cd1e9b 100644 --- a/ckanext/hierarchy/helpers.py +++ b/ckanext/hierarchy/helpers.py @@ -112,6 +112,14 @@ def render_tree(top_nodes=None, group_type='organization'): return _render_tree(top_nodes, group_type) + +def render_tree_list(top_nodes=None, group_type='organization'): + '''Returns HTML for a hierarchy of all publishers''' + if not top_nodes: + return '' + return _render_tree(top_nodes, group_type) + + def _render_tree(top_nodes, group_type): '''Renders a tree of nodes. 10x faster than Jinja/organization_tree.html Note: avoids the slow url_for routine. diff --git a/ckanext/hierarchy/plugin.py b/ckanext/hierarchy/plugin.py index 75c5555d9..4e6e3846e 100644 --- a/ckanext/hierarchy/plugin.py +++ b/ckanext/hierarchy/plugin.py @@ -77,7 +77,8 @@ def get_helpers(self): helpers.get_allowable_parent_groups, 'is_include_children_selected': helpers.is_include_children_selected, - 'render_tree': helpers.render_tree + 'render_tree': helpers.render_tree, + 'render_tree_list': helpers.render_tree_list } # IPackageController diff --git a/ckanext/hierarchy/templates/group/snippets/group_list.html b/ckanext/hierarchy/templates/group/snippets/group_list.html index 33763ee44..55bc6f40b 100644 --- a/ckanext/hierarchy/templates/group/snippets/group_list.html +++ b/ckanext/hierarchy/templates/group/snippets/group_list.html @@ -17,5 +17,5 @@ {% else %} {% set top_nodes = h.group_tree(organizations=groups, type_='group') %} {% endif %} - {{ h.render_tree(top_nodes=top_nodes,group_type="group") | safe }} + {{ h.render_tree_list(top_nodes=top_nodes,group_type="group") | safe }} diff --git a/ckanext/hierarchy/templates/organization/snippets/organization_list.html b/ckanext/hierarchy/templates/organization/snippets/organization_list.html index 56c390cf9..d5512df9c 100644 --- a/ckanext/hierarchy/templates/organization/snippets/organization_list.html +++ b/ckanext/hierarchy/templates/organization/snippets/organization_list.html @@ -8,7 +8,7 @@ #} -{% if q is not defined %}{% set q = c.q %}{% endif %} +{% if q is not defined %}{% set q = g.q %}{% endif %} {% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %} {% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
    @@ -17,5 +17,5 @@ {% else %} {% set top_nodes = h.group_tree(organizations=organizations, type_='organization') %} {% endif %} - {{ h.render_tree(top_nodes=top_nodes) | safe }} + {{ h.render_tree_list(top_nodes=top_nodes) | safe }}