From dce5dade012ee8641cb4ce2cc1e8b756b88723b1 Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Thu, 7 Jul 2022 09:13:59 +0000 Subject: [PATCH] [FIX] website: missing alternate URLs for pages Install multiple langages, each time translating the website. In a private browsing session access /contactus, show the page source, the multiple alternate URL (`` in the ``) are all pointing the canonical URL instead of the alternative. closes odoo/odoo#95683 X-original-commit: 3d4b4d3dcff864613a9e7038137e21674425ed08 Signed-off-by: Romain Derie (rde) Signed-off-by: Julien Castiaux --- addons/website/models/website.py | 6 +++--- addons/website/tests/test_page.py | 35 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index eff0160eeb6..f3267065e4e 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -1331,11 +1331,11 @@ class Website(models.Model): router = http.root.get_db_router(request.db).bind('') path = router.build(rule.endpoint, args) - if lang != self.default_lang_id: - path = f'/{lang.url_code}{path if path != "/" else ""}' except (NotFound, AccessError, MissingError): # The build method returns a quoted URL so convert in this case for consistency. - path = urls.url_quote_plus(request.httprequest.environ['REQUEST_URI'], safe='/') + path = urls.url_quote_plus(request.httprequest.path, safe='/') + if lang != self.default_lang_id: + path = f'/{lang.url_code}{path if path != "/" else ""}' canonical_query_string = f'?{urls.url_encode(canonical_params)}' if canonical_params else '' return self.get_base_url() + path + canonical_query_string diff --git a/addons/website/tests/test_page.py b/addons/website/tests/test_page.py index 2f61652f686..1ab59fb2ef4 100644 --- a/addons/website/tests/test_page.py +++ b/addons/website/tests/test_page.py @@ -302,7 +302,7 @@ class WithContext(HttpCase): "There should be no crash when a public user is accessing `/` which is rerouting to another page with a different URL.") root_html = html.fromstring(r.content) canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href'] - self.assertEqual(canonical_url, website.domain + "/") + self.assertIn(canonical_url, [f"{website.domain}/", f"{website.domain}/page_1"]) def test_t_cache_footer_debug_link(self): # Debug link (flag) should be use the current url. @@ -312,3 +312,36 @@ class WithContext(HttpCase): self.assertIn('