[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
This commit is contained in:
Jeremy Kersten
2020-03-19 15:37:47 +00:00
committed by Romain Derie
parent 4c40393490
commit 9430dd1287
2 changed files with 9 additions and 1 deletions
+1 -1
View File
@@ -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):
+8
View File
@@ -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):