Skip to content

Commit

Permalink
[#1002] Allow can_preview to return a dict
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jun 18, 2013
1 parent 0a9216b commit 9098fb9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
53 changes: 36 additions & 17 deletions ckan/controllers/package.py
Expand Up @@ -1338,27 +1338,46 @@ def resource_datapreview(self, id, resource_id):
on_same_domain = datapreview.resource_is_on_same_domain(data_dict)
data_dict['resource']['on_same_domain'] = on_same_domain

# FIXME this wants to not use plugins as it is an imported name
# and we already import it an p should really only be in
# extensu=ions in my opinion also just make it look nice and be
# readable grrrrrr
plugins = p.PluginImplementations(p.IResourcePreview)
plugins_that_can_preview = [plugin for plugin in plugins
if plugin.can_preview(data_dict)]
if len(plugins_that_can_preview) == 0:
plugins_that_can_preview = []
plugins_fixable = []
for plugin in p.PluginImplementations(p.IResourcePreview):
p_info = {'plugin': plugin, 'quality': 1}
data = plugin.can_preview(data_dict)
# old school plugins return true/False
if isinstance(data, bool):
p_info['can_preview'] = data
else:
# new school provide a dict
p_info.update(data)
# if we can preview
if p_info['can_preview']:
plugins_that_can_preview.append(p_info)
elif p_info.get('fixable'):
plugins_fixable.append(p_info)
num_plugins = len(plugins_that_can_preview)
if num_plugins == 0:
for plug in plugins_fixable:
log.info('%s would allow previews if %s' % (
plug['plugin'], plugin['fixable']))
abort(409, _('No preview has been defined.'))
if len(plugins_that_can_preview) > 1:
log.warn('Multiple previews are possible. {0}'.format(
plugins_that_can_preview))

plugin = plugins_that_can_preview[0]
plugin.setup_template_variables(context, data_dict)

elif num_plugins > 1:
preview_plugin = plugins_that_can_preview[0]['plugin']
else:
# multiple plugins
plugs = [pl['plugin'] for pl in plugins_that_can_preview]
log.warn('Multiple previews are possible. {0}'.format(plugs))

This comment has been minimized.

Copy link
@domoritz

domoritz Jun 18, 2013

Contributor

This should only be raised if there are multiple previews with the best quality.

This comment has been minimized.

Copy link
@tobes

tobes Jun 19, 2013

Author Contributor

yeah this is probably true

qual = max(plugins_that_can_preview, key=lambda x:x['quality'])

This comment has been minimized.

Copy link
@domoritz

domoritz Jun 18, 2013

Contributor

Do we need this here. I think max already returns the plugin dict.

> a = [{'a':2, 'b':5}, {'a':3, 'b':4}]
> max(a, key=lambda x: x['b'])
{'a': 2, 'b': 5}

This should be enough:

preview_plugin = max(plugins_that_can_preview,
                                key=lambda x: x['quality'])

Also, there is space missing between x:x.

# we are just grabing one of the best quality here
preview_plugins = [pl['plugin'] for pl in
plugins_that_can_preview
if pl['quality'] == qual]
preview_plugin = preview_plugins[0]

preview_plugin.setup_template_variables(context, data_dict)
c.resource_json = json.dumps(c.resource)

except NotFound:
abort(404, _('Resource not found'))
except NotAuthorized:
abort(401, _('Unauthorized to read resource %s') % id)
else:
return render(plugin.preview_template(context, data_dict))
return render(preview_plugin.preview_template(context, data_dict))
16 changes: 14 additions & 2 deletions ckan/plugins/interfaces.py
Expand Up @@ -202,8 +202,20 @@ class IResourcePreview(Interface):

def can_preview(self, data_dict):
'''
Return True if the extension can preview the resource. The ``data_dict``
contains the resource and the package.
Returns info on whether the plugin can preview the resource.
This can be done in two ways.
The old way is to just return True or False.
The new way is to return a dict with the following
{
'can_preview': bool - if the extension can preview the resource
'fixable': string - if the extension cannot preview but could for
example if the resource_proxy was enabled.
'quality': int - how good the preview is 1-poor, 2-average, 3-good
used if multiple extensions can preview
}
The ``data_dict`` contains the resource and the package.
Make sure to ckeck the ``on_same_domain`` value of the
resource or the url if your preview requires the resource to be on
Expand Down

0 comments on commit 9098fb9

Please sign in to comment.