[IMP] website: merge cache of is_multilang_url with url_rewrite
Fwd-port of 6e87ee0 closes odoo/odoo#64916 X-original-commit: 6e987e6f7f5e6422bb1d04ea8764ab298446ffb0 Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com> Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
This commit is contained in:
@@ -197,7 +197,6 @@ def url_for(url_from, lang_code=None, no_rewrite=False):
|
|||||||
new_url = request.env['ir.http'].url_rewrite(path)
|
new_url = request.env['ir.http'].url_rewrite(path)
|
||||||
new_url = new_url and qs and new_url + '?%s' % qs
|
new_url = new_url and qs and new_url + '?%s' % qs
|
||||||
|
|
||||||
|
|
||||||
return url_lang(new_url or url_from, lang_code=lang_code)
|
return url_lang(new_url or url_from, lang_code=lang_code)
|
||||||
|
|
||||||
|
|
||||||
@@ -224,23 +223,18 @@ def is_multilang_url(local_url, lang_url_codes=None):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
query_string = url[1] if len(url) > 1 else None
|
query_string = url[1] if len(url) > 1 else None
|
||||||
router = request.httprequest.app.get_db_router(request.db).bind('')
|
|
||||||
|
|
||||||
def is_multilang_func(func):
|
|
||||||
return (func and func.routing.get('website', False) and
|
|
||||||
func.routing.get('multilang', func.routing['type'] == 'http'))
|
|
||||||
|
|
||||||
|
|
||||||
# Try to match an endpoint in werkzeug's routing table
|
# Try to match an endpoint in werkzeug's routing table
|
||||||
try:
|
try:
|
||||||
func = router.match(path, method='POST', query_args=query_string)[0]
|
_, func = request.env['ir.http'].url_rewrite(path, query_args=query_string)
|
||||||
return is_multilang_func(func)
|
|
||||||
except werkzeug.exceptions.MethodNotAllowed:
|
# /page/xxx has no endpoint/func but is multilang
|
||||||
func = router.match(path, method='GET', query_args=query_string)[0]
|
return (not func or (
|
||||||
return is_multilang_func(func)
|
func.routing.get('website', False)
|
||||||
except werkzeug.exceptions.NotFound:
|
and func.routing.get('multilang', func.routing['type'] == 'http')
|
||||||
return True
|
))
|
||||||
except Exception:
|
except Exception as exception:
|
||||||
|
_logger.warning(exception)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -443,7 +437,7 @@ class IrHttp(models.AbstractModel):
|
|||||||
# most of the time the browser is loading and inexisting assets or image. A standard 404 is enough.
|
# most of the time the browser is loading and inexisting assets or image. A standard 404 is enough.
|
||||||
# Earlier check would be difficult since we don't want to break data modules
|
# Earlier check would be difficult since we don't want to break data modules
|
||||||
path_components = request.httprequest.path.split('/')
|
path_components = request.httprequest.path.split('/')
|
||||||
request.is_frontend = len(path_components) < 3 or path_components[2] != 'static' or not '.' in path_components[-1]
|
request.is_frontend = len(path_components) < 3 or path_components[2] != 'static' or '.' not in path_components[-1]
|
||||||
routing_error = e
|
routing_error = e
|
||||||
|
|
||||||
request.is_frontend_multilang = not func or (func and request.is_frontend and func.routing.get('multilang', func.routing['type'] == 'http'))
|
request.is_frontend_multilang = not func or (func and request.is_frontend and func.routing.get('multilang', func.routing['type'] == 'http'))
|
||||||
@@ -583,7 +577,7 @@ class IrHttp(models.AbstractModel):
|
|||||||
code = exception.code
|
code = exception.code
|
||||||
|
|
||||||
values.update(
|
values.update(
|
||||||
status_message=werkzeug.http.HTTP_STATUS_CODES.get(code,''),
|
status_message=werkzeug.http.HTTP_STATUS_CODES.get(code, ''),
|
||||||
status_code=code,
|
status_code=code,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -658,20 +652,20 @@ class IrHttp(models.AbstractModel):
|
|||||||
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
|
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
@tools.ormcache('path')
|
@tools.ormcache('path', 'query_args')
|
||||||
def url_rewrite(self, path):
|
def url_rewrite(self, path, query_args=None):
|
||||||
new_url = False
|
new_url = False
|
||||||
req = request.httprequest
|
req = request.httprequest
|
||||||
router = req.app.get_db_router(request.db).bind('')
|
router = req.app.get_db_router(request.db).bind('')
|
||||||
|
endpoint = False
|
||||||
try:
|
try:
|
||||||
_ = router.match(path, method='POST')
|
endpoint = router.match(path, method='POST', query_args=query_args)
|
||||||
except werkzeug.exceptions.MethodNotAllowed as e:
|
except werkzeug.exceptions.MethodNotAllowed:
|
||||||
_ = router.match(path, method='GET')
|
endpoint = router.match(path, method='GET', query_args=query_args)
|
||||||
except werkzeug.routing.RequestRedirect as e:
|
except werkzeug.routing.RequestRedirect as e:
|
||||||
new_url = e.new_url[7:] # remove scheme
|
new_url = e.new_url[7:] # remove scheme
|
||||||
except werkzeug.exceptions.NotFound as e:
|
_, endpoint = self.url_rewrite(new_url, query_args)
|
||||||
|
endpoint = endpoint and [endpoint]
|
||||||
|
except werkzeug.exceptions.NotFound:
|
||||||
new_url = path
|
new_url = path
|
||||||
except Exception as e:
|
return new_url or path, endpoint and endpoint[0]
|
||||||
raise e
|
|
||||||
|
|
||||||
return new_url or path
|
|
||||||
|
|||||||
Reference in New Issue
Block a user