示例#1
0
文件: helpers.py 项目: zydio/ckan
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.

    from pylons import request

    default_locale = False
    locale = kw.pop('locale', None)
    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 default_locale:
        url = '%s%s' % (root, 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)
    return url
示例#2
0
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 = ''
    # ckan.root_path is defined when we have none standard language
    # position in the url
    root_path = config.get('ckan.root_path')
    if root_path:
        # 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 = '%s%s' % (root, url_to_amend)
    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':
        raise ckan.exceptions.CkanUrlException('There is a broken url being created %s' % kw)

    return url
示例#3
0
文件: helpers.py 项目: Brackets/ckan
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