Factorize selection validation
review603002
This commit is contained in:
1 parent
1a362c3879
commit
5fc611575c
7 files changed
+52
-51
No files matched your search
@@ -141,6 +141,16 @@ this repository contains the full copyright notices and license terms. -->
|
||||
<field name="module">ir</field>
|
||||
<field name="fuzzy" eval="False"/>
|
||||
</record>
|
||||
<record model="ir.translation"
|
||||
id="translation_selection_validation_record">
|
||||
<field name="name">selection_validation_record</field>
|
||||
<field name="lang">en_US</field>
|
||||
<field name="type">error</field>
|
||||
<field name="src">The field "%s" on "%s" is not in the selection.</field>
|
||||
<field name="value">The field "%s" on "%s" is not in the selection.</field>
|
||||
<field name="module">ir</field>
|
||||
<field name="fuzzy" eval="False"/>
|
||||
</record>
|
||||
<record model="ir.translation" id="translation_time_format_validation_record">
|
||||
<field name="name">time_format_validation_record</field>
|
||||
<field name="lang">en_US</field>
|
||||
|
||||
@@ -297,23 +297,6 @@ class ModelSQL(ModelStorage):
|
||||
upd2.append(FIELDS[field._type].sql_format(value))
|
||||
else:
|
||||
upd_todo.append(fname)
|
||||
if (hasattr(field, 'selection')
|
||||
and field.selection
|
||||
and value):
|
||||
if field._type == 'reference':
|
||||
if isinstance(value, basestring):
|
||||
value = value.split(',')[0]
|
||||
else:
|
||||
value, _ = value
|
||||
if isinstance(field.selection, (tuple, list)):
|
||||
test = dict(field.selection)
|
||||
else:
|
||||
test = dict(getattr(cls, field.selection)())
|
||||
if value not in test:
|
||||
raise Exception('ValidateError',
|
||||
'The value "%s" for the field "%s" '
|
||||
'is not in the selection'
|
||||
% (value, fname))
|
||||
upd0 += ', create_uid, create_date'
|
||||
upd1 += ', %s, %s'
|
||||
upd2.append(Transaction().user)
|
||||
@@ -788,30 +771,6 @@ class ModelSQL(ModelStorage):
|
||||
upd_todo.append(field)
|
||||
else:
|
||||
updend.append(field)
|
||||
if field in cls._fields \
|
||||
and hasattr(cls._fields[field], 'selection') \
|
||||
and cls._fields[field].selection \
|
||||
and values[field]:
|
||||
if cls._fields[field]._type == 'reference':
|
||||
if isinstance(values[field], basestring):
|
||||
val = values[field].split(',')[0]
|
||||
else:
|
||||
val, _ = values[field]
|
||||
else:
|
||||
val = values[field]
|
||||
if isinstance(cls._fields[field].selection, (tuple, list)):
|
||||
if val not in dict(cls._fields[field].selection):
|
||||
raise Exception('ValidateError',
|
||||
'The value "%s" for the field "%s" ' \
|
||||
'is not in the selection' % \
|
||||
(val, field))
|
||||
else:
|
||||
if val not in dict(getattr(cls,
|
||||
cls._fields[field].selection)()):
|
||||
raise Exception('ValidateError',
|
||||
'The value "%s" for the field "%s" ' \
|
||||
'is not in the selection' % \
|
||||
(val, field))
|
||||
|
||||
upd0.append(('write_uid', '%s'))
|
||||
upd0.append(('write_date', '%s'))
|
||||
|
||||
@@ -1059,6 +1059,28 @@ class ModelStorage(Model):
|
||||
digits_test(getattr(record, field_name),
|
||||
field.digits, field_name)
|
||||
|
||||
# validate selection
|
||||
if hasattr(field, 'selection') and field.selection:
|
||||
if isinstance(field.selection, (tuple, list)):
|
||||
test = set(dict(field.selection).keys())
|
||||
else:
|
||||
test = set(dict(getattr(cls,
|
||||
field.selection)()).keys())
|
||||
# None and '' are equivalent
|
||||
if '' in test or None in test:
|
||||
test.add('')
|
||||
test.add(None)
|
||||
for record in records:
|
||||
value = getattr(record, field_name)
|
||||
if field._type == 'reference':
|
||||
if isinstance(value, ModelStorage):
|
||||
value = value.__class__.__name__
|
||||
elif value:
|
||||
value, _ = value.split(',')
|
||||
if value not in test:
|
||||
cls.raise_user_error('selection_validation_record',
|
||||
error_args=cls._get_error_args(field_name))
|
||||
|
||||
def format_test(value, format, field_name):
|
||||
if not value:
|
||||
return
|
||||
|
||||
@@ -26,6 +26,7 @@ try:
|
||||
TIMEZONES = [(x, x) for x in pytz.common_timezones]
|
||||
except ImportError:
|
||||
TIMEZONES = []
|
||||
TIMEZONES += [(None, '')]
|
||||
|
||||
__all__ = [
|
||||
'User', 'UserAction', 'UserGroup', 'Warning_',
|
||||
|
||||
@@ -29,9 +29,10 @@ class ExportData(ModelSQL):
|
||||
date = fields.Date('Date')
|
||||
datetime = fields.DateTime('DateTime')
|
||||
selection = fields.Selection([
|
||||
('select1', 'Select 1'),
|
||||
('select2', 'Select 2'),
|
||||
], 'Selection')
|
||||
(None, ''),
|
||||
('select1', 'Select 1'),
|
||||
('select2', 'Select 2'),
|
||||
], 'Selection')
|
||||
many2one = fields.Many2One('test.export_data.target',
|
||||
'Many2One')
|
||||
many2many = fields.Many2Many('test.export_data.relation',
|
||||
@@ -39,8 +40,9 @@ class ExportData(ModelSQL):
|
||||
one2many = fields.One2Many('test.export_data.target', 'one2many',
|
||||
'One2Many')
|
||||
reference = fields.Reference('Reference', [
|
||||
('test.export_data.target', 'Target'),
|
||||
])
|
||||
(None, ''),
|
||||
('test.export_data.target', 'Target'),
|
||||
])
|
||||
|
||||
|
||||
class ExportDataTarget2:
|
||||
|
||||
@@ -73,9 +73,10 @@ class ImportDataSelection(ModelSQL):
|
||||
"Import Data Selection"
|
||||
__name__ = 'test.import_data.selection'
|
||||
selection = fields.Selection([
|
||||
('select1', 'Select 1'),
|
||||
('select2', 'Select 2'),
|
||||
], 'Selection')
|
||||
(None, ''),
|
||||
('select1', 'Select 1'),
|
||||
('select2', 'Select 2'),
|
||||
], 'Selection')
|
||||
|
||||
|
||||
class ImportDataMany2OneTarget(ModelSQL):
|
||||
@@ -136,5 +137,6 @@ class ImportDataReference(ModelSQL):
|
||||
"Import Data Reference"
|
||||
__name__ = 'test.import_data.reference'
|
||||
reference = fields.Reference('Reference', [
|
||||
('test.import_data.reference.selection', 'Test'),
|
||||
])
|
||||
(None, ''),
|
||||
('test.import_data.reference.selection', 'Test'),
|
||||
])
|
||||
@@ -429,6 +429,7 @@ class One2ManyReferenceTarget(ModelSQL):
|
||||
__name__ = 'test.one2many_reference.target'
|
||||
name = fields.Char('Name', required=True)
|
||||
origin = fields.Reference('Origin', [
|
||||
(None, ''),
|
||||
('test.one2many_reference', 'One2Many Reference'),
|
||||
])
|
||||
|
||||
@@ -520,6 +521,7 @@ class Many2ManyReferenceRelation(ModelSQL):
|
||||
'Many2Many Relation'
|
||||
__name__ = 'test.many2many_reference.relation'
|
||||
origin = fields.Reference('Origin', [
|
||||
(None, ''),
|
||||
('test.many2many_reference', 'Many2Many Reference'),
|
||||
])
|
||||
target = fields.Many2One('test.many2many_reference.target',
|
||||
@@ -551,6 +553,7 @@ class Reference(ModelSQL):
|
||||
__name__ = 'test.reference'
|
||||
name = fields.Char('Name', required=True)
|
||||
reference = fields.Reference('Reference', selection=[
|
||||
(None, ''),
|
||||
('test.reference.target', 'Target'),
|
||||
])
|
||||
|
||||
@@ -566,6 +569,7 @@ class ReferenceRequired(ModelSQL):
|
||||
__name__ = 'test.reference_required'
|
||||
name = fields.Char('Name', required=True)
|
||||
reference = fields.Reference('Reference', selection=[
|
||||
(None, ''),
|
||||
('test.reference.target', 'Target'),
|
||||
], required=True)
|
||||
|
||||
@@ -578,6 +582,7 @@ class Property(ModelSQL):
|
||||
'Test Many2One'))
|
||||
numeric = fields.Property(fields.Numeric('Test Numeric'))
|
||||
selection = fields.Property(fields.Selection([
|
||||
(None, ''),
|
||||
('option_a', 'Option A'),
|
||||
('option_b', 'Option B')
|
||||
], 'Test Selection'))
|
||||
Reference in new issue
Block a user