Skip to content

Commit

Permalink
Implement metadata collection list/view UIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-saeon committed Nov 14, 2018
1 parent 97bad6e commit bd06ba6
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 32 deletions.
91 changes: 91 additions & 0 deletions ckanext/metadata/controllers/metadata_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import ckan.plugins.toolkit as tk
from ckan.controllers.group import GroupController
from ckan.lib.render import TemplateNotFound
import ckan.model as model
import ckan.lib.helpers as helpers


class MetadataCollectionController(GroupController):
Expand Down Expand Up @@ -47,3 +49,92 @@ def _render_template(self, template_name, group_type):
return tk.render(self._substitute_name(template_name), extra_vars={'group_type': group_type})
except TemplateNotFound:
return tk.render(template_name, extra_vars={'group_type': group_type})

def _get_org_dict(self, organization_id):
context = {'model': model, 'session': model.Session, 'user': tk.c.user}
data_dict = {'id': organization_id}
try:
return tk.get_action('organization_show')(context, data_dict)
except tk.NotFound:
tk.abort(404, tk._('Group not found'))
except tk.NotAuthorized:
tk.abort(403, tk._('Not authorized to see this page'))

def index(self, organization_id):
"""
Copied from GroupController.index() with some modifications.
"""
tk.c.org_dict = self._get_org_dict(organization_id)

page = tk.h.get_page_number(tk.request.params) or 1
items_per_page = 21

context = {'model': model, 'session': model.Session,
'user': tk.c.user, 'for_view': True,
'with_private': False}

q = tk.c.q = tk.request.params.get('q', '')
sort_by = tk.c.sort_by_selected = tk.request.params.get('sort')
try:
tk.check_access('site_read', context)
tk.check_access('metadata_collection_list', context)
except tk.NotAuthorized:
tk.abort(403, tk._('Not authorized to see this page'))

if tk.c.userobj:
context['user_id'] = tk.c.userobj.id
context['user_is_admin'] = tk.c.userobj.sysadmin

try:
data_dict_global_results = {
'owner_org': organization_id,
'all_fields': False,
'q': q,
'sort': sort_by,
'type': 'metadata_collection',
}
global_results = tk.get_action('metadata_collection_list')(context, data_dict_global_results)
except tk.ValidationError as e:
if e.error_dict and e.error_dict.get('message'):
msg = e.error_dict['message']
else:
msg = str(e)
tk.h.flash_error(msg)
tk.c.page = helpers.Page([], 0)
return tk.render(self._index_template('metadata_collection'),
extra_vars={'group_type': 'metadata_collection'})

data_dict_page_results = {
'owner_org': organization_id,
'all_fields': True,
'q': q,
'sort': sort_by,
'type': 'metadata_collection',
'limit': items_per_page,
'offset': items_per_page * (page - 1),
'include_extras': True
}
page_results = tk.get_action('metadata_collection_list')(context, data_dict_page_results)

tk.c.page = helpers.Page(
collection=global_results,
page=page,
url=tk.h.pager_url,
items_per_page=items_per_page,
)

tk.c.page.items = page_results
return tk.render(self._index_template('metadata_collection'),
extra_vars={'group_type': 'metadata_collection'})

def read(self, organization_id, id, limit=20):
tk.c.org_dict = self._get_org_dict(organization_id)
return super(MetadataCollectionController, self).read(id, limit)

def activity(self, organization_id, id, offset=0):
tk.c.org_dict = self._get_org_dict(organization_id)
return super(MetadataCollectionController, self).activity(id, offset)

def about(self, organization_id, id):
tk.c.org_dict = self._get_org_dict(organization_id)
return super(MetadataCollectionController, self).about(id)
2 changes: 2 additions & 0 deletions ckanext/metadata/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def metadata_collection_show_schema():
'extras': _extras_schema(),
'num_followers': [],
'package_count': [],
'display_name': [],
})
return schema

Expand Down Expand Up @@ -261,6 +262,7 @@ def infrastructure_show_schema():
schema.update({
'num_followers': [],
'package_count': [],
'display_name': [],
})
return schema

Expand Down
14 changes: 4 additions & 10 deletions ckanext/metadata/templates/metadata_collection/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
{% block subtitle %}{{ h.get_translated(c.group_dict, 'title') or c.group_dict.display_name }} - {{ _('Metadata Collections') }}{% endblock %}

