Skip to content

Commit

Permalink
[#1251] view proxy sort out
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Nov 4, 2013
1 parent 7f138ef commit 0ddeec8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
8 changes: 8 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -1642,6 +1642,14 @@ def rendered_resource_view(resource_view, resource, package):
return snippet(template, **data_dict)


def view_resource_url(resource_view, resource, package, **kw):
'''
Returns url for resource. made to be overridden by extensions. i.e
by resource proxy.
'''
return resource['url']


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
Expand Down
2 changes: 1 addition & 1 deletion ckanext/imageview/theme/templates/image_view.html
@@ -1 +1 @@
<img src="{{resource_view.get('image_url') or resource.get('url')}}" />
<img src="{{ resource_view.get('image_url') or h.view_resource_url(resource_view, resource, package, proxy_schemes=['http']) }}" />
43 changes: 33 additions & 10 deletions ckanext/resourceproxy/plugin.py
Expand Up @@ -2,21 +2,32 @@

import ckan.lib.helpers as h
import ckan.plugins as p
import ckan.lib.datapreview as datapreview
import urlparse
from pylons import config

log = getLogger(__name__)


def get_proxified_resource_url(data_dict):
def get_proxified_resource_url(data_dict, proxy_schemes=['http','https']):

This comment has been minimized.

Copy link
@vitorbaptista

vitorbaptista Dec 17, 2013

Contributor

This breaks the test in https://github.com/okfn/ckan/blob/1251-resource-view/ckanext/resourceproxy/tests/test_proxy.py#L132-L139, because the javascript scheme isn't proxied. Then it gets to webtest's lint which fails when trying to get an URL which path doesn't start with a slash (as in http://google.com).

AFAIK, it doesn't make much sense to proxy javascript "URLs", so I guess the problem is with the test (or with webtest). I'm not sure how to fix it, though.

Ideas?

'''
:param data_dict: contains a resource and package dict
:type data_dict: dictionary
:param proxy_schemes: list of url schemes to proxy for.
:type data_dict: list
'''
url = h.url_for(
action='proxy_resource',
controller='ckanext.resourceproxy.controller:ProxyController',
id=data_dict['package']['name'],
resource_id=data_dict['resource']['id'])
log.info('Proxified url is {0}'.format(url))

ckan_url = config.get('ckan.site_url', '//localhost:5000')
url = data_dict['resource']['url']
scheme = urlparse.urlparse(url).scheme
compare_domains = datapreview.compare_domains
if not compare_domains([ckan_url, url]) and scheme in proxy_schemes:
url = h.url_for(
action='proxy_resource',
controller='ckanext.resourceproxy.controller:ProxyController',
id=data_dict['package']['name'],
resource_id=data_dict['resource']['id'])
log.info('Proxified url is {0}'.format(url))
return url


Expand All @@ -38,13 +49,25 @@ class ResourceProxy(p.SingletonPlugin):
``config.get('ckan.resource_proxy_enabled', False)``
"""
p.implements(p.IRoutes, inherit=True)
p.implements(p.IConfigurer, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)

def update_config(self, config):
config['ckan.resource_proxy_enabled'] = True

def before_map(self, m):
m.connect('/dataset/{id}/resource/{resource_id}/proxy',
controller='ckanext.resourceproxy.controller:ProxyController',
action='proxy_resource')
return m

def get_helpers(self):
return {'view_resource_url': self.view_resource_url}

def view_resource_url(self, resource_view, resource,
package, proxy_schemes=['http','https']):
'''
Returns the proxy url if its availiable
'''
data_dict = {'resource_view': resource_view,
'resource': resource,
'package': package}
return get_proxified_resource_url(data_dict,
proxy_schemes=proxy_schemes)

0 comments on commit 0ddeec8

Please sign in to comment.