def url_for(*args, **kw): '''Return the URL for the given controller, action, id, etc. Usage:: import ckan.plugins.toolkit as toolkit url = toolkit.url_for(controller='package', action='read', id='my_dataset') => returns '/dataset/my_dataset' Or, using a named route:: toolkit.url_for('dataset_read', id='changed') This is a wrapper for :py:func:`routes.url_for` that adds some extra features that CKAN needs. ''' locale = kw.pop('locale', None) # remove __ckan_no_root and add after to not pollute url no_root = kw.pop('__ckan_no_root', False) # routes will get the wrong url for APIs if the ver is not provided if kw.get('controller') == 'api': ver = kw.get('ver') if not ver: 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)
def url_for_static(*args, **kw): """Create url for static content that does not get translated eg css, js wrapper for routes.url_for""" # make sure that if we specify the url that it is not unicode if args: args = (str(args[0]), ) + args[1:] my_url = _routes_default_url_for(*args, **kw) return my_url
def url_for_static(*args, **kw): """Create url for static content that does not get translated eg css, js wrapper for routes.url_for""" # make sure that if we specify the url that it is not unicode if args: args = (str(args[0]),) + args[1:] my_url = _routes_default_url_for(*args, **kw) return my_url
def url_for(*args, **kw): """Create url adding i18n information if selected wrapper for routes.url_for""" locale = kw.pop('locale', None) # remove __ckan_no_root and add after to not pollute url no_root = kw.pop('__ckan_no_root', False) # routes will get the wrong url for APIs if the ver is not provided if kw.get('controller') == 'api': ver = kw.get('ver') if not ver: raise Exception('api calls must specify the version! e.g. ver=1') # fix ver to include the slash kw['ver'] = '/%s' % ver my_url = _routes_default_url_for(*args, **kw) kw['__ckan_no_root'] = no_root return _add_i18n_to_url(my_url, locale=locale, **kw)
def url_for_logo(*args, **kw): def fix_arg(arg): url = urlparse.urlparse(str(arg)) url_is_relative = ( url.scheme == '' and url.netloc == '' and not url.path.startswith('/') ) if url_is_relative: return '/' + url.geturl() return url.geturl() if args: args = (fix_arg(args[0]), ) + args[1:] my_url = _routes_default_url_for(*args, **kw) return my_url
def url_for(*args, **kw): """Create url adding i18n information if selected wrapper for routes.url_for""" locale = kw.pop("locale", None) # remove __ckan_no_root and add after to not pollute url no_root = kw.pop("__ckan_no_root", False) # routes will get the wrong url for APIs if the ver is not provided if kw.get("controller") == "api": ver = kw.get("ver") if not ver: raise Exception("api calls must specify the version! e.g. ver=1") # fix ver to include the slash kw["ver"] = "/%s" % ver my_url = _routes_default_url_for(*args, **kw) kw["__ckan_no_root"] = no_root return _add_i18n_to_url(my_url, locale=locale, **kw)
def _add_i18n_to_url(url_to_amend, **kw): # If the locale keyword param is provided then the url is rewritten # using that locale .If return_to is provided this is used as the url # (as part of the language changing feature). # A locale of default will not add locale info to the url. default_locale = False locale = kw.pop('locale', None) no_root = kw.pop('__ckan_no_root', False) allowed_locales = ['default'] + i18n.get_locales() if locale and locale not in allowed_locales: locale = None if locale: if locale == 'default': default_locale = True else: try: locale = request.environ.get('CKAN_LANG') default_locale = request.environ.get('CKAN_LANG_IS_DEFAULT', True) except TypeError: default_locale = True try: root = request.environ.get('SCRIPT_NAME', '') except TypeError: 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] + root # ckan.root_path is defined when we have none standard language # position in the url root_path = config.get('ckan.root_path', None) if root_path: # FIXME this can be written better once the merge # 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) else: root = 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) else: if default_locale: url = url_to_amend else: # we need to strip the root from the url and the add it before # the language specification. url = url_to_amend[len(root):] url = '%s/%s%s' % (root, locale, url) # stop the root being added twice in redirects if no_root: url = url_to_amend[len(root):] if not default_locale: url = '/%s%s' % (locale, url) if url == '/packages': error = 'There is a broken url being created %s' % kw raise ckan.exceptions.CkanUrlException(error) return url
def url_for_static(*args, **kw): """Create url for static content that does not get translated eg css, js wrapper for routes.url_for""" my_url = _routes_default_url_for(*args, **kw) return my_url
def url_for(*args, **kw): """Create url adding i18n information if selected wrapper for routes.url_for""" locale = kw.pop('locale', None) my_url = _routes_default_url_for(*args, **kw) return _add_i18n_to_url(my_url, locale=locale, **kw)