[FIX] website: sync website homepage url with page url change

- 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 <jke@odoo.com>
This commit is contained in:
Romain Derie
2023-12-21 17:24:56 +00:00
parent 462c3f55e1
commit c66551ff8a
2 changed files with 44 additions and 0 deletions
+6
View File
@@ -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
+38
View File
@@ -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': '<div>HomepageUrlTest</div>',
'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])