[FIX] YAML: Add support for `reference` fields in YAML

YAML import was missing support for loading values into fields
where `field.type == 'reference'`.
This commit is contained in:
Leonardo Rochael Almeida 2015-06-03 03:59:37 +02:00 committed by Martin Trigaux
parent 11886f4d9f
commit 39745a4290
3 changed files with 56 additions and 2 deletions

View File

@ -11,5 +11,8 @@
'data': ['ir.model.access.csv'],
'installable': True,
'auto_install': False,
'test': [
'tests/test_import_reference.yml',
]
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,44 @@
- |
YAML Import reference scenario:
Check that importing into a "reference" type field works with YAML using
both implicit references and !ref.
- |
Given records imitating menu entries similar to the following entry from the base module:
<menuitem id="menu_module_tree" parent="menu_management" name="Local Modules"
sequence="5" action="open_module_tree" />
taken from openerp/addons/base/module/module_view.xml
- >
A Menu item using !record and implicit xml_id lookup and sequence cast:
-
!record { model: ir.ui.menu, id: test_menu_0 }:
name: "Local Modules 0"
parent_id: base.menu_management
sequence: 90
action: base.open_module_tree
- >
A Menu item using !record and explicit !ref and sequence !eval
-
!record { model: ir.ui.menu, id: test_menu_1 }:
name: "Local Modules 1"
parent_id: !refid base.menu_management
sequence: !eval 91
action: !refid base.open_module_tree
- >
Then these menu items should be present, properly configured and
pointing to the same action.
-
!assert { model: ir.ui.menu, id: test_menu_0, string: menu item 0 is properly configured }:
- name == 'Local Modules 0'
- sequence == 90
- parent_id.name == 'Modules'
- action.name == 'Local Modules'
-
!assert { model: ir.ui.menu, id: test_menu_1, string: menu item 1 is properly configured }:
- name == 'Local Modules 1'
- sequence == 91
- parent_id.name == 'Modules'
- action.name == 'Local Modules'

View File

@ -500,7 +500,11 @@ class YamlInterpreter(object):
else:
value = ids
elif node.id:
value = self.get_id(node.id)
if field and field.type == 'reference':
record = self.get_record(node.id)
value = "%s,%s" % (record._name, record.id)
else:
value = self.get_id(node.id)
else:
value = None
return value
@ -518,7 +522,7 @@ class YamlInterpreter(object):
elements = self.process_ref(expression, field)
if field.type in ("many2many", "one2many"):
value = [(6, 0, elements)]
else: # many2one
else: # many2one or reference
if isinstance(elements, (list,tuple)):
value = self._get_first_result(elements)
else:
@ -539,6 +543,9 @@ class YamlInterpreter(object):
# enforce ISO format for string datetime values, to be locale-agnostic during tests
time.strptime(expression, misc.DEFAULT_SERVER_DATETIME_FORMAT)
value = expression
elif field.type == "reference":
record = self.get_record(expression)
value = "%s,%s" % (record._name, record.id)
else: # scalar field
if is_eval(expression):
value = self.process_eval(expression)