Browse Source

[FIX] base_custom_info: Check contrain on model into template before the write

We must avoid to rely on the order in which computed fields (including related fields) and constrains methods are applied.
Due to a recent change into the ORM, the contrains on the model_id into CustomInfoTemplate is now called AFTER the recompute of the related model field into property_ids.info_value_ids
As side effect, when the constrains is called, the model on the info value is already updated with the new value and we no more know the old value....
12.0-mig-module_prototyper_last
Laurent Mignon (ACSONE) 5 years ago
committed by Alexandre Díaz
parent
commit
1d5fa4a522
  1. 26
      base_custom_info/models/custom_info_template.py

26
base_custom_info/models/custom_info_template.py

@ -53,18 +53,18 @@ class CustomInfoTemplate(models.Model):
self._inverse_model() self._inverse_model()
@api.multi @api.multi
@api.constrains("model_id")
def _check_model(self):
"""Avoid error when updating base module and a submodule extends a
model that falls out of this one's dependency graph.
def _check_model_update_allowed(self, model_id):
"""Check if the template's model can be updated.
Template can be updated only if no property values already exists for
this template
""" """
for record in self: for record in self:
with self.env.norecompute():
oldmodels = record.mapped("property_ids.info_value_ids.model")
if oldmodels and record.model not in oldmodels:
raise ValidationError(
_("You cannot change the model because it is in use.")
)
if (model_id != record.model_id.id
and record.mapped("property_ids.info_value_ids")):
raise ValidationError(
_("You cannot change the model because it is in use.")
)
@api.multi @api.multi
def check_access_rule(self, operation): def check_access_rule(self, operation):
@ -74,3 +74,9 @@ class CustomInfoTemplate(models.Model):
model.check_access_rights(operation) model.check_access_rights(operation)
model.check_access_rule(operation) model.check_access_rule(operation)
return super(CustomInfoTemplate, self).check_access_rule(operation) return super(CustomInfoTemplate, self).check_access_rule(operation)
@api.multi
def write(self, vals):
if 'model_id' in vals:
self._check_model_update_allowed(vals['model_id'])
return super(CustomInfoTemplate, self).write(vals)
Loading…
Cancel
Save