[FIX] website: prevent traceback if xml homepage view has no page

The XML file homepage view record is not supposed to be deleted as it is used
to bootstrap newly created website.

If that view is deleted, everything's fine. But if that view's page is not
found (m2o link broken somehow), then the code would crash.

This would only appear if the user is playing in the backend in technical but
it seems to happen more than it should.

This commit will prevent the code to crash in such a case.

Step to reproduce:
1. Go to settings > technical > views (debug)
2. Find the xml homepage view (with xml_id)
3. Go to its website.page and break the m2o view_id
4. Try to create a new website, it will crash

opw-2081894

closes odoo/odoo#39149

X-original-commit: 1d4dd200c664b99dbdb3e65e3950be1cb5e79a0a
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
This commit is contained in:
Romain Derie
2019-10-21 23:21:45 +00:00
committed by fw-bot
parent ed5b6c9695
commit 7cce59a3c3
+13 -3
View File
@@ -188,6 +188,7 @@ class Website(models.Model):
# Page Management
# ----------------------------------------------------------
def _bootstrap_homepage(self):
Page = self.env['website.page']
standard_homepage = self.env.ref('website.homepage', raise_if_not_found=False)
if not standard_homepage:
return
@@ -200,10 +201,19 @@ class Website(models.Model):
</t>''' % (self.id)
standard_homepage.with_context(website_id=self.id).arch_db = new_homepage_view
self.homepage_id = self.env['website.page'].search([('website_id', '=', self.id),
('key', '=', standard_homepage.key)])
homepage_page = Page.search([
('website_id', '=', self.id),
('key', '=', standard_homepage.key),
])
if not homepage_page:
homepage_page = Page.create({
'website_published': True,
'url': '/',
'view_id': self.with_context(website_id=self.id).viewref('website.homepage').id,
})
# prevent /-1 as homepage URL
self.homepage_id.url = '/'
homepage_page.url = '/'
self.homepage_id = homepage_page
# Bootstrap default menu hierarchy, create a new minimalist one if no default
default_menu = self.env.ref('website.main_menu')