[FIX] base: lighten default values validation in ir.values

Rev acd8c4de18 prevents the creation of default values for fields that
are not defined. This is too restrictive because of the following
points:
    - one may want to create default values for fields that are yet to be
      defined
    - "fake" default fields used in res.config that do not point to real
      fields

The initial scenario this revision was trying to fix is the creation of
wrong default values through studio. This scenario is still fixed, as it
concerns the last `except` branch.

The tests have been updated to reflect these changes.
This commit is contained in:
Simon Lejeune
2017-03-22 12:48:56 +01:00
parent 915e07bcac
commit fcbfe47695
2 changed files with 10 additions and 4 deletions
+5 -1
View File
@@ -7,6 +7,10 @@ from odoo import api, fields, models, tools, _
from odoo.exceptions import AccessError, MissingError, ValidationError
from odoo.tools import pickle
import logging
_logger = logging.getLogger(__name__)
EXCLUDED_FIELDS = set(('code',
'report_sxw_content', 'report_rml_content', 'report_sxw', 'report_rml',
'report_sxw_content_data', 'report_rml_content_data', 'search_view', ))
@@ -224,7 +228,7 @@ class IrValues(models.Model):
field = self.env[model]._fields[field_name]
field.convert_to_cache(value, self.browse())
except KeyError:
raise ValidationError(_("Invalid field %s.%s") % (model, field_name))
_logger.warning("Invalid field %s.%s", model, field_name)
except Exception:
raise ValidationError(_("Invalid value for %s.%s: %s") % (model, field_name, value))
+5 -3
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import tools
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase
@@ -41,11 +42,12 @@ class TestIrValues(TransactionCase):
self.assertTrue(d, "At least one value should be retrieved for this model.")
self.assertEqual(d[2], '007', "Can't retrieve the created default value.")
# create invalid defaults
with self.assertRaises(ValidationError):
# create valid but unusable defaults, a ValidationError should not be thrown
with tools.mute_logger('odoo.addons.base.ir.ir_values'):
ir_values.set_default('unknown_model', 'unknown_field', 42)
with self.assertRaises(ValidationError):
ir_values.set_default('res.partner', 'unknown_field', 42)
# create invalid defaults
with self.assertRaises(ValidationError):
ir_values.set_default('res.partner', 'lang', 'some_LANG')
with self.assertRaises(ValidationError):