[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 <juc@odoo.com>
This commit is contained in:
Julien Castiaux
2022-11-14 17:17:59 +01:00
parent 930b2f015e
commit 1219f043fe
2 changed files with 26 additions and 6 deletions
+10 -6
View File
@@ -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
@@ -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'<link rel="alternate" hreflang="en" href="http://[^"]+/"/>')
r = self.url_open(be_prefix + '/contactus')
self.assertRegex(r.text, r'<link rel="alternate" hreflang="en" href="http://[^"]+/contactus"/>')
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")