From 9430dd12878fd702baf2415700842e6db5f1a06c Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Fri, 13 Mar 2020 15:16:44 +0000 Subject: [PATCH] [FIX] website: perf - use lang cache in url for. Before this commit, url_lang (= url_for) will make query to find the lang_code but we alrady have this info in ormcache with get_available method. So instead to rebrowse (search query) the lang, we use a new method that will try to find it in cache if possible before to make the query. The gain on each page is n-1 (where n is # lang installed) Because the switcher of languages do an url_for for each lang available. Related to #47257 task-2211013 X-original-commit: 2cecbe356ce283e15fb0bbd0a6e9aa4735af520e --- addons/http_routing/models/ir_http.py | 2 +- odoo/addons/base/models/res_lang.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/addons/http_routing/models/ir_http.py b/addons/http_routing/models/ir_http.py index ae3ca509b73..5469310228f 100644 --- a/addons/http_routing/models/ir_http.py +++ b/addons/http_routing/models/ir_http.py @@ -150,7 +150,7 @@ def url_lang(path_or_uri, lang_code=None): location = werkzeug.urls.url_join(request.httprequest.path, location) lang_url_codes = [url_code for _, url_code, _ in Lang.get_available()] lang_code = pycompat.to_text(lang_code or request.context['lang']) - lang_url_code = Lang._lang_get(lang_code).url_code + lang_url_code = Lang._lang_code_to_urlcode(lang_code) lang_url_code = lang_url_code if lang_url_code in lang_url_codes else lang_code if (len(lang_url_codes) > 1 or force_lang) and is_multilang_url(location, lang_url_codes): diff --git a/odoo/addons/base/models/res_lang.py b/odoo/addons/base/models/res_lang.py index 6528b44482d..0b58daf5411 100644 --- a/odoo/addons/base/models/res_lang.py +++ b/odoo/addons/base/models/res_lang.py @@ -213,6 +213,14 @@ class Lang(models.Model): langs = self.with_context(active_test=False).search([]) return sorted([(lang.code, lang.url_code, lang.name) for lang in langs], key=itemgetter(2)) + @api.model + @tools.ormcache('code') + def _lang_code_to_urlcode(self, code): + for c, urlc, name in self.get_available(): + if c == code: + return urlc + return self._lang_get(code).url_code + @api.model @tools.ormcache() def get_installed(self):