{% block breadcrumb_content %}
<li>{% link_for _('Metadata Collections'), named_route='/metadata_collection' %}</li>
<li class="active">{% link_for h.get_translated(c.group_dict, 'title') or c.group_dict.display_name |truncate(35),
controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController', action='read', id=c.group_dict.name %}</li>
{% snippet "metadata_collection/snippets/breadcrumb_content_outer.html" %}
{% snippet "metadata_collection/snippets/breadcrumb_content_item.html" %}
{% endblock %}

{% block content_action %}
{% if h.check_access('metadata_collection_update', {'id': c.group_dict.id}) %}
{% link_for _('Manage'), controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='edit', id=c.group_dict.name, class_='btn', icon='wrench' %}
{% endif %}
{% snippet "metadata_collection/snippets/group_action_manage.html" %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('metadata_collection_read', _('Datasets'), id=c.group_dict.name) }}
{{ h.build_nav_icon('metadata_collection_activity', _('Activity Stream'), id=c.group_dict.name, offset=0) }}
{{ h.build_nav_icon('metadata_collection_about', _('About'), id=c.group_dict.name) }}
{% snippet "metadata_collection/snippets/group_content_nav.html" %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
{% block subtitle %}{{ h.get_translated(c.group_dict, 'title') or c.group_dict.display_name }} - {{ _('Metadata Collections') }}{% endblock %}

{% block breadcrumb_content %}
<li>{% link_for _('Metadata Collections'), named_route='/metadata_collection' %}</li>
<li class="active">{% link_for h.get_translated(c.group_dict, 'title') or c.group_dict.display_name |truncate(35),
controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController', action='read', id=c.group_dict.name %}</li>
{% snippet "metadata_collection/snippets/breadcrumb_content_outer.html" %}
{% snippet "metadata_collection/snippets/breadcrumb_content_item.html" %}
{% endblock %}

{% block content_action %}
{% if h.check_access('metadata_collection_update', {'id': c.group_dict.id}) %}
{% link_for _('Manage'), controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='edit', id=c.group_dict.name, class_='btn', icon='wrench' %}
{% endif %}
{% snippet "metadata_collection/snippets/group_action_manage.html" %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('metadata_collection_read', _('Datasets'), id=c.group_dict.name) }}
{{ h.build_nav_icon('metadata_collection_activity', _('Activity Stream'), id=c.group_dict.name, offset=0) }}
{{ h.build_nav_icon('metadata_collection_about', _('About'), id=c.group_dict.name) }}
{% snippet "metadata_collection/snippets/group_content_nav.html" %}
{% endblock %}
4 changes: 2 additions & 2 deletions ckanext/metadata/templates/metadata_collection/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block subtitle %}{{ _('Metadata Collections') }}{% endblock %}

{% block breadcrumb_content %}
<li class="active">{% link_for _('Metadata Collections'), named_route='/metadata_collection' %}</li>
{% snippet "metadata_collection/snippets/breadcrumb_content_outer.html" %}
{% endblock %}

{% block page_header %}{% endblock %}
Expand All @@ -22,7 +22,7 @@ <h1 class="hide-heading">{{ _('Metadata Collection') }}</h1>
{% block groups_list %}
{% if c.page.items or request.params %}
{% if c.page.items %}
{% snippet "group/snippets/group_list.html", groups=c.page.items %}
{% snippet "metadata_collection/snippets/group_list.html", groups=c.page.items, organization_id=c.org_dict.name %}
{% endif %}
{% else %}
<p class="empty">
Expand Down
14 changes: 4 additions & 10 deletions ckanext/metadata/templates/metadata_collection/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@
{% block subtitle %}{{ h.get_translated(c.group_dict, 'title') or c.group_dict.display_name }} - {{ _('Metadata Collections') }}{% endblock %}

