From 1aaf0eaa4c5dca4609feacb3ae524a515bfdc6e8 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Wed, 6 Nov 2013 16:17:52 +0100 Subject: [PATCH] [FIX] Multiple fixes to url_for url_for now accepts a path or an uri correctly handling shemeless uri's url params are shadowed by keep_query bzr revid: fme@openerp.com-20131106151752-sdov6zynz2g80uro --- addons/website/models/website.py | 34 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index acd42f2051f..d04d25e64fb 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -6,7 +6,7 @@ import math import simplejson import traceback import urllib -from urlparse import urljoin +import urlparse import werkzeug import werkzeug.exceptions @@ -53,29 +53,33 @@ def auth_method_public(): request.uid = request.session.uid http.auth_methods['public'] = auth_method_public -def url_for(path, lang=None, keep_query=None): - if request: - path = urljoin(request.httprequest.path, path) +def url_for(path_or_uri, lang=None, keep_query=None): + location = path_or_uri.strip() + url = urlparse.urlparse(location) + if request and not url.netloc and not url.scheme: + location = urlparse.urljoin(request.httprequest.path, location) langs = request.context.get('langs') - if path[0] == '/' and (len(langs) > 1 or lang): - ps = path.split('/') + if location[0] == '/' and (len(langs) > 1 or lang): + ps = location.split('/') lang = lang or request.context.get('lang') if ps[1] in langs: ps[1] = lang else: ps.insert(1, lang) - path = '/'.join(ps) + location = '/'.join(ps) if keep_query: - keep = [] - params = werkzeug.url_decode(request.httprequest.query_string) - params_keys = tuple(params.keys()) + url = urlparse.urlparse(location) + location = url.path + params = werkzeug.url_decode(url.query) + query_params = frozenset(werkzeug.url_decode(request.httprequest.query_string).keys()) for kq in keep_query: - keep += fnmatch.filter(params_keys, kq) - if keep: - params = dict([(k, params[k]) for k in keep]) - path += u'?%s' % werkzeug.urls.url_encode(params) + for param in fnmatch.filter(query_params, kq): + params[param] = request.params[param] + params = werkzeug.urls.url_encode(params) + if params: + location += '?%s' % params - return path + return location def urlplus(url, params): if not params: