Skip to content
Snippets Groups Projects
Commit a4096276 authored by Denis Ledoux's avatar Denis Ledoux
Browse files

[FIX] ir_ui_view: do not change view mode when just changing the inherit

Some views have the primary mode while having an inherit_id view
In such views, if the user wants to change the inherit_id,
the mode must remain primary.

The use case behind this is a user who want to change
the inherit_id view of the view
product.product.form (product.product_normal_form_view)
to another view.
This view is in primary mode while having an inherit_id
(product.product_template_form_view).
In such a case, the primary mode must remain,
otherwise the view will no longer be opened by default
when opening a product.
Indeed, when searching the default form view of a model,
it only searches for primary mode views for this model.

The `all` is there because this is an `api.multi` method.
We could consider adding a `self.ensure_one`,
but it breaks the API if someones calls this method with multiple records.
This is likely to happen, as this method is used
in `write` which is likely to be called with multiple records.

In my opinion, in master, this should be replaced by an
onchange so the user can see the change of mode
when he adds or remove an inherit_id for a view.

opw-1916324

closes odoo/odoo#30241
parent c5a8e397
No related branches found
No related tags found
No related merge requests found
......@@ -330,7 +330,10 @@ actual arch.
def _compute_defaults(self, values):
if 'inherit_id' in values:
values.setdefault('mode', 'extension' if values['inherit_id'] else 'primary')
# Do not automatically change the mode if the view already has an inherit_id,
# and the user change it to another.
if not values['inherit_id'] or all(not view.inherit_id for view in self):
values.setdefault('mode', 'extension' if values['inherit_id'] else 'primary')
return values
@api.model
......
......@@ -845,6 +845,12 @@ class ViewModeField(ViewCase):
})
self.assertEqual(view2.mode, 'extension')
view2.write({'inherit_id': None})
self.assertEqual(view2.mode, 'primary')
view2.write({'inherit_id': view.id})
self.assertEqual(view2.mode, 'extension')
@mute_logger('odoo.sql_db')
def testModeExplicit(self):
view = self.View.create({
......@@ -857,6 +863,7 @@ class ViewModeField(ViewCase):
'arch': '<qweb/>'
})
self.assertEqual(view.mode, 'primary')
self.assertEqual(view2.mode, 'primary')
with self.assertRaises(IntegrityError):
self.View.create({
......@@ -908,6 +915,27 @@ class ViewModeField(ViewCase):
view.write({'mode': 'primary'})
def testChangeInheritOfPrimary(self):
"""
A primary view with an inherit_id must remain primary when changing the inherit_id
"""
base1 = self.View.create({
'inherit_id': None,
'arch': '<qweb/>',
})
base2 = self.View.create({
'inherit_id': None,
'arch': '<qweb/>',
})
view = self.View.create({
'mode': 'primary',
'inherit_id': base1.id,
'arch': '<qweb/>',
})
self.assertEqual(view.mode, 'primary')
view.write({'inherit_id': base2.id})
self.assertEqual(view.mode, 'primary')
class TestDefaultView(ViewCase):
def testDefaultViewBase(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment