From 1219f043fe1477359ad85ad7f829664a12735bb2 Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Thu, 27 Oct 2022 14:00:31 +0000 Subject: [PATCH] [FIX] http_routing: should redirect only for multilang Define a route that is website but not multilang, e.g. @route('/example', website=True, multilang=False) Login to the frontend, change the website lang to another (non-default) lang (e.g. install french, keep english as default lang, log in the french website) then access the '/example' controller by typing it directly in your address bar. You are being redirected to '/fr/example', you should not. This commit restore the behavior pre-httpocalypse, that is the address is kept as-is. Note: in the comment, the 4th and 5th cases were inverted, we use this commit as an opportunity to reorder the two. closes odoo/odoo#105686 X-original-commit: be7a02917a66136a8d3b601d61a898b0419ff79d Signed-off-by: Julien Castiaux --- addons/http_routing/models/ir_http.py | 16 ++++++++++------ addons/test_website/tests/test_is_multilang.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/addons/http_routing/models/ir_http.py b/addons/http_routing/models/ir_http.py index 3579a10f93b..444a8e5241f 100644 --- a/addons/http_routing/models/ir_http.py +++ b/addons/http_routing/models/ir_http.py @@ -369,14 +369,14 @@ class IrHttp(models.AbstractModel): 3/ Use the URL as-is saving the requested lang when the user is a bot and that the lang is missing from the URL. - 4/ Redirect the browser when the lang is missing from the URL - but another lang than the default one has been requested. The - requested lang is injected before the original path. - - 5) Use the url as-is when the lang is missing from the URL, that + 4) Use the url as-is when the lang is missing from the URL, that another lang than the default one has been requested but that it is forbidden to redirect (e.g. POST) + 5/ Redirect the browser when the lang is missing from the URL + but another lang than the default one has been requested. The + requested lang is injected before the original path. + 6/ Redirect the browser when the lang is present in the URL but it is the default lang. The lang is removed from the original URL. @@ -415,7 +415,11 @@ class IrHttp(models.AbstractModel): else: url_lang_str = '' path_no_lang = path - allow_redirect = request.httprequest.method != 'POST' + + allow_redirect = ( + request.httprequest.method != 'POST' + and getattr(request, 'is_frontend_multilang', True) + ) # There is no user on the environment yet but the following code # requires one to set the lang on the request. Temporary grant diff --git a/addons/test_website/tests/test_is_multilang.py b/addons/test_website/tests/test_is_multilang.py index 22167234d01..38bf27c3338 100644 --- a/addons/test_website/tests/test_is_multilang.py +++ b/addons/test_website/tests/test_is_multilang.py @@ -57,6 +57,7 @@ class TestIsMultiLang(odoo.tests.HttpCase): it_href = body.find('./head/link[@rel="alternate"][@hreflang="it"]').get('href') fr_href = body.find('./head/link[@rel="alternate"][@hreflang="fr"]').get('href') en_href = body.find('./head/link[@rel="alternate"][@hreflang="en"]').get('href') + self.assertEqual(urlparse(it_href).path, f'/{it.url_code}/test_lang_url/my-super-country-italia-{country1.id}') self.assertEqual(urlparse(fr_href).path, f'/{be.url_code}/test_lang_url/my-super-country-belgium-{country1.id}') self.assertEqual(urlparse(en_href).path, f'/test_lang_url/my-super-country-{country1.id}') @@ -79,3 +80,18 @@ class TestIsMultiLang(odoo.tests.HttpCase): self.assertRegex(r.text, r'') r = self.url_open(be_prefix + '/contactus') self.assertRegex(r.text, r'') + + def test_04_multilang_false(self): + website = self.env['website'].search([], limit=1) + fr = self.env.ref('base.lang_fr').sudo() + en = self.env.ref('base.lang_en').sudo() + fr.active = True + + website.default_lang_id = en + website.language_ids = en + fr + self.opener.cookies['frontend_lang'] = fr.iso_code + + res = self.url_open('/get_post_nomultilang', allow_redirects=False) + res.raise_for_status() + + self.assertEqual(res.status_code, 200, "Should not be redirected")