From 939b61822e5f017d280f981be60cec90b0bb856d Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Tue, 20 Jun 2023 10:10:51 +0000 Subject: [PATCH] [IMP] http_routing: server_redirect in case of 404 It is a tradeoff since we will add extra requests in case of 404 to check if a redirect exists. But it will allow to redirect old unlinked record to a new record. Until now, if you delete e.g. a product instead to archive it, you have no way to redirect old url to the new product. closes odoo/odoo#125828 X-original-commit: 95dd21c4d69a06b56b908e138f7746ef3d131d82 Signed-off-by: Julien Castiaux (juc) --- addons/http_routing/models/ir_http.py | 2 +- addons/test_website/tests/test_redirect.py | 23 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/addons/http_routing/models/ir_http.py b/addons/http_routing/models/ir_http.py index 46e89529d54..3be942d5d18 100644 --- a/addons/http_routing/models/ir_http.py +++ b/addons/http_routing/models/ir_http.py @@ -640,7 +640,7 @@ class IrHttp(models.AbstractModel): code, values = cls._get_exception_code_values(exception) request.cr.rollback() - if code == 403: + if code in (404, 403): try: response = cls._serve_fallback() if response: diff --git a/addons/test_website/tests/test_redirect.py b/addons/test_website/tests/test_redirect.py index 1fd67e59ffa..f707f7ef971 100644 --- a/addons/test_website/tests/test_redirect.py +++ b/addons/test_website/tests/test_redirect.py @@ -2,7 +2,6 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import odoo from odoo.tests import HttpCase, tagged -from odoo.tests.common import HOST from odoo.tools import mute_logger from odoo.addons.http_routing.models.ir_http import slug @@ -210,3 +209,25 @@ class TestRedirect(HttpCase): self.assertTrue( r.url.endswith(url_rec2), "Unpublished record should redirect to published record set in redirect") + + @mute_logger('odoo.http') + def test_05_redirect_404_notfound_record(self): + # 1. Accessing unexisting record: raise 404 + url_rec1 = '/test_website/200/unexisting-100000' + r = self.url_open(url_rec1) + self.assertEqual(r.status_code, 404) + + # 2. Accessing unpublished record with redirect to a 404: expecting 404 + redirect = self.env['website.rewrite'].create({ + 'name': 'Test 301 Redirect route unexisting record', + 'redirect_type': '301', + 'url_from': url_rec1, + 'url_to': '/get', + }) + r = self.url_open(url_rec1, allow_redirects=False) + self.assertEqual(r.status_code, 301) + self.assertTrue(r.headers.get('Location', '').endswith(redirect.url_to)) + + r = self.url_open(url_rec1, allow_redirects=True) + self.assertEqual(r.status_code, 200) + self.assertTrue(r.url.endswith(redirect.url_to))