[FIX] core: validation of field parameters

The feature was actually not working as expected.  It has been fixed,
and a test now ensures it does work as expected.  This commit also fixes
some invalid parameters, hopefully nothing critical.

closes odoo/odoo#60429

X-original-commit: 231bdca3f26d96f578503694e5aec062de2317ed
Related: odoo/enterprise#14285
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2020-10-21 10:18:36 +00:00
parent 5e9748cc82
commit 8de95a3fb4
6 changed files with 30 additions and 9 deletions
@@ -9,7 +9,7 @@ class AccountPaymentRegister(models.TransientModel):
_description = 'Register Payment'
# == Business fields ==
payment_date = fields.Date(String="Payment Date", required=True,
payment_date = fields.Date(string="Payment Date", required=True,
default=fields.Date.context_today)
amount = fields.Monetary(currency_field='currency_id', store=True, readonly=False,
compute='_compute_amount')
+2 -2
View File
@@ -74,13 +74,13 @@ class MrpWorkorder(models.Model):
compute='_compute_dates_planned',
inverse='_set_dates_planned',
states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
store=True, tracking=True, copy=False)
store=True, copy=False)
date_planned_finished = fields.Datetime(
'Scheduled End Date',
compute='_compute_dates_planned',
inverse='_set_dates_planned',
states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},
store=True, tracking=True, copy=False)
store=True, copy=False)
date_start = fields.Datetime(
'Start Date', copy=False,
states={'done': [('readonly', True)], 'cancel': [('readonly', True)]})
@@ -34,7 +34,7 @@ class Sponsor(models.Model):
image_256 = fields.Image("Image 256", related="image_512", max_width=256, max_height=256, store=False)
image_128 = fields.Image("Image 128", related="image_512", max_width=128, max_height=128, store=False)
website_image_url = fields.Char(
string='Image URL', max_width=256, max_height=256,
string='Image URL',
compute='_compute_website_image_url', compute_sudo=True, store=False)
@api.depends('partner_id')
+1 -1
View File
@@ -335,7 +335,7 @@ class IrAttachment(models.Model):
access_token = fields.Char('Access Token', groups="base.group_user")
# the field 'datas' is computed and may use the other fields below
raw = fields.Binary(string="File Content (raw)", compute='_compute_raw', inverse='_inverse_raw', context_dependent=False)
raw = fields.Binary(string="File Content (raw)", compute='_compute_raw', inverse='_inverse_raw')
datas = fields.Binary(string='File Content (base64)', compute='_compute_datas', inverse='_inverse_datas')
db_datas = fields.Binary('Database Data', attachment=False)
store_fname = fields.Char('Stored Filename')
@@ -2896,6 +2896,28 @@ class TestSelectionOndeleteAdvanced(common.TransactionCase):
self.registry.setup_models(self.env.cr)
class TestFieldParametersValidation(common.TransactionCase):
def test_invalid_parameter(self):
self.addCleanup(self.registry.model_cache.clear)
class Foo(models.Model):
_module = None
_name = _description = 'test_new_api.field_parameter_validation'
name = fields.Char(invalid_parameter=42)
Foo._build_model(self.registry, self.env.cr)
self.addCleanup(self.registry.__delitem__, Foo._name)
with self.assertLogs('odoo.fields', level='WARNING') as cm:
self.registry.setup_models(self.env.cr)
self.assertTrue(cm.output[0].startswith(
"WARNING:odoo.fields:Field test_new_api.field_parameter_validation.name: "
"unknown parameter 'invalid_parameter'"
))
def insert(model, *fnames):
""" Return the expected query string to INSERT the given columns. """
columns = ['create_uid', 'create_date', 'write_uid', 'write_date'] + sorted(fnames)
+3 -4
View File
@@ -356,8 +356,9 @@ class Field(MetaField('DummyField', (object,), {})):
def _setup_attrs(self, model, name):
""" Initialize the field parameter attributes. """
# validate extra arguments
for key in self.args:
attrs = self._get_attrs(model, name)
# validate arguments
for key in attrs:
# TODO: improve filter as there are attributes on the class which
# are not valid on the field, probably
if not (hasattr(self, key) or model._valid_field_parameter(self, key)):
@@ -368,8 +369,6 @@ class Field(MetaField('DummyField', (object,), {})):
" allow it",
model._name, name, key
)
attrs = self._get_attrs(model, name)
self.__dict__.update(attrs)
# prefetch only stored, column, non-manual and non-deprecated fields