Files
odoo_source/addons/test_website/tests/test_page.py
T
Romain Derie 9c733d8c22 [IMP] website: allow controller as homepage
Before this commit, only a website.page could be used as a custom
homepage ('custom' meaning other than '/').

But it is a real use case and requirement for our users to be able to
select a controller as homepage.
For instance, an ecommerce would want its homepage to be the shop and
not a regular page from where you then have to navigate to the shop.

Right now, this can (almost) be achieved by doing some technical
advanced operations:
- Move the 'Shop' menu first in the menu navbar of the website
- Delete the specific '/' website.page for the website
- Delete the generic '/' website.page (no website_id)

That way, the system will redirect `/` to `/shop`, which is not ideal as
it should remain `/` in the URL.

That's because, until now, the homepage (`/` controller) serve order
was:
- Serve the website.page set as homepage (`website.homepage_id`),
  happens when one did select another page as homepage through the page
  properties dialog
- Serve the website.page having `/` as URL (default)
- Serve the first accessible menu if there no `/` page or other page set
  as homepage, it acts as a last resort attempt to not serve a 404 and
  to try serving relevant content (the first menu of a website is most
  likely always better than a 404)
- Serve 404

This commit allows to introduce an URL as homepage, instead of only a
website.page. It can be done through the website settings in the
backend.

There is 2 main points to keep in mind about serving the homepage:
- make sure we don't serve a 404 as the website homepage. This is the
  website entry point, serving a 404 is terrible. That's why we have
  some fallback mechanism like serving the first menu.
- We need to serve / before fallbacking to the first menu, as a lot of
  site just remove the 'Home' first menu since it is a duplicate of the
  logo, which also redirect to the homepage. In such cases, it doesn't
  mean that the user want his first menu to be the homepage. We
  shouldn't rely on such a behavior, it should just be used as a last
  resort.

With this commit, the homepage serve order is now:
- If homepage URL is set (empty by default), serve the website.page
  matching it
- If homepage URL is set (empty by default), serve the controller
  matching it
- If homepage URL is not set, serve the `/` website.page
- Serve the first accessible menu as last resort. It should be relevant
  content, at least better than a 404
- Serve 404

Most DBs will just have a website.page with '/' as URL and keep the
homepage_url setting empty.

task-2969683

closes odoo/odoo#99100

Related: odoo/upgrade#3876
Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
2022-09-09 00:56:54 +02:00

60 lines
2.5 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import HttpCase, tagged
from odoo.tests.common import HOST
from odoo.tools import config, mute_logger
@tagged('-at_install', 'post_install')
class WithContext(HttpCase):
def test_01_homepage_url(self):
# Setup
website = self.env['website'].browse([1])
website.write({
'name': 'Test Website',
'domain': f'http://{HOST}:{config["http_port"]}',
'homepage_url': '/unexisting',
})
home_url = '/'
contactus_url = '/contactus'
contactus_url_full = website.domain + contactus_url
contactus_content = b'content="Contact Us | Test Website"'
self.env['website.menu'].search([
('website_id', '=', website.id),
('url', '=', contactus_url),
]).sequence = 1
# 404 shouldn't be served but fallback on first menu
# -------------------------------------------
# / page exists | first menu | homepage_url
# -------------------------------------------
# yes | /contactus | /unexisting
# -------------------------------------------
r = self.url_open(website.homepage_url)
self.assertEqual(r.status_code, 404, "The website homepage_url should be a 404")
r = self.url_open(home_url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.history[0].status_code, 303)
self.assertEqual(r.url, contactus_url_full)
self.assertIn(contactus_content, r.content)
# same with 403
# -------------------------------------------
# / page exists | first menu | homepage_url
# -------------------------------------------
# yes | /contactus | /test_website/200/name-1
# -------------------------------------------
rec_unpublished = self.env['test.model'].create({
'name': 'name',
'is_published': False,
})
website.homepage_url = f"/test_website/200/name-{rec_unpublished.id}"
with mute_logger('odoo.http'): # mute 403 warning
r = self.url_open(website.homepage_url)
self.assertEqual(r.status_code, 403, "The website homepage_url should be a 403")
r = self.url_open(home_url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.history[0].status_code, 303)
self.assertEqual(r.url, contactus_url_full)
self.assertIn(contactus_content, r.content)