summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorFabien Meghazi <fme@openerp.com>2014-01-30 20:50:43 +0100
committerFabien Meghazi <fme@openerp.com>2014-01-30 20:50:43 +0100
commitfc979b9a7a23bbfc8ea4367ecb6b00607eb13b72 (patch)
tree729f85d974d48efc65289b57354c3d64ad6d3a59 /addons
parent60fc222a180430929a544a7702c223b47b21ec9c (diff)
[FIX] url_for()
bzr revid: fme@openerp.com-20140130195043-pkfkmqg80c2yk5p5
Diffstat (limited to 'addons')
-rw-r--r--addons/website/models/website.py35
-rw-r--r--addons/website/views/website_templates.xml2
2 files changed, 25 insertions, 12 deletions
diff --git a/addons/website/models/website.py b/addons/website/models/website.py
index f16d31418f1..0c71508edf4 100644
--- a/addons/website/models/website.py
+++ b/addons/website/models/website.py
@@ -5,7 +5,6 @@ import itertools
import logging
import math
import re
-import urllib
import urlparse
import simplejson
@@ -38,24 +37,38 @@ def keep_query(*args, **kw):
def url_for(path_or_uri, lang=None):
location = path_or_uri.strip()
+ force_lang = lang is not None
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)
- force_lang = lang is not None
lang = lang or request.context.get('lang')
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):
- ps = location.split('/')
- if ps[1] in langs:
+
+ if (len(langs) > 1 or force_lang) and is_multilang_url(location, langs):
+ ps = location.split('/')
+ if ps[1] in langs:
+ # Replace the language only if we explicitly provide a language to url_for
+ if force_lang:
ps[1] = lang
- else:
- ps.insert(1, lang)
- location = '/'.join(ps)
+ # Remove the default language unless it's explicitly provided
+ elif ps[1] == request.website.default_lang_code:
+ 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
-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:
router = request.httprequest.app.get_db_router(request.db).bind('')
func = router.match(path)[0]
diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml
index 05c3220544f..e34d1e5925a 100644
--- a/addons/website/views/website_templates.xml
+++ b/addons/website/views/website_templates.xml
@@ -16,7 +16,7 @@
((submenu.url != '/' and request.httprequest.path.startswith(submenu.url)) or
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"/>
</a>
</li>