{% block breadcrumb_content %}
<li>{% link_for _('Metadata Collections'), named_route='/metadata_collection' %}</li>
<li class="active">{% link_for h.get_translated(c.group_dict, 'title') or c.group_dict.display_name |truncate(35),
controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController', action='read', id=c.group_dict.name %}</li>
{% snippet "metadata_collection/snippets/breadcrumb_content_outer.html" %}
{% snippet "metadata_collection/snippets/breadcrumb_content_item.html" %}
{% endblock %}

{% block content_action %}
{% if h.check_access('metadata_collection_update', {'id': c.group_dict.id}) %}
{% link_for _('Manage'), controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='edit', id=c.group_dict.name, class_='btn', icon='wrench' %}
{% endif %}
{% snippet "metadata_collection/snippets/group_action_manage.html" %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('metadata_collection_read', _('Datasets'), id=c.group_dict.name) }}
{{ h.build_nav_icon('metadata_collection_activity', _('Activity Stream'), id=c.group_dict.name, offset=0) }}
{{ h.build_nav_icon('metadata_collection_about', _('About'), id=c.group_dict.name) }}
{% snippet "metadata_collection/snippets/group_content_nav.html" %}
{% endblock %}

{% block secondary_content %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li class="active">{% link_for h.get_translated(c.group_dict, 'title') or c.group_dict.display_name |truncate(35),
controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='read', id=c.group_dict.name, organization_id=c.org_dict.name %}</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<li>{% link_for _('Organizations'), controller='organization', action='index' %}</li>
<li>{% link_for c.org_dict.display_name|truncate(35), controller='organization', action='read', id=c.org_dict.name %}</li>
<li>{% link_for _('Metadata Collections'), controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='index', organization_id=c.org_dict.name %}</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% if h.check_access('metadata_collection_update', {'id': c.group_dict.id}) %}
{% link_for _('Manage'), controller='ckanext.metadata.controllers.metadata_collection:MetadataCollectionController',
action='edit', id=c.group_dict.name, organization_id=c.org_dict.name, class_='btn', icon='wrench' %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ h.build_nav_icon('metadata_collection_read', _('Datasets'), id=c.group_dict.name, organization_id=c.org_dict.name) }}
{{ h.build_nav_icon('metadata_collection_activity', _('Activity Stream'), id=c.group_dict.name, offset=0, organization_id=c.org_dict.name) }}
{{ h.build_nav_icon('metadata_collection_about', _('About'), id=c.group_dict.name, organization_id=c.org_dict.name) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{#
Renders a media item for a metadata collection. This should be used in a list.
Copied from ckan/templates/group/snippets/group_item.html; modified {% set url = ... %}.

group - A group dict.
organization_id - id/name of the owner org

#}
{% set url = h.url_for('metadata_collection_read', action='read', id=group.name, organization_id=organization_id) %}
{% block item %}
<li class="media-item">
{% block item_inner %}
{% block image %}
<img src="{{ group.image_display_url or h.url_for_static('/base/images/placeholder-group.png') }}" alt="{{ group.name }}" class="media-image">
{% endblock %}
{% block title %}
<h3 class="media-heading">{{ h.get_translated(group, 'title') or group.display_name }}</h3>
{% endblock %}
{% block description %}
{% if h.get_translated(group, 'description') %}
<p>{{ h.markdown_extract(h.get_translated(group, 'description'), extract_length=80) }}</p>
{% endif %}
{% endblock %}
{% block datasets %}
{% if group.package_count %}
<strong class="count">{{ ungettext('{num} Dataset', '{num} Datasets', group.package_count).format(num=group.package_count) }}</strong>
{% elif group.package_count == 0 %}
<span class="count">{{ _('0 Datasets') }}</span>
{% endif %}
{% endblock %}
{% block link %}
<a href="{{ url }}" title="{{ _('View {name}').format(name=h.get_translated(group, 'title') or group.display_name) }}" class="media-view">
<span>{{ _('View {name}').format(name=h.get_translated(group, 'title') or group.display_name) }}</span>
</a>
{% endblock %}
{% if group.user_member %}
<input name="group_remove.{{ group.id }}" value="{{ _('Remove') }}" type="submit" class="btn btn-danger btn-small media-edit" title="{{ _('Remove dataset from this group') }}"/>
{% endif %}
{% endblock %}
</li>
{% endblock %}
{% if position is divisibleby 3 %}
<li class="clearfix js-hide"></li>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "group/snippets/group_list.html" %}

{% block group_list_inner %}
{% for group in groups %}
{% snippet "metadata_collection/snippets/group_item.html", group=group, organization_id=organization_id, position=loop.index %}
{% endfor %}
{% endblock %}

0 comments on commit bd06ba6

Please sign in to comment.