Skip to content

Commit

Permalink
[FIX] base: lighten default values validation in ir.values
Browse files Browse the repository at this point in the history
Rev acd8c4d prevents the creation of default values for fiels that
are not defined. This is too restrictive because of the following
points:
    - one may want to create default values for 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.
  • Loading branch information
sle-odoo committed Mar 21, 2017
1 parent ff0053b commit 515c5b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion odoo/addons/base/ir/ir_values.py
Expand Up @@ -7,6 +7,10 @@
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', ))
Expand Down Expand Up @@ -224,7 +228,7 @@ def set_default(self, model, field_name, value, for_all_users=True, company_id=F
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))

Expand Down
8 changes: 5 additions & 3 deletions odoo/addons/base/tests/test_ir_values.py
@@ -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

Expand Down Expand Up @@ -41,11 +42,12 @@ def test_defaults(self):
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):
Expand Down

0 comments on commit 515c5b1

Please sign in to comment.