[FIX] url_for()

bzr revid: fme@openerp.com-20140130195043-pkfkmqg80c2yk5p5
This commit is contained in:
Fabien Meghazi 2014-01-30 20:50:43 +01:00
parent 60fc222a18
commit fc979b9a7a
2 changed files with 25 additions and 12 deletions

View File

@ -5,7 +5,6 @@ import itertools
import logging import logging
import math import math
import re import re
import urllib
import urlparse import urlparse
import simplejson import simplejson
@ -38,24 +37,38 @@ def keep_query(*args, **kw):
def url_for(path_or_uri, lang=None): def url_for(path_or_uri, lang=None):
location = path_or_uri.strip() location = path_or_uri.strip()
force_lang = lang is not None
url = urlparse.urlparse(location) url = urlparse.urlparse(location)
if request and url.path and not url.netloc and not url.scheme:
if request and not url.netloc and not url.scheme and (url.path or force_lang):
location = urlparse.urljoin(request.httprequest.path, location) location = urlparse.urljoin(request.httprequest.path, location)
force_lang = lang is not None
lang = lang or request.context.get('lang') lang = lang or request.context.get('lang')
langs = [lg[0] for lg in request.website.get_languages()] langs = [lg[0] for lg in request.website.get_languages()]
if lang != request.website.default_lang_code and (force_lang or (location[0] == '/' and len(langs) > 1)):
if is_multilang_url(location): if (len(langs) > 1 or force_lang) and is_multilang_url(location, langs):
ps = location.split('/') ps = location.split('/')
if ps[1] in langs: if ps[1] in langs:
# Replace the language only if we explicitly provide a language to url_for
if force_lang:
ps[1] = lang ps[1] = lang
else: # Remove the default language unless it's explicitly provided
ps.insert(1, lang) elif ps[1] == request.website.default_lang_code:
location = '/'.join(ps) ps.pop(1)
# Insert the context language or the provided language
elif lang != request.website.default_lang_code or force_lang:
ps.insert(1, lang)
location = '/'.join(ps)
return location return location
def is_multilang_url(path): def is_multilang_url(path, langs=None):
if not langs:
langs = [lg[0] for lg in request.website.get_languages()]
spath = path.split('/')
# if a language is already in the path, remove it
if spath[1] in langs:
spath.pop(1)
path = '/'.join(spath)
try: try:
router = request.httprequest.app.get_db_router(request.db).bind('') router = request.httprequest.app.get_db_router(request.db).bind('')
func = router.match(path)[0] func = router.match(path)[0]

View File

@ -16,7 +16,7 @@
((submenu.url != '/' and request.httprequest.path.startswith(submenu.url)) or ((submenu.url != '/' and request.httprequest.path.startswith(submenu.url)) or
request.httprequest.path == submenu.url) and 'active' request.httprequest.path == submenu.url) and 'active'
"> ">
<a t-att-href="url_for(submenu.url)" t-ignore="true" t-att-target="'blank' if submenu.new_window else None"> <a t-att-href="submenu.url" t-ignore="true" t-att-target="'blank' if submenu.new_window else None">
<span t-field="submenu.name"/> <span t-field="submenu.name"/>
</a> </a>
</li> </li>