[FIX] base: recompute view.model_data_id (#13885)

The field was a stored computed field without dependencies, and therefore it
was never (re)computed. Add a simple dependency on the model `ir.model.data`
to make the recomputation everytime a view is modified. Moreover, when views
are created from modules, delay the recomputation of view fields until their
`ir.model.data` record is created.
This commit is contained in:
Raphael Collet
2016-10-19 15:20:14 +02:00
committed by GitHub
parent d3e2aa5551
commit 3ec5a23007
3 changed files with 41 additions and 3 deletions
+21
View File
@@ -141,6 +141,24 @@ class TestViewSaving(common.TransactionCase):
Company = self.registry('res.company')
View = self.registry('ir.ui.view')
# create a view with an xmlid, like the file import would
with self.env.norecompute():
self.view_id = View.create(self.cr, self.uid, {
'name': "Test View",
'type': 'qweb',
'arch': ET.tostring(self.arch, encoding='utf-8').decode('utf-8')
})
imd = self.env['ir.model.data'].create({
'module': 'website',
'name': 'test_view',
'model': 'ir.ui.view',
'res_id': self.view_id,
})
# the xml_id of the view should not be flagged as 'noupdate'
self.assertEqual(View.browse(self.cr, self.uid, self.view_id).model_data_id, imd)
self.assertFalse(imd.noupdate)
replacement = ET.tostring(h.DIV(
h.H3("Column 2"),
h.UL(
@@ -152,6 +170,9 @@ class TestViewSaving(common.TransactionCase):
View.save(self.cr, self.uid, res_id=self.view_id, value=replacement,
xpath='/div/div[2]')
# the xml_id of the view should be flagged as 'noupdate'
self.assertTrue(imd.noupdate)
company = Company.browse(self.cr, self.uid, 1)
self.assertEqual(company.name, "Acme Corporation")
self.assertEqual(company.phone, "+12 3456789")
+12 -3
View File
@@ -229,10 +229,11 @@ actual arch.
for view, view_wo_lang in zip(self, self.with_context(lang=None)):
view_wo_lang.arch = view.arch_base
@api.depends('write_date')
def _compute_model_data_id(self):
# get the last ir_model_data record corresponding to self
# get the first ir_model_data record corresponding to self
domain = [('model', '=', 'ir.ui.view'), ('res_id', 'in', self.ids)]
for data in self.env['ir.model.data'].search_read(domain, ['res_id']):
for data in self.env['ir.model.data'].search_read(domain, ['res_id'], order='id desc'):
view = self.browse(data['res_id'])
view.model_data_id = data['id']
@@ -337,7 +338,15 @@ actual arch.
values['name'] = "%s %s" % (values.get('model'), values['type'])
self.clear_caches()
return super(View, self).create(self._compute_defaults(values))
if 'install_mode_data' in self._context:
# the view is created from a data file by installing a module; delay
# the recomputation of field 'model_data_id' until its xmlid record
# is created
with self.env.norecompute():
return super(View, self).create(self._compute_defaults(values))
else:
return super(View, self).create(self._compute_defaults(values))
@api.multi
def write(self, vals):
+8
View File
@@ -14,6 +14,14 @@ import openerp.tools
Field = E.field
class ViewXMLID(common.TransactionCase):
def test_model_data_id(self):
""" Check whether views know their xmlid record. """
view = self.env.ref('base.view_company_form')
self.assertTrue(view)
self.assertTrue(view.model_data_id)
self.assertEqual(view.model_data_id.complete_name, 'base.view_company_form')
class ViewCase(common.TransactionCase):
def setUp(self):
super(ViewCase, self).setUp()