From 8cc293cf0646f563e9fcb91265815acc4a9cfb00 Mon Sep 17 00:00:00 2001 From: John Glover Date: Thu, 31 Oct 2013 18:41:52 +0100 Subject: [PATCH 1/3] [#1251] PEP8 --- ckan/lib/datapreview.py | 7 ++++--- ckan/logic/action/get.py | 2 ++ ckan/logic/schema.py | 9 +++++---- ckan/plugins/interfaces.py | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ckan/lib/datapreview.py b/ckan/lib/datapreview.py index ff850da76fb..9fb8bb712e5 100644 --- a/ckan/lib/datapreview.py +++ b/ckan/lib/datapreview.py @@ -122,12 +122,13 @@ def get_preview_plugin(data_dict, return_first=False): key=lambda x: x['quality'])['plugin'] return preview_plugin -def get_view_plugin(view_type): +def get_view_plugin(view_type): + ''' + Returns the IResourceView plugin associated with the given view_type. + ''' for plugin in p.PluginImplementations(p.IResourceView): info = plugin.info() name = info.get('name') if name == view_type: return plugin - - diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index c41f7b5b89a..ccb4861d6ad 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -903,6 +903,7 @@ def resource_view_show(context, data_dict): _check_access('resource_view_show', context, data_dict) return model_dictize.resource_view_dictize(resource_view, context) + def resource_view_list(context, data_dict): ''' Return the metadata of a resource_view. @@ -923,6 +924,7 @@ def resource_view_list(context, data_dict): resource_views = q.order_by(model.ResourceView.order).all() return model_dictize.resource_view_list_dictize(resource_views, context) + def resource_status_show(context, data_dict): '''Return the statuses of a resource's tasks. diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py index 8eb3fdd62cd..148f09170ff 100644 --- a/ckan/logic/schema.py +++ b/ckan/logic/schema.py @@ -589,8 +589,9 @@ def default_resource_view_schema(): def default_update_resource_view_schema(): schema = default_resource_view_schema() - schema.update({'id': [not_missing, not_empty, unicode]}) - schema.update({'resource_id': [ignore_missing, resource_id_exists ]}) - schema.update({'view_type': [ignore]}) #can not change after create + schema.update({ + 'id': [not_missing, not_empty, unicode], + 'resource_id': [ignore_missing, resource_id_exists], + 'view_type': [ignore] # can not change after create + }) return schema - diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py index 8a28152af86..4a7926177f6 100644 --- a/ckan/plugins/interfaces.py +++ b/ckan/plugins/interfaces.py @@ -253,6 +253,7 @@ def preview_template(self, context, data_dict): The ``data_dict`` contains the resource and the package. ''' + class IResourcePreview(Interface): ''' For backwards compatibility with the old resource preview code. ''' From e6ba1e987f2db6c4756e87897cf2dab81ef0e34c Mon Sep 17 00:00:00 2001 From: John Glover Date: Thu, 31 Oct 2013 18:46:11 +0100 Subject: [PATCH 2/3] [#1251] Show resource views on resource read page. Still unstyled. --- ckan/controllers/package.py | 25 +++++++++++-------- ckan/lib/helpers.py | 15 +++++++++++ ckan/templates/package/resource_read.html | 10 +++++--- .../package/snippets/resource_item.html | 4 +-- .../package/snippets/resource_view.html | 6 +++++ 5 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 ckan/templates/package/snippets/resource_view.html diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index b0adf5c5ac5..f6b4a3f8748 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -381,8 +381,12 @@ def read(self, id, format='html'): # can the resources be previewed? for resource in c.pkg_dict['resources']: - resource['can_be_previewed'] = self._resource_preview( - {'resource': resource, 'package': c.pkg_dict}) + try: + get_action('resource_view_list')( + context, {'id': resource['id']}) + resource['has_views'] = True + except NotFound: + resource['has_views'] = False self._setup_template_variables(context, {'id': id}, package_type=package_type) @@ -1194,15 +1198,16 @@ def resource_read(self, id, resource_id): c.related_count = c.pkg.related_count - c.resource['can_be_previewed'] = self._resource_preview( - {'resource': c.resource, 'package': c.package}) - return render('package/resource_read.html') + vars = {} + try: + vars['resource_views'] = get_action('resource_view_list')( + context, {'id': resource_id}) + c.resource['has_views'] = True + except NotFound: + vars['resource_views'] = [] + c.resource['has_views'] = False - def _resource_preview(self, data_dict): - return bool(datapreview.res_format(data_dict['resource']) - in datapreview.direct() + datapreview.loadable() - or datapreview.get_preview_plugin( - data_dict, return_first=True)) + return render('package/resource_read.html', extra_vars=vars) def resource_download(self, id, resource_id): """ diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index edd9746f0a8..27741392c04 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -1614,6 +1614,20 @@ def resource_preview(resource, package): raw_resource_url=resource.get('url')) +def rendered_resource_view(resource_view, resource, package): + ''' + Returns a rendered resource view snippet. + ''' + view_plugin = datapreview.get_view_plugin(resource_view['view_type']) + context = {} + data_dict = {'resource_view': resource_view, + 'resource': resource, + 'package': package} + vars = view_plugin.setup_template_variables(context, data_dict) or {} + template = view_plugin.preview_template(context, data_dict) + return snippet(template, **vars) + + def list_dict_filter(list_, search_field, output_field, value): ''' Takes a list of dicts and returns the value of a given key if the item has a matching value for a supplied key @@ -1816,6 +1830,7 @@ def get_site_statistics(): 'render_markdown', 'format_resource_items', 'resource_preview', + 'rendered_resource_view', 'SI_number_span', 'localised_number', 'localised_SI_number', diff --git a/ckan/templates/package/resource_read.html b/ckan/templates/package/resource_read.html index 46406746a6f..638a3dd95ab 100644 --- a/ckan/templates/package/resource_read.html +++ b/ckan/templates/package/resource_read.html @@ -36,7 +36,7 @@ {{ _('View') }} {% elif res.resource_type == 'api' %} {{ _('API Endpoint') }} - {% elif not res.can_be_previewed %} + {% elif not res.has_views %} {{ _('Go to resource') }} {% else %} {{ _('Download') }} @@ -72,8 +72,12 @@

{{ _('From the dataset abstract') }}

{% endblock %} - {% block data_preview %} - {{ h.resource_preview(c.resource, c.package) }} + {% block resource_views %} +
+ {% for resource_view in resource_views %} + {% snippet 'package/snippets/resource_view.html', resource_view=resource_view, resource=c.resource, package=c.package %} + {% endfor %} +
{% endblock %} {% endblock %} diff --git a/ckan/templates/package/snippets/resource_item.html b/ckan/templates/package/snippets/resource_item.html index c5b22521d64..d543282e025 100644 --- a/ckan/templates/package/snippets/resource_item.html +++ b/ckan/templates/package/snippets/resource_item.html @@ -28,7 +28,7 @@ {% block resource_item_explore_links %}
  • - {% if res.can_be_previewed %} + {% if res.has_views %} {{ _('Preview') }} {% else %} @@ -39,7 +39,7 @@
  • - {% if res.can_be_previewed %} + {% if res.has_views %} {{ _('Download') }} {% else %} diff --git a/ckan/templates/package/snippets/resource_view.html b/ckan/templates/package/snippets/resource_view.html new file mode 100644 index 00000000000..537b723ce46 --- /dev/null +++ b/ckan/templates/package/snippets/resource_view.html @@ -0,0 +1,6 @@ +{% set rendered_resource_view = h.rendered_resource_view(resource_view, resource, package) %} + +

    {{resource_view['title']}}

    +
    {{resource_view['description']}}
    + +{{rendered_resource_view}} From 5737dcd5998a2bdba1421d51778be48ad16532a9 Mon Sep 17 00:00:00 2001 From: John Glover Date: Thu, 31 Oct 2013 18:46:55 +0100 Subject: [PATCH 3/3] [#1251] Update ckanext.imageview --- ckanext/imageview/plugin.py | 16 ++++------------ .../imageview/theme/templates/image_view.html | 1 + 2 files changed, 5 insertions(+), 12 deletions(-) create mode 100644 ckanext/imageview/theme/templates/image_view.html diff --git a/ckanext/imageview/plugin.py b/ckanext/imageview/plugin.py index 3289058a8fd..f7b54349478 100644 --- a/ckanext/imageview/plugin.py +++ b/ckanext/imageview/plugin.py @@ -3,22 +3,15 @@ import ckan.plugins as p from ckan.lib.navl.validators import ignore_empty -from ckan.common import json - log = logging.getLogger(__name__) -try: - import ckanext.resourceproxy.plugin as proxy -except ImportError: - pass - DEFAULT_IMAGE_FORMATS = ['png', 'jpeg', 'jpg', 'gif'] class ImageView(p.SingletonPlugin): '''This extenstion makes views of images''' - p.implements(p.IConfigurable, inherit=True) + p.implements(p.IConfigurer, inherit=True) p.implements(p.IResourceView, inherit=True) def update_config(self, config): @@ -27,14 +20,13 @@ def update_config(self, config): def info(self): return {'name': 'image', 'schema': {'image_url': [ignore_empty, unicode]}, - 'form_template': 'image_form.html' - } + 'form_template': 'image_form.html'} def can_preview(self, data_dict): return True def setup_template_variables(self, context, data_dict): - return + return {'image_url': data_dict['resource_view']['image_url']} def preview_template(self, context, data_dict): - return 'image.html' + return 'image_view.html' diff --git a/ckanext/imageview/theme/templates/image_view.html b/ckanext/imageview/theme/templates/image_view.html new file mode 100644 index 00000000000..45eeb18e3c3 --- /dev/null +++ b/ckanext/imageview/theme/templates/image_view.html @@ -0,0 +1 @@ +