[FIX] models: check constraints on computed stored fields

closes odoo/odoo#36008

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2019-08-23 10:13:19 +00:00
parent e388efd059
commit fdba00ebb8
4 changed files with 22 additions and 2 deletions
+2
View File
@@ -376,6 +376,8 @@ actual arch.
# Any exception raised below will cause a transaction rollback.
self = self.with_context(check_field_names=True)
for view in self:
if not view.arch:
continue
view_arch = etree.fromstring(view.arch.encode('utf-8'))
view._valid_inheritance(view_arch)
view_def = view.read_combined(['arch'])
+7
View File
@@ -148,6 +148,13 @@ class Message(models.Model):
message.name = self._context.get('compute_name',
"[%s] %s" % (message.discussion.name or '', message.author.name or ''))
@api.constrains('name')
def _check_name(self):
# dummy constraint to check on computed field
for message in self:
if message.name.startswith("[X]"):
raise ValidationError("No way!")
@api.depends('author.name', 'discussion.name', 'body')
def _compute_display_name(self):
for message in self:
@@ -8,7 +8,7 @@ from PIL import Image
import psycopg2
from odoo import fields
from odoo.exceptions import AccessError, UserError
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.tests import common
from odoo.tools import mute_logger, float_repr
from odoo.tools.date_utils import add, subtract, start_of, end_of
@@ -439,7 +439,7 @@ class TestFields(common.TransactionCase):
# remove oneself from discussion participants: we can no longer create
# messages in discussion
discussion.participants -= self.env.user
with self.assertRaises(Exception):
with self.assertRaises(ValidationError):
self.env['test_new_api.message'].create({'discussion': discussion.id, 'body': 'Whatever'})
# make sure that assertRaises() does not leave fields to recompute
@@ -450,6 +450,12 @@ class TestFields(common.TransactionCase):
discussion.participants += self.env.user
self.env['test_new_api.message'].create({'discussion': discussion.id, 'body': 'Whatever'})
# check constraint on recomputed field
self.assertTrue(discussion.messages)
with self.assertRaises(ValidationError):
discussion.name = "X"
discussion.flush()
def test_20_float(self):
""" test rounding of float fields """
record = self.env['test_new_api.mixed'].create({})
+5
View File
@@ -3708,6 +3708,11 @@ Fields:
else:
field.compute(self)
if field.store and any(self._ids):
# check constraints of the fields that have been computed
fnames = [f.name for f in self._field_computed[field]]
self.filtered('id')._validate_fields(fnames)
def _parent_store_create(self):
""" Set the parent_path field on ``self`` after its creation. """
if not self._parent_store: