[IMP] Yaml scripts calls on_changes and fields_view_get by defaults
Very big change, this may emphasize some of the troubles, runbot can become red until we fix them all. bzr revid: fp@tinyerp.com-20111114212627-5kdjxkws1lqm0c38
This commit is contained in:
parent
95c31ad01c
commit
8d7176a11e
|
@ -150,7 +150,6 @@ class res_partner(osv.osv):
|
|||
_defaults = {
|
||||
'active': lambda *a: 1,
|
||||
'customer': lambda *a: 1,
|
||||
'address': [{'type': 'default'}],
|
||||
'category_id': _default_category,
|
||||
'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'res.partner', context=c),
|
||||
'color': 0,
|
||||
|
|
|
@ -206,6 +206,7 @@ class users(osv.osv):
|
|||
return dict.fromkeys(ids, '')
|
||||
|
||||
_columns = {
|
||||
'id': fields.integer('ID'),
|
||||
'name': fields.char('User Name', size=64, required=True, select=True,
|
||||
help="The new user's real name, used for searching"
|
||||
" and most listings"),
|
||||
|
@ -755,6 +756,28 @@ class users_view(osv.osv):
|
|||
return res
|
||||
return super(users_view, self).read(cr, uid, ids, fields, context, load)
|
||||
|
||||
def fields_get(self, cr, user, allfields=None, context=None, write_access=True):
|
||||
res = super(users_view, self).fields_get(cr, user, allfields, context, write_access)
|
||||
apps, others = self.pool.get('res.groups').get_classified(cr, user, context)
|
||||
for app, groups in apps:
|
||||
ids = [g.id for name, g in groups]
|
||||
app_name = name_boolean_groups(ids)
|
||||
sel_name = name_selection_groups(ids)
|
||||
selection = [(g.id, name) for name, g in groups]
|
||||
res[app_name] = {'type': 'boolean', 'string': app}
|
||||
tips = [name + ': ' + (g.comment or '') for name, g in groups]
|
||||
if tips:
|
||||
res[app_name].update(help='\n'.join(tips))
|
||||
res[sel_name] = {'type': 'selection', 'string': 'Group', 'selection': selection}
|
||||
|
||||
for sec, groups in others:
|
||||
for gname, g in groups:
|
||||
name = name_boolean_group(g.id)
|
||||
res[name] = {'type': 'boolean', 'string': gname}
|
||||
if g.comment:
|
||||
res[name].update(help=g.comment)
|
||||
return res
|
||||
|
||||
def fields_view_get(self, cr, uid, view_id=None, view_type='form',
|
||||
context=None, toolbar=False, submenu=False):
|
||||
# in form views, transform 'groups_id' into reified group fields
|
||||
|
|
|
@ -302,7 +302,7 @@ class YamlInterpreter(object):
|
|||
|
||||
def create_osv_memory_record(self, record, fields):
|
||||
model = self.get_model(record.model)
|
||||
record_dict = self._create_record(model, fields)
|
||||
record_dict = self._create_record(model, fields, False)
|
||||
id_new=model.create(self.cr, self.uid, record_dict, context=self.context)
|
||||
self.id_map[record.id] = int(id_new)
|
||||
return record_dict
|
||||
|
@ -313,13 +313,19 @@ class YamlInterpreter(object):
|
|||
model = self.get_model(record.model)
|
||||
|
||||
view_id = record.view
|
||||
if record.view:
|
||||
if view_id and (view_id is not True):
|
||||
view_id = self.pool.get('ir.model.data')._get_id(self.cr, 1, self.module, record.view)
|
||||
|
||||
if model.is_transient():
|
||||
record_dict=self.create_osv_memory_record(record, fields)
|
||||
else:
|
||||
self.validate_xml_id(record.id)
|
||||
try:
|
||||
self.pool.get('ir.model.data')._get_id(self.cr, 1, self.module, record.id)
|
||||
default = False
|
||||
except ValueError:
|
||||
default = True
|
||||
|
||||
if self.isnoupdate(record) and self.mode != 'init':
|
||||
id = self.pool.get('ir.model.data')._update_dummy(self.cr, 1, record.model, self.module, record.id)
|
||||
# check if the resource already existed at the last update
|
||||
|
@ -332,26 +338,29 @@ class YamlInterpreter(object):
|
|||
|
||||
|
||||
#context = self.get_context(record, self.eval_context)
|
||||
context = record.context #TOFIX: record.context like {'withoutemployee':True} should pass from self.eval_context. example: test_project.yml in project module
|
||||
if view_id is not None:
|
||||
view = model.fields_view_get(self.cr, 1, view_id, 'form', context)
|
||||
#TOFIX: record.context like {'withoutemployee':True} should pass from self.eval_context. example: test_project.yml in project module
|
||||
context = record.context
|
||||
if view_id:
|
||||
varg = view_id
|
||||
if view_id is True: varg = False
|
||||
view = model.fields_view_get(self.cr, 1, varg, 'form', context)
|
||||
view_id = etree.fromstring(view['arch'].encode('utf-8'))
|
||||
|
||||
record_dict = self._create_record(model, fields, view_id)
|
||||
record_dict = self._create_record(model, fields, view_id, default=default)
|
||||
self.logger.debug("RECORD_DICT %s" % record_dict)
|
||||
id = self.pool.get('ir.model.data')._update(self.cr, 1, record.model, \
|
||||
self.module, record_dict, record.id, noupdate=self.isnoupdate(record), mode=self.mode, context=context)
|
||||
if record.test:
|
||||
if record.test: # TODO: remove this
|
||||
model.read(cr, 1, [id], context=self.context)
|
||||
model.name_search(cr, 1, [('id','=',id)], context=self.context)
|
||||
self.id_map[record.id] = int(id)
|
||||
if config.get('import_partial'):
|
||||
self.cr.commit()
|
||||
|
||||
def _create_record(self, model, fields, view=None, parent={}):
|
||||
def _create_record(self, model, fields, view=False, parent={}, default=True):
|
||||
allfields = model.fields_get(self.cr, 1, context=self.context)
|
||||
if view is not None:
|
||||
defaults = model.default_get(self.cr, 1, allfields, context=self.context)
|
||||
if view is not False:
|
||||
defaults = default and model.default_get(self.cr, 1, allfields, context=self.context) or {}
|
||||
fg = model.fields_get(self.cr, 1, context=self.context)
|
||||
else:
|
||||
default = {}
|
||||
|
@ -359,25 +368,37 @@ class YamlInterpreter(object):
|
|||
record_dict = {}
|
||||
fields = fields or {}
|
||||
|
||||
def process_val(key, val):
|
||||
if fg[key]['type']=='many2one':
|
||||
if type(val) in (tuple,list):
|
||||
val = val[0]
|
||||
elif (fg[key]['type']=='one2many') and len(val):
|
||||
if type(val[0]) == dict:
|
||||
val = map(lambda x: (0,0,x), val)
|
||||
return val
|
||||
|
||||
# Process all on_change calls
|
||||
nodes = view and [view] or []
|
||||
nodes = (view is not False) and [view] or []
|
||||
while nodes:
|
||||
el = nodes.pop(0)
|
||||
if el.tag=='field':
|
||||
field_name = el.attrib['name']
|
||||
assert field_name in fg, "The field '%s' is defined in the form view but not on the object '%s'!" % (field_name, model._name)
|
||||
if field_name in fields:
|
||||
view2 = None
|
||||
# if the form view is not inline, we call fields_view_get
|
||||
if view and (fg[field_name]['type']=='one2many'):
|
||||
if (view is not False) and (fg[field_name]['type']=='one2many'):
|
||||
view2 = view.find("field[@name='%s']/form"%(field_name,))
|
||||
if not view2:
|
||||
view2 = self.pool.get(fg[field_name]['relation']).fields_view_get(self.cr, 1, False, 'form', self.context)
|
||||
view2 = etree.fromstring(view2['arch'].encode('utf-8'))
|
||||
|
||||
field_value = self._eval_field(model, field_name, fields[field_name], view2, parent=record_dict)
|
||||
field_value = self._eval_field(model, field_name, fields[field_name], view2, parent=record_dict, default=default)
|
||||
#if (field_name in record_dict) and record_dict[field_name] == field_value:
|
||||
# print 'WARNING ***', field_name,
|
||||
record_dict[field_name] = field_value
|
||||
elif (field_name in defaults) and (field_name not in record_dict):
|
||||
record_dict[field_name] = defaults[field_name]
|
||||
record_dict[field_name] = process_val(field_name, defaults[field_name])
|
||||
else:
|
||||
continue
|
||||
|
||||
|
@ -404,16 +425,17 @@ class YamlInterpreter(object):
|
|||
# Evaluation args
|
||||
args = map(lambda x: eval(x, ctx), match.group(2).split(','))
|
||||
result = getattr(model, match.group(1))(self.cr, 1, [], *args)
|
||||
for key, val in result.get('value', {}).items():
|
||||
for key, val in (result or {}).get('value', {}).items():
|
||||
if key not in fields:
|
||||
record_dict[key] = val
|
||||
assert key in fg, "The returning field '%s' from your on_change call '%s' does not exist on the object '%s'" % (key, match.group(1), model._name)
|
||||
record_dict[key] = process_val(key, val)
|
||||
else:
|
||||
nodes = list(el) + nodes
|
||||
|
||||
for field_name, expression in fields.items():
|
||||
if field_name in record_dict:
|
||||
continue
|
||||
field_value = self._eval_field(model, field_name, expression)
|
||||
field_value = self._eval_field(model, field_name, expression, default=False)
|
||||
record_dict[field_name] = field_value
|
||||
|
||||
return record_dict
|
||||
|
@ -444,7 +466,7 @@ class YamlInterpreter(object):
|
|||
def process_eval(self, node):
|
||||
return eval(node.expression, self.eval_context)
|
||||
|
||||
def _eval_field(self, model, field_name, expression, view=None, parent={}):
|
||||
def _eval_field(self, model, field_name, expression, view=False, parent={}, default=True):
|
||||
# TODO this should be refactored as something like model.get_field() in bin/osv
|
||||
if field_name in model._columns:
|
||||
column = model._columns[field_name]
|
||||
|
@ -465,7 +487,7 @@ class YamlInterpreter(object):
|
|||
value = self.get_id(expression)
|
||||
elif column._type == "one2many":
|
||||
other_model = self.get_model(column._obj)
|
||||
value = [(0, 0, self._create_record(other_model, fields, view, parent)) for fields in expression]
|
||||
value = [(0, 0, self._create_record(other_model, fields, view, parent, default=default)) for fields in expression]
|
||||
elif column._type == "many2many":
|
||||
ids = [self.get_id(xml_id) for xml_id in expression]
|
||||
value = [(6, 0, ids)]
|
||||
|
|
|
@ -24,13 +24,14 @@ class Assert(YamlTag):
|
|||
super(Assert, self).__init__(**kwargs)
|
||||
|
||||
class Record(YamlTag):
|
||||
def __init__(self, model, id, use='id', **kwargs):
|
||||
def __init__(self, model, id, use='id', view=True, **kwargs):
|
||||
self.model = model
|
||||
self.id = id
|
||||
self.view = view
|
||||
super(Record, self).__init__(**kwargs)
|
||||
def __str__(self):
|
||||
return '!record {model: %s, id: %s}:' % (str(self.model,), str(self.id,))
|
||||
|
||||
|
||||
class Python(YamlTag):
|
||||
def __init__(self, model, severity=logging.ERROR, name="", **kwargs):
|
||||
self.model= model
|
||||
|
|
Loading…
Reference in New Issue