ir.model: move the check inside create() and write(), simplfy defaults.
Since the create() function does call cr.commit(), the validity check for selection options shall happen before the call to orm._auto_init(). bzr revid: p_christ@hol.gr-20110107092655-kokkx3pz2ilawyc0
This commit is contained in:
parent
925afa15cd
commit
1a3432b439
|
@ -183,36 +183,41 @@ class ir_model_fields(osv.osv):
|
||||||
}
|
}
|
||||||
_rec_name='field_description'
|
_rec_name='field_description'
|
||||||
_defaults = {
|
_defaults = {
|
||||||
'view_load': lambda *a: 0,
|
'view_load': 0,
|
||||||
'selection': lambda *a: "[('key','label')]",
|
'selection': "",
|
||||||
'domain': lambda *a: "[]",
|
'domain': "[]",
|
||||||
'name': lambda *a: 'x_',
|
'name': 'x_',
|
||||||
'state': lambda self,cr,uid,ctx={}: (ctx and ctx.get('manual',False)) and 'manual' or 'base',
|
'state': lambda self,cr,uid,ctx={}: (ctx and ctx.get('manual',False)) and 'manual' or 'base',
|
||||||
'on_delete': lambda *a: 'set null',
|
'on_delete': 'set null',
|
||||||
'select_level': lambda *a: '0',
|
'select_level': '0',
|
||||||
'size': lambda *a: 64,
|
'size': 64,
|
||||||
'field_description': lambda *a: '',
|
'field_description': '',
|
||||||
'selectable': lambda *a: 1,
|
'selectable': 1,
|
||||||
}
|
}
|
||||||
_order = "name"
|
_order = "name"
|
||||||
|
|
||||||
def _check_selection(self, cr, uid, ids, context=None):
|
def _check_selection(self, cr, uid, selection, context=None):
|
||||||
selection_field = self.browse(cr, uid, ids[0], context=context)
|
|
||||||
try:
|
try:
|
||||||
selection_list = eval(selection_field.selection)
|
selection_list = eval(selection)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.getLogger('ir.model').error('Invalid selection list definition for fields.selection %s', selection_field.name , exc_info=True)
|
logging.getLogger('ir.model').warning('Invalid selection list definition for fields.selection', exc_info=True)
|
||||||
return False
|
raise except_orm(_('Error'),
|
||||||
if not (isinstance(selection_list, list) and selection_list):
|
_("The Selection Options expression is not a valid Pythonic expression." \
|
||||||
return False
|
"Please provide an expression in the [('key','Label'), ...] format."))
|
||||||
for selection_item in selection_list:
|
|
||||||
if not (isinstance(selection_item, (tuple,list)) and len(selection_item) == 2):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
_constraints = [
|
check = True
|
||||||
(_check_selection, "Wrong list of values specified for a field of type selection, it should be written as [('key','value')]", ['selection'])
|
if not (isinstance(selection_list, list) and selection_list):
|
||||||
]
|
check = False
|
||||||
|
else:
|
||||||
|
for item in selection_list:
|
||||||
|
if not (isinstance(item, (tuple,list)) and len(item) == 2):
|
||||||
|
check = False
|
||||||
|
break
|
||||||
|
|
||||||
|
if not check:
|
||||||
|
raise except_orm(_('Error'),
|
||||||
|
_("The Selection Options expression is must be in the [('key','Label'), ...] format!"))
|
||||||
|
return True
|
||||||
|
|
||||||
def _size_gt_zero_msg(self, cr, user, ids, context=None):
|
def _size_gt_zero_msg(self, cr, user, ids, context=None):
|
||||||
return _('Size of the field can never be less than 1 !')
|
return _('Size of the field can never be less than 1 !')
|
||||||
|
@ -220,6 +225,7 @@ class ir_model_fields(osv.osv):
|
||||||
_sql_constraints = [
|
_sql_constraints = [
|
||||||
('size_gt_zero', 'CHECK (size>0)',_size_gt_zero_msg ),
|
('size_gt_zero', 'CHECK (size>0)',_size_gt_zero_msg ),
|
||||||
]
|
]
|
||||||
|
|
||||||
def unlink(self, cr, user, ids, context=None):
|
def unlink(self, cr, user, ids, context=None):
|
||||||
for field in self.browse(cr, user, ids, context):
|
for field in self.browse(cr, user, ids, context):
|
||||||
if field.state <> 'manual':
|
if field.state <> 'manual':
|
||||||
|
@ -239,6 +245,10 @@ class ir_model_fields(osv.osv):
|
||||||
context = {}
|
context = {}
|
||||||
if context and context.get('manual',False):
|
if context and context.get('manual',False):
|
||||||
vals['state'] = 'manual'
|
vals['state'] = 'manual'
|
||||||
|
if vals.get('ttype', False) == 'selection':
|
||||||
|
if not vals.get('selection',False):
|
||||||
|
raise except_orm(_('Error'), _('For selection fields, the Selection Options must be given!'))
|
||||||
|
self._check_selection(cr, user, vals['selection'], context=context)
|
||||||
res = super(ir_model_fields,self).create(cr, user, vals, context)
|
res = super(ir_model_fields,self).create(cr, user, vals, context)
|
||||||
if vals.get('state','base') == 'manual':
|
if vals.get('state','base') == 'manual':
|
||||||
if not vals['name'].startswith('x_'):
|
if not vals['name'].startswith('x_'):
|
||||||
|
@ -256,6 +266,22 @@ class ir_model_fields(osv.osv):
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def write(self, cr, user, ids, vals, context=None):
|
||||||
|
# TODO: we have to check/restrict many more conditions here, like
|
||||||
|
# changing the type, name or model_id of fields eg.
|
||||||
|
|
||||||
|
if 'selection' in vals:
|
||||||
|
have_selection = False
|
||||||
|
for item in self.browse(cr, user, ids, context=context):
|
||||||
|
if item.ttype == 'selection':
|
||||||
|
have_selection = True
|
||||||
|
break
|
||||||
|
if have_selection:
|
||||||
|
self._check_selection(cr, user, vals['selection'], context=context)
|
||||||
|
res = super(ir_model_fields,self).write(cr, user, ids, vals, context=context)
|
||||||
|
# TODO do we need to perform _auto_init here, too?
|
||||||
|
return res
|
||||||
|
|
||||||
ir_model_fields()
|
ir_model_fields()
|
||||||
|
|
||||||
class ir_model_access(osv.osv):
|
class ir_model_access(osv.osv):
|
||||||
|
|
Loading…
Reference in New Issue