diff --git a/CHANGELOG b/CHANGELOG index 29a878b71d..8fb51a45f8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Validate selection format in Dict schema * Allow to extend Field's string and help * Add console * Add Model.__names__ to retrieve model and field names diff --git a/trytond/ir/message.xml b/trytond/ir/message.xml index d8447712bf..9030322608 100644 --- a/trytond/ir/message.xml +++ b/trytond/ir/message.xml @@ -109,6 +109,9 @@ this repository contains the full copyright notices and license terms. --> Invalid domain in schema "%(schema)s". + + Invalid selection in schema "%(schema)s". + Recursion error: Record "%(rec_name)s" with parent "%(parent_rec_name)s" was configured as ancestor of itself. diff --git a/trytond/model/dictschema.py b/trytond/model/dictschema.py index 8a414c4ef3..7c0e7f0de3 100644 --- a/trytond/model/dictschema.py +++ b/trytond/model/dictschema.py @@ -16,6 +16,10 @@ class DomainError(ValidationError): pass +class SelectionError(ValidationError): + pass + + class DictSchemaMixin(object): _rec_name = 'string' name = fields.Char('Name', required=True) @@ -67,6 +71,7 @@ class DictSchemaMixin(object): def validate(cls, schemas): super(DictSchemaMixin, cls).validate(schemas) cls.check_domain(schemas) + cls.check_selection(schemas) @classmethod def check_domain(cls, schemas): @@ -84,7 +89,19 @@ class DictSchemaMixin(object): gettext('ir.msg_dict_schema_invalid_domain', schema=schema.rec_name)) - def get_selection_json(self, name): + @classmethod + def check_selection(cls, schemas): + for schema in schemas: + if schema.type_ != 'selection': + continue + try: + dict(json.loads(schema.get_selection_json())) + except Exception: + raise SelectionError( + gettext('ir.msg_dict_schema_invalid_selection', + schema=schema.rec_name)) + + def get_selection_json(self, name=None): db_selection = self.selection or '' selection = [[w.strip() for w in v.split(':', 1)] for v in db_selection.splitlines() if v] diff --git a/trytond/tests/test_field_dict.py b/trytond/tests/test_field_dict.py index 0405ad4937..f861b6a7f4 100644 --- a/trytond/tests/test_field_dict.py +++ b/trytond/tests/test_field_dict.py @@ -3,6 +3,7 @@ import unittest from trytond import backend +from trytond.model.dictschema import SelectionError from trytond.model.exceptions import RequiredValidationError from trytond.pool import Pool from trytond.tests.test_tryton import activate_module, with_transaction @@ -129,6 +130,20 @@ class FieldDictTestCase(unittest.TestCase): self.assertDictEqual(dict_.dico, {'type': 'arabic'}) + @with_transaction() + def test_invalid_selection_schema(self): + "Test invalid selection schema" + pool = Pool() + DictSchema = pool.get('test.dict.schema') + + with self.assertRaises(SelectionError): + DictSchema.create([{ + 'name': 'selection', + 'string': "Selection", + 'type_': 'selection', + 'selection': 'foo', + }]) + @with_transaction() @unittest.skipIf( backend.name() != 'postgresql', 'jsonb only supported by postgresql')