Skip to content

Commit

Permalink
Merge pull request #64 from ckan/improve_show_function
Browse files Browse the repository at this point in the history
Back to base show functions
  • Loading branch information
amercader authored May 29, 2023
2 parents 65c2382 + c0c3a8d commit 9078e8e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,7 @@ should come before hierarchy_form and hierarchy_group_form

## Config settings

### Custom group_type_show_action

If you use custom group types you'll need to define which `show`
action to use for each group type. For example, if you have a
group type called `country` you'll need to create a new action
called `country_show` in you extension. This is the default behaviour
and in this case you don't need to add any settings to your config file.
On the contrary, if you prefer to continue using the base action `organization_show`
you'll need to add the following setting to your config file:

ckanext.hierarchy.group_type_show_action.country = organization_show

You can define a different action for each group type.
None at present

## Tests

Expand Down
24 changes: 9 additions & 15 deletions ckanext/hierarchy/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ckan.plugins import toolkit
import ckan.plugins as p
import ckan.model as model
from ckan.common import request, is_flask_request
Expand Down Expand Up @@ -47,45 +46,40 @@ def group_tree_section(id_, type_='organization', include_parents=True,
{'id': id_, 'type': type_, })


def get_group_type_show(type_):
""" User with custom group types will be able to
use a "custom_type_show" action or the base action organization_show
for each new group type."""
custom_show_action = toolkit.config.get(
'ckanext.hierarchy.group_type_show_action.{}'.format(type_)
)
return custom_show_action or '{}_show'.format(type_)
def _get_action_name(group_id):
model_obj = model.Group.get(group_id)
return "organization_show" if model_obj.is_organization else "group_show"


def group_tree_parents(id_, type_='organization'):
show_fn = get_group_type_show(type_)
def group_tree_parents(id_):
action_name = _get_action_name(id_)
data_dict = {
'id': id_,
'include_dataset_count': False,
'include_users': False,
'include_followers': False,
'include_tags': False
}
tree_node = p.toolkit.get_action(show_fn)({}, data_dict)
tree_node = p.toolkit.get_action(action_name)({}, data_dict)
if (tree_node['groups']):
parent_id = tree_node['groups'][0]['name']
parent_node = \
p.toolkit.get_action(show_fn)({}, {'id': parent_id})
p.toolkit.get_action(action_name)({}, {'id': parent_id})
return group_tree_parents(parent_id) + [parent_node]
else:
return []


def group_tree_get_longname(id_, default="", type_='organization'):
show_fn = get_group_type_show(type_)
action_name = _get_action_name(id_)
data_dict = {
'id': id_,
'include_dataset_count': False,
'include_users': False,
'include_followers': False,
'include_tags': False
}
tree_node = p.toolkit.get_action(show_fn)({}, data_dict)
tree_node = p.toolkit.get_action(action_name)({}, data_dict)
longname = tree_node.get("longname", default)
if not longname:
return default
Expand Down
2 changes: 1 addition & 1 deletion ckanext/hierarchy/templates/package/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% set organization = pkg.organization.title %}
{% set group_type = pkg.organization.type %}
<li>{% link_for _('Organizations'), controller=group_type, action='index' %}</li>
{% set parent_list = h.group_tree_parents(pkg.organization.name, type_=group_type) %}
{% set parent_list = h.group_tree_parents(pkg.organization.name) %}
{% for parent_node in parent_list %}
<li>{% link_for parent_node.title|truncate(35), controller=group_type, action='read', id=parent_node.name %}</li>
{% endfor %}
Expand Down

0 comments on commit 9078e8e

Please sign in to comment.