Skip to content

Commit

Permalink
[#715] Remove helper and do work in controller and minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jun 26, 2013
1 parent b897af9 commit 18397d5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 35 deletions.
11 changes: 11 additions & 0 deletions ckan/controllers/package.py
Expand Up @@ -344,6 +344,11 @@ def read(self, id, format='html'):
c.current_package_id = c.pkg.id
c.related_count = c.pkg.related_count

# 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})

self._setup_template_variables(context, {'id': id},
package_type=package_type)

Expand Down Expand Up @@ -1142,8 +1147,14 @@ def resource_read(self, id, resource_id):
c.datastore_api = '%s/api/action' % config.get('ckan.site_url', '').rstrip('/')

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')

def _resource_preview(self, data_dict):
return bool(datapreview.get_preview_plugin(data_dict, return_first=True))

def resource_download(self, id, resource_id):
"""
Provides a direct download by redirecting the user to the url stored
Expand Down
7 changes: 6 additions & 1 deletion ckan/lib/datapreview.py
Expand Up @@ -52,13 +52,16 @@ def _on_same_domain(data_dict):
return compare_domains([ckan_url, resource_url])


def get_preview_plugin(data_dict):
def get_preview_plugin(data_dict, return_first=False):
'''Determines whether there is an extension that can preview the resource.
:param data_dict: contains a resource and package dict.
The resource dict has to have a value for ``on_same_domain``
:type data_dict: dictionary
:param return_first: If True return the first plugin that can preview
:type return_first: bool
Returns a dict of plugins that can preview or ones that are fixable'''

data_dict['resource']['on_same_domain'] = _on_same_domain(data_dict)
Expand All @@ -76,6 +79,8 @@ def get_preview_plugin(data_dict):
p_info.update(data)
# if we can preview
if p_info['can_preview']:
if return_first:
plugin
plugins_that_can_preview.append(p_info)
elif p_info.get('fixable'):
plugins_fixable.append(p_info)
Expand Down
31 changes: 4 additions & 27 deletions ckan/lib/helpers.py
Expand Up @@ -1486,29 +1486,7 @@ def format_resource_items(items):
return sorted(output, key=lambda x: x[0])


def can_be_previewed(resource, package):
data_dict = {'resource': resource, 'package': package}
if datapreview.can_be_previewed(data_dict):
return True

format_lower = resource['format'].lower()

if resource['url']:

direct_embed = config.get('ckan.preview.direct', '').split()
if not direct_embed:
direct_embed = datapreview.DEFAULT_DIRECT_EMBED
loadable_in_iframe = config.get('ckan.preview.loadable', '').split()
if not loadable_in_iframe:
loadable_in_iframe = datapreview.DEFAULT_LOADABLE_IFRAME

return (format_lower in direct_embed or
format_lower in loadable_in_iframe)

return False


def resource_preview(resource, pkg_id):
def resource_preview(resource, package):
'''
Returns a rendered snippet for a embedded resource preview.
Expand All @@ -1521,7 +1499,7 @@ def resource_preview(resource, pkg_id):
directly = False
url = ''

data_dict = {'resource': resource, 'package': c.package}
data_dict = {'resource': resource, 'package': package}

if not resource['url']:
return snippet("dataviewer/snippets/no_preview.html",
Expand All @@ -1534,9 +1512,9 @@ def resource_preview(resource, pkg_id):
if not loadable_in_iframe:
loadable_in_iframe = datapreview.DEFAULT_LOADABLE_IFRAME

if datapreview.get_preview_plugin(data_dict):
if datapreview.get_preview_plugin(data_dict, return_first=True):
url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], id=pkg_id, qualified=True)
resource_id=resource['id'], id=package['id'], qualified=True)
elif format_lower in direct_embed:
directly = True
url = resource['url']
Expand Down Expand Up @@ -1676,7 +1654,6 @@ def SI_number_span(number):
'render_markdown',
'format_resource_items',
'resource_preview',
'can_be_previewed',
'SI_number_span',
'localised_number',
'localised_SI_number',
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/resource_read.html
Expand Up @@ -36,7 +36,7 @@
<i class="icon-eye-open"></i> {{ _('View') }}
{% elif res.resource_type == 'api' %}
<i class="icon-key"></i> {{ _('API Endpoint') }}
{% elif not h.can_be_previewed(res, pkg) %}
{% elif not res.can_be_previewed %}
<i class="icon-external-link"></i> {{ _('Go to resource') }}
{% else %}
<i class="icon-download"></i> {{ _('Download') }}
Expand Down Expand Up @@ -73,7 +73,7 @@ <h3>{{ _('From the dataset abstract') }}</h3>
</div>
</div>
{% block data_preview %}
{{ h.resource_preview(c.resource, c.package.id) }}
{{ h.resource_preview(c.resource, c.package) }}
{% endblock %}
</section>
{% endblock %}
Expand Down
5 changes: 2 additions & 3 deletions ckan/templates/package/snippets/resource_item.html
@@ -1,5 +1,4 @@
{% set url = h.url_for(controller='package', action='resource_read', id=pkg.name, resource_id=res.id) %}
{% set can_be_previewed = h.can_be_previewed(resource=res, package=pkg) %}
<li class="resource-item">
{% block resource_item_title %}
<a class="heading" href="{{ url }}" title="{{ res.name or res.description }}">
Expand All @@ -25,7 +24,7 @@
{% block resource_item_explore_links %}
<li>
<a href="{{ url }}">
{% if can_be_previewed %}
{% if res.can_be_previewed %}
<i class="icon-bar-chart"></i>
{{ _('Preview') }}
{% else %}
Expand All @@ -36,7 +35,7 @@
</li>
<li>
<a href="{{ res.url }}" class="resource-url-analytics" target="_blank">
{% if can_be_previewed %}
{% if res.can_be_previewed %}
<i class="icon-download"></i>
{{ _('Download') }}
{% else %}
Expand Down
3 changes: 1 addition & 2 deletions ckan/templates_legacy/package/resource_read.html
Expand Up @@ -146,8 +146,7 @@
<div class="resource-preview">
<div class="preview-header">
<h3>Preview</h3>

${ h.resource_preview(c.resource, c.package.id) }
${ h.resource_preview(c.resource, c.package) }
<a py:if="c.pkg.private" title="Cannot embed as resource is private." style="display: none;" class="btn disabled" data-toggle="modal" href=".modal-data-viewer-embed-dialog">Embed</a>
<a py:if="not c.pkg.private" style="display: none;" class="btn btn-primary" data-toggle="modal" href=".modal-data-viewer-embed-dialog">Embed</a>
</div>
Expand Down

0 comments on commit 18397d5

Please sign in to comment.