[REf] yaml_import.py: refactoring of _create_record()

bzr revid: qdp-launchpad@openerp.com-20121004115617-ihb4j3tdeco68vob
This commit is contained in:
Quentin (OpenERP) 2012-10-04 13:56:17 +02:00
parent bcf6dccf2d
commit 3d1b4398bf
1 changed files with 55 additions and 56 deletions

View File

@ -328,16 +328,6 @@ class YamlInterpreter(object):
self.cr.commit()
def _create_record(self, model, fields, view_info=False, parent={}, default=True):
view = view_info and etree.fromstring(view_info['arch'].encode('utf-8')) or False
if view is not False:
defaults = default and model._add_missing_default_values(self.cr, SUPERUSER_ID, {}, context=self.context) or {}
fg = model.fields_get(self.cr, SUPERUSER_ID, context=self.context)
else:
defaults = {}
fg = {}
record_dict = defaults.copy()
fields = fields or {}
def process_val(key, val):
if fg[key]['type']=='many2one':
if type(val) in (tuple,list):
@ -349,61 +339,70 @@ class YamlInterpreter(object):
val = map(lambda x: (0,0,x), val)
return val
# Process all on_change calls
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:
one2many_form_view = None
# if the form view is not inline, we call fields_view_get
if (view is not False) and (fg[field_name]['type']=='one2many'):
one2many_form_view = view_info['fields'][field_name]['views'].get('form')
if not one2many_form_view:
one2many_form_view = self.pool.get(fg[field_name]['relation']).fields_view_get(self.cr, SUPERUSER_ID, False, 'form', self.context)
view = view_info and etree.fromstring(view_info['arch'].encode('utf-8')) or False
fields = fields or {}
if view is not False:
defaults = default and model._add_missing_default_values(self.cr, SUPERUSER_ID, fields, context=self.context) or {}
fg = view_info['fields']
# copy the default values in record_dict, only if they are in the view (because that's what the client does)
# the other default values will be added later on by the create().
record_dict = dict([(key, val) for key, val in defaults.items() if key in fg])
# Process all on_change calls
nodes = [view]
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:
one2many_form_view = None
# if the form view is not inline, we call fields_view_get
if (view is not False) and (fg[field_name]['type']=='one2many'):
one2many_form_view = view_info['fields'][field_name]['views'].get('form')
if not one2many_form_view:
one2many_form_view = self.pool.get(fg[field_name]['relation']).fields_view_get(self.cr, SUPERUSER_ID, False, 'form', self.context)
field_value = self._eval_field(model, field_name, fields[field_name], one2many_form_view or view_info, parent=record_dict, default=default)
record_dict[field_name] = field_value
#if (field_name in defaults) and defaults[field_name] == field_value:
# print '*** You can remove these lines:', field_name, field_value
field_value = self._eval_field(model, field_name, fields[field_name], one2many_form_view or view_info, parent=record_dict, default=default)
record_dict[field_name] = field_value
#if (field_name in defaults) and defaults[field_name] == field_value:
# print '*** You can remove these lines:', field_name, field_value
#if field_name has a default value or a value is given in the yaml file, we must call its on_change()
elif field_name not in defaults:
continue
#if field_name has a default value or a value is given in the yaml file, we must call its on_change()
elif field_name not in defaults:
continue
if not el.attrib.get('on_change', False):
continue
match = re.match("([a-z_1-9A-Z]+)\((.*)\)", el.attrib['on_change'])
assert match, "Unable to parse the on_change '%s'!" % (el.attrib['on_change'], )
if not el.attrib.get('on_change', False):
continue
match = re.match("([a-z_1-9A-Z]+)\((.*)\)", el.attrib['on_change'])
assert match, "Unable to parse the on_change '%s'!" % (el.attrib['on_change'], )
# creating the context
class parent2(object):
def __init__(self, d):
self.d = d
def __getattr__(self, name):
return self.d.get(name, False)
# creating the context
class parent2(object):
def __init__(self, d):
self.d = d
def __getattr__(self, name):
return self.d.get(name, False)
ctx = record_dict.copy()
ctx['context'] = self.context
ctx['uid'] = SUPERUSER_ID
ctx['parent'] = parent2(parent)
for a in fg:
if a not in ctx:
ctx[a]=process_val(a, defaults.get(a, False))
ctx = record_dict.copy()
ctx['context'] = self.context
ctx['uid'] = SUPERUSER_ID
ctx['parent'] = parent2(parent)
for a in fg:
if a not in ctx:
ctx[a]=process_val(a, defaults.get(a, False))
# Evaluation args
args = map(lambda x: eval(x, ctx), match.group(2).split(','))
result = getattr(model, match.group(1))(self.cr, SUPERUSER_ID, [], *args)
for key, val in (result or {}).get('value', {}).items():
if key not in fields:
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)
# Evaluation args
args = map(lambda x: eval(x, ctx), match.group(2).split(','))
result = getattr(model, match.group(1))(self.cr, SUPERUSER_ID, [], *args)
for key, val in (result or {}).get('value', {}).items():
assert key in fg, "The returning field '%s' from your on_change call '%s' does not exist on the object '%s' or in its view named '%s'" % (key, match.group(1), model._name, view_info['name'])
record_dict[key] = process_val(key, val)
#if (key in fields) and record_dict[key] == process_val(key, val):
# print '*** You can remove these lines:', key, val
else:
nodes = list(el) + nodes
else:
nodes = list(el) + nodes
else:
record_dict = {}
for field_name, expression in fields.items():
if field_name in record_dict: