Skip to content

Commit

Permalink
Simplified plugins interface
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Oct 26, 2012
1 parent d2628c2 commit 558138d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
3 changes: 2 additions & 1 deletion ckan/controllers/package.py
Expand Up @@ -1334,7 +1334,6 @@ def resource_datapreview(self, id, resource_id):
c.resource = get_action('resource_show')(context,
{'id': resource_id})
c.package = get_action('package_show')(context, {'id': id})
c.resource_json = json.dumps(c.resource)

plugins = ckanplugins.PluginImplementations(ckanplugins.IResourcePreview)
plugins_that_can_preview = [p for p in plugins if p.can_preview(c.resource)]
Expand All @@ -1346,6 +1345,8 @@ def resource_datapreview(self, id, resource_id):
plugin = plugins_that_can_preview[0]
plugin.setup_template_variables(context, {'resource': c.resource, 'package': c.package})

c.resource_json = json.dumps(c.resource)

except NotFound:
abort(404, _('Resource not found'))
except NotAuthorized:
Expand Down
34 changes: 14 additions & 20 deletions ckan/lib/helpers.py
Expand Up @@ -1265,19 +1265,23 @@ def format_resource_items(items):
return sorted(output, key=lambda x: x[0])


def _can_be_previewed(resource, is_same_domain):
def _can_be_previewed(resource):
'''
Determines whether there is an extension that can preview the resource.
:param resource: a resource dictionary
:param is_same_domain: True if the resource is on the
same domain (same url as CKAN itself)
'''
# compare CKAN domain and resource URL
import ckan.plugins.toolkit as toolkit
ckan_domain = toolkit.request.environ['HTTP_HOST'].lower()
request_protocol = toolkit.request.environ['SERVER_PROTOCOL'].lower()

parsed = urlparse.urlparse(resource['url'])
resource_domain = (parsed.hostname + ':' + str(parsed.port)).lower()

resource['on_same_domain'] = (ckan_domain == resource_domain
and parsed.scheme.lower() in request_protocol)

plugins = ckanplugins.PluginImplementations(ckanplugins.IResourcePreview)
for plugin in plugins:
if plugin.can_preview(resource) and (is_same_domain or not plugin.requires_same_orign(resource)):
return True
return False
return any(plugin.can_preview(resource) for plugin in plugins)


def resource_preview(resource, pkg_id):
Expand All @@ -1298,17 +1302,7 @@ def resource_preview(resource, pkg_id):
directly = False
url = ''

# compare CKAN domain and resource URL
import ckan.plugins.toolkit as toolkit
ckan_domain = toolkit.request.environ['HTTP_HOST'].lower()
request_protocol = toolkit.request.environ['SERVER_PROTOCOL'].lower()

parsed = urlparse.urlparse(resource['url'])
resource_domain = (parsed.hostname + ':' + str(parsed.port)).lower()

is_same_domain = ckan_domain == resource_domain and parsed.scheme.lower() in request_protocol

if _can_be_previewed(resource, is_same_domain):
if _can_be_previewed(resource):
url = url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], id=pkg_id, qualified=True)
elif format_lower in DIRECT_EMBEDS:
Expand Down
10 changes: 5 additions & 5 deletions ckan/plugins/interfaces.py
Expand Up @@ -201,17 +201,17 @@ class IResourcePreview(Interface):
def can_preview(self, resource):
'''
Return True if the extension can preview the resource.
'''
def requires_same_orign(self, resource):
'''
Return True if the extension cannot preview the resource from
a different domain because of the same origin policy.
Make sure you also make sure to ckeck the ``on_same_domain`` value of the
resource or the url if your preview requires the resource to be on
the same domain because of the same origin policy.
'''

def setup_template_variables(self, context, data_dict):
'''
Add variables to c just prior to the template being rendered.
Change the url to a proxied domain if necessary.
'''

def preview_template(self, context):
Expand Down

0 comments on commit 558138d

Please sign in to comment.