summaryrefslogtreecommitdiff
path: root/addons/website/models
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2019-01-14 10:34:50 +0100
committerChristophe Simonis <chs@odoo.com>2019-01-14 10:34:50 +0100
commit3cbc9dd6c034f8bbba8a75def07d063e3f625bdb (patch)
tree55d7e9b940e27a1dad5415b6b721ab464d0bc418 /addons/website/models
parent976429c8c69b2225e96ccb0e254ee4849832ed27 (diff)
parent6a0675d36dd888daa4183f3d609ce334ed3cb493 (diff)
[MERGE] forward port branch 12.0 up to 6a0675d36dd
Diffstat (limited to 'addons/website/models')
-rw-r--r--addons/website/models/ir_ui_view.py15
-rw-r--r--addons/website/models/website.py29
2 files changed, 34 insertions, 10 deletions
diff --git a/addons/website/models/ir_ui_view.py b/addons/website/models/ir_ui_view.py
index 1a98713326d..d37852ae0de 100644
--- a/addons/website/models/ir_ui_view.py
+++ b/addons/website/models/ir_ui_view.py
@@ -143,8 +143,12 @@ class View(models.Model):
return self.filtered(lambda view: not view.website_id)
for view in self:
+ # specific view: add it if it's for the current website and ignore
+ # it if it's for another website
if view.website_id and view.website_id.id == current_website_id:
most_specific_views |= view
+ # generic view: add it only if, for the current website, there is no
+ # specific view for this view (based on the same `key` attribute)
elif not view.website_id and not any(view.key == view2.key and view2.website_id and view2.website_id.id == current_website_id for view2 in self):
most_specific_views |= view
@@ -201,6 +205,17 @@ class View(models.Model):
@api.model
@tools.ormcache_context('self._uid', 'xml_id', keys=('website_id',))
def get_view_id(self, xml_id):
+ """If a website_id is in the context and the given xml_id is not an int
+ then try to get the id of the specific view for that website, but
+ fallback to the id of the generic view if there is no specific.
+
+ If no website_id is in the context, it might randomly return the generic
+ or the specific view, so it's probably not recommanded to use this
+ method. `viewref` is probably more suitable.
+
+ Archived views are ignored (unless the active_test context is set, but
+ then the ormcache_context will not work as expected).
+ """
if 'website_id' in self._context and not isinstance(xml_id, int):
current_website = self.env['website'].browse(self._context.get('website_id'))
domain = ['&', ('key', '=', xml_id)] + current_website.website_domain()
diff --git a/addons/website/models/website.py b/addons/website/models/website.py
index 82709c4663d..4aea35aa481 100644
--- a/addons/website/models/website.py
+++ b/addons/website/models/website.py
@@ -503,12 +503,18 @@ class Website(models.Model):
def viewref(self, view_id, raise_if_not_found=True):
''' Given an xml_id or a view_id, return the corresponding view record.
In case of website context, return the most specific one.
+
+ If no website_id is in the context, it will return the generic view,
+ instead of a random one like `get_view_id`.
+
+ Look also for archived views, no matter the context.
+
:param view_id: either a string xml_id or an integer view_id
:param raise_if_not_found: should the method raise an error if no view found
:return: The view record or empty recordset
'''
View = self.env['ir.ui.view']
- view = None
+ view = View
if isinstance(view_id, str):
if 'website_id' in self._context:
domain = [('key', '=', view_id)] + self.env['website'].website_domain(self._context.get('website_id'))
@@ -520,20 +526,20 @@ class Website(models.Model):
if views:
view = views.filter_duplicate()
else:
- view = self.env.ref(view_id)
+ # we handle the raise below
+ view = self.env.ref(view_id, raise_if_not_found=False)
# self.env.ref might return something else than an ir.ui.view (eg: a theme.ir.ui.view)
- if view._name != 'ir.ui.view':
- view = None
+ if not view or view._name != 'ir.ui.view':
+ # make sure we always return a recordset
+ view = View
elif isinstance(view_id, int):
view = View.browse(view_id)
else:
raise ValueError('Expecting a string or an integer, not a %s.' % (type(view_id)))
- if view:
- return view
- if raise_if_not_found:
+ if not view and raise_if_not_found:
raise ValueError('No record found for unique ID %s. It may have been deleted.' % (view_id))
- return None
+ return view
@api.model
def get_template(self, template):
@@ -1151,9 +1157,12 @@ class Menu(models.Model):
@api.multi
def unlink(self):
default_menu = self.env.ref('website.main_menu', raise_if_not_found=False)
+ menus_to_remove = self
for menu in self.filtered(lambda m: default_menu and m.parent_id.id == default_menu.id):
- self.env['website.menu'].search([('url', '=', menu.url), ('id', '!=', menu.id)]).unlink()
- return super(Menu, self).unlink()
+ menus_to_remove |= self.env['website.menu'].search([('url', '=', menu.url),
+ ('website_id', '!=', False),
+ ('id', '!=', menu.id)])
+ return super(Menu, menus_to_remove).unlink()
@api.one
def _compute_visible(self):