From c66551ff8a2a75800a8804a92eb3c54a6ac33c33 Mon Sep 17 00:00:00 2001 From: Romain Derie Date: Mon, 11 Dec 2023 15:07:06 +0100 Subject: [PATCH] [FIX] website: sync website homepage url with page url change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create a "/test" page, publish it - Set "/test" as homepage url directly on the website (in the settings) - Go to that page - Open the page properties and change its URL from /test to /else From there, the website setting (`homepage_url` field) was still set to /test and not /else. It means that the first "published" menu would then be used to avoid serving a 404 as homepage to your visitors. It is a bit confusing and people probably just expect to change that website setting too when changing the page URL. We had some feedback about similar flows (that couldn't be reproduced or where people weren't sure) where people ended up writting on the wrong page or losing their content. Maybe this will mitigate the issue. Kind of related: task-3476840 closes odoo/odoo#147086 X-original-commit: 374a1b31f70a3209b1ae11db8e5886350579c7f9 Signed-off-by: Jérémy Kersten --- addons/website/models/website_page.py | 6 +++++ addons/website/tests/test_page.py | 38 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/addons/website/models/website_page.py b/addons/website/models/website_page.py index e4d0487b028..d5e5eb67e4c 100644 --- a/addons/website/models/website_page.py +++ b/addons/website/models/website_page.py @@ -178,6 +178,12 @@ class Page(models.Model): 'url_to': url, 'website_id': website_id, }) + # Sync website's homepage URL + website = self.env['website'].get_current_website() + page_url_normalized = {'homepage_url': page.url} + website._handle_homepage_url(page_url_normalized) + if website.homepage_url == page_url_normalized['homepage_url']: + website.homepage_url = vals['url'] vals['url'] = url # If name has changed, check for key uniqueness diff --git a/addons/website/tests/test_page.py b/addons/website/tests/test_page.py index f967d44be5e..9d24d93946d 100644 --- a/addons/website/tests/test_page.py +++ b/addons/website/tests/test_page.py @@ -306,6 +306,44 @@ class WithContext(HttpCase): canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href'] self.assertIn(canonical_url, [f"{website.domain}/", f"{website.domain}/page_1"]) + def test_website_homepage_url_change(self): + website = self.env['website'].browse([1]) + + test_page = self.env['website.page'].create({ + 'name': 'HomepageUrlTest', + 'type': 'qweb', + 'arch': '
HomepageUrlTest
', + 'key': 'test.homepage_url_test', + 'url': '/homepage_url_test', + 'is_published': True, + }) + self.assertEqual(test_page.url, '/homepage_url_test') + + # If one has set the `homepage_url` to a specific page URL.. + website.write({ + 'name': 'Test Website', + 'domain': f'http://{HOST}:{config["http_port"]}', + 'homepage_url': test_page.url, + }) + home_url_full = website.domain + '/' + r = self.url_open('/') + self.assertEqual(r.status_code, 200) + self.assertEqual(r.url, home_url_full) + self.assertIn(b"HomepageUrlTest", r.content) + + # .. and then change that page URL .. + with MockRequest(self.env, website=website): + test_page.url = '/url-changed' + + # .. the `homepage_url` should be changed to follow the new page URL + r = self.url_open('/') + self.assertEqual(r.status_code, 200) + self.assertEqual( + r.url, home_url_full, """URL should still be '/', note that if this + `assert` fail, the loaded URL will probably be the first available + menu different from '/', see homepage controller.""") + self.assertIn(b"HomepageUrlTest", r.content) + def test_06_homepage_url(self): # Setup website = self.env['website'].browse([1])