Skip to content

Commit

Permalink
Merge pull request #2599 from opendatazurich/2592-fix-absolute-uri-wh…
Browse files Browse the repository at this point in the history
…en-uploading-master

Use `ckan.site_url` to generate urls of resources
  • Loading branch information
wardi committed Sep 3, 2015
2 parents 88c7045 + 4dac041 commit 9cc0596
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
44 changes: 36 additions & 8 deletions ckan/lib/helpers.py
Expand Up @@ -110,6 +110,28 @@ def url(*args, **kw):
return _add_i18n_to_url(my_url, locale=locale, **kw)


def get_site_protocol_and_host():
'''Return the protocol and host of the configured `ckan.site_url`.
This is needed to generate valid, full-qualified URLs.
If `ckan.site_url` is set like this::
ckan.site_url = http://example.com
Then this function would return a tuple `('http', 'example.com')`
If the setting is missing, `(None, None)` is returned instead.
'''
site_url = config.get('ckan.site_url', None)
if site_url is not None:
parsed_url = urlparse.urlparse(site_url)
return (
parsed_url.scheme.encode('utf-8'),
parsed_url.netloc.encode('utf-8')
)
return (None, None)


def url_for(*args, **kw):
'''Return the URL for the given controller, action, id, etc.
Expand Down Expand Up @@ -139,6 +161,8 @@ def url_for(*args, **kw):
raise Exception('api calls must specify the version! e.g. ver=3')
# fix ver to include the slash
kw['ver'] = '/%s' % ver
if kw.get('qualified', False):
kw['protocol'], kw['host'] = get_site_protocol_and_host()
my_url = _routes_default_url_for(*args, **kw)
kw['__ckan_no_root'] = no_root
return _add_i18n_to_url(my_url, locale=locale, **kw)
Expand Down Expand Up @@ -222,7 +246,11 @@ def _add_i18n_to_url(url_to_amend, **kw):
root = ''
if kw.get('qualified', False):
# if qualified is given we want the full url ie http://...
root = _routes_default_url_for('/', qualified=True)[:-1]
protocol, host = get_site_protocol_and_host()
root = _routes_default_url_for('/',
qualified=True,
host=host,
protocol=protocol)[:-1]
# ckan.root_path is defined when we have none standard language
# position in the url
root_path = config.get('ckan.root_path', None)
Expand All @@ -231,15 +259,15 @@ def _add_i18n_to_url(url_to_amend, **kw):
# into the ecportal core is done - Toby
# we have a special root specified so use that
if default_locale:
root = re.sub('/{{LANG}}', '', root_path)
root_path = re.sub('/{{LANG}}', '', root_path)
else:
root = re.sub('{{LANG}}', locale, root_path)
root_path = re.sub('{{LANG}}', locale, root_path)
# make sure we don't have a trailing / on the root
if root[-1] == '/':
root = root[:-1]
url = url_to_amend[len(re.sub('/{{LANG}}', '', root_path)):]
url = '%s%s' % (root, url)
root = re.sub('/{{LANG}}', '', root_path)
if root_path[-1] == '/':
root_path = root_path[:-1]

url_path = url_to_amend[len(root):]
url = '%s%s%s' % (root, root_path, url_path)
else:
if default_locale:
url = url_to_amend
Expand Down
69 changes: 69 additions & 0 deletions ckan/tests/lib/test_helpers.py
@@ -1,7 +1,9 @@
import nose
import i18n

import ckan.lib.helpers as h
import ckan.exceptions
from ckan.tests import helpers

eq_ = nose.tools.eq_
CkanUrlException = ckan.exceptions.CkanUrlException
Expand Down Expand Up @@ -55,6 +57,73 @@ def test_url_for_static_or_external_works_with_protocol_relative_url(self):
eq_(h.url_for_static_or_external(url), url)


class TestHelpersUrlFor(object):

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_default(self):
url = '/dataset/my_dataset'
generated_url = h.url_for(controller='package', action='read', id='my_dataset')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_with_locale(self):
url = '/de/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
locale='de')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_not_qualified(self):
url = '/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=False)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified(self):
url = 'http://example.com/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/prefix')
def test_url_for_qualified_with_root_path(self):
url = 'http://example.com/my/prefix/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified_with_locale(self):
url = 'http://example.com/de/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_qualified_with_root_path_and_locale(self):
url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)


class TestHelpersRenderMarkdown(object):

def test_render_markdown_allow_html(self):
Expand Down

0 comments on commit 9cc0596

Please sign in to comment.