From 4d0a189796e645003e55ac82b0804fce5fb54ae0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 11 Oct 2011 10:12:02 +0200 Subject: [PATCH] [IMP] handle case where o2m commands are pairs instead of triplets bzr revid: xmo@openerp.com-20111011081202-y1ou74u038o2kl58 --- openerp/osv/orm.py | 16 ++++++++++------ tests/test_orm.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index a17eb49e8b3..7cd2cbc22c5 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -4785,12 +4785,16 @@ class BaseModel(object): """ o2m_model = self._all_columns[field_name].column._obj - # extract and handle case of single ids (instead of commands): - # convert to LINK_TO commands (4) - c1, c2 = itertools.tee(o2m_commands) - commands = list(itertools.chain( - (command for command in c1 if isinstance(command, (list, tuple))), - ((4, id, None) for id in c2 if not isinstance(id, (list, tuple))))) + # convert single ids and pairs to tripled commands + commands = [] + for o2m_command in o2m_commands: + if not isinstance(o2m_command, (list, tuple)): + commands.append((4, o2m_command, False)) + elif len(o2m_command) == 2: + command, id = o2m_command + commands.append((command, id, False)) + else: + commands.append(o2m_command) # extract records to read, by id, in a mapping dict ids_to_read = [id for (command, id, _) in commands if command in (1, 4)] diff --git a/tests/test_orm.py b/tests/test_orm.py index 365ac95ee4d..ff82b779cd9 100644 --- a/tests/test_orm.py +++ b/tests/test_orm.py @@ -119,3 +119,21 @@ class TestO2MSerialization(unittest2.TestCase): {'name': 'grault'}, {'id': ids[5], 'name': 'garply'} ]) + + def test_LINK_TO_pairs(self): + "LINK_TO commands can be written as pairs, instead of triplets" + ids = [ + self.address.create(self.cr, UID, {'name': 'foo'}), + self.address.create(self.cr, UID, {'name': 'bar'}), + self.address.create(self.cr, UID, {'name': 'baz'}) + ] + commands = map(lambda id: (4, id), ids) + + results = list(self.partner.resolve_o2m_commands_to_record_dicts( + self.cr, UID, 'address', commands, ['name'])) + + self.assertEqual(results, [ + {'id': ids[0], 'name': 'foo'}, + {'id': ids[1], 'name': 'bar'}, + {'id': ids[2], 'name': 'baz'} + ])