Validate selection format in Dict schema

issue7947
review257131002
This commit is contained in:
Cédric Krier
2019-03-22 11:46:52 +01:00
parent fb59d8c9b4
commit 07d79c7861
4 changed files with 37 additions and 1 deletions
+1
View File
@@ -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
+3
View File
@@ -109,6 +109,9 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_dict_schema_invalid_domain">
<field name="text">Invalid domain in schema "%(schema)s".</field>
</record>
<record model="ir.message" id="msg_dict_schema_invalid_selection">
<field name="text">Invalid selection in schema "%(schema)s".</field>
</record>
<record model="ir.message" id="msg_recursion_error">
<field name="text">Recursion error: Record "%(rec_name)s" with parent "%(parent_rec_name)s" was configured as ancestor of itself.</field>
</record>
+18 -1
View File
@@ -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]
+15
View File
@@ -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')