Skip to content

Commit

Permalink
Merge pull request #4 from FCSCOpendata/orgs
Browse files Browse the repository at this point in the history
move recursive from jinja into python
  • Loading branch information
MuhammadIsmailShahzad authored May 20, 2022
2 parents c693dd6 + aada3d7 commit bff718f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 10 deletions.
50 changes: 50 additions & 0 deletions ckanext/hierarchy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,53 @@ 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_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.
'''
html = '<ul class="hierarchy-tree-top">'
for node in top_nodes:
html += _render_tree_node(node, group_type)
return html + '</ul>'

def _render_tree_node(node, group_type):
body = ''
node_name = node['name']
if node['highlighted']:
body += f'<li class="highlighted" id="node_{node_name}">'
else:
body += f'<li id="node_{node_name}">'

if group_type == "organization":
body += '<a href="/organization/%s">%s</a>' % (node['name'], node['title'])
else:
body += '<a href="/group/%s">%s</a>' % (node['name'], node['title'])
if node['children']:
body+= '<ul class="hierarchy-tree">'
for child in node['children']:
body += _render_tree_node(child, group_type)
body += '</ul>'
body += "</li>"
return body
2 changes: 2 additions & 0 deletions ckanext/hierarchy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +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_list': helpers.render_tree_list
}

# IPackageController
Expand Down
4 changes: 3 additions & 1 deletion ckanext/hierarchy/templates/group/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}
<div id="organization-tree">
{% 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 }}
</div>

{% endblock %}
5 changes: 3 additions & 2 deletions ckanext/hierarchy/templates/group/snippets/group_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#}

{% 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' %}
<div id="publisher-tree">
{% 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_list(top_nodes=top_nodes,group_type="group") | safe }}
</div>
4 changes: 3 additions & 1 deletion ckanext/hierarchy/templates/group/snippets/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ <h1 class="heading">
{% block description %}
{{ super() }}
{% if group_dict %}
{% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %}
{% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
<hr>
{% 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 %}
5 changes: 3 additions & 2 deletions ckanext/hierarchy/templates/organization/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}
<div id="organization-tree">
{% 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 }}
</div>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

#}

{% 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' %}
<div id="publisher-tree">
{% 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_list(top_nodes=top_nodes) | safe }}
</div>
4 changes: 3 additions & 1 deletion ckanext/hierarchy/templates/snippets/organization.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ <h1 class="heading">
{% block description %}
{{ super() }}
{% if group_dict %}
{% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %}
{% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}
<hr>
{% 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 %}

0 comments on commit bff718f

Please sign in to comment.