Merge lp:~camptocamp/openerp-connector/7.0-modifier-shadow-name-1322645 into lp:~openerp-connector-core-editors/openerp-connector/7.0

Proposed by Guewen Baconnier @ Camptocamp
Status: Merged
Approved by: Guewen Baconnier @ Camptocamp
Approved revision: 624
Merged at revision: 631
Proposed branch: lp:~camptocamp/openerp-connector/7.0-modifier-shadow-name-1322645
Merge into: lp:~openerp-connector-core-editors/openerp-connector/7.0
Diff against target: 43 lines (+24/-1)
2 files modified
connector/tests/test_mapper.py (+16/-0)
connector/unit/mapper.py (+8/-1)
To merge this branch: bzr merge lp:~camptocamp/openerp-connector/7.0-modifier-shadow-name-1322645
Reviewer Review Type Date Requested Status
OpenERP Connector Core Editors Pending
Review via email: mp+220812@code.launchpad.net

Commit message

extract the attribute name from the closure when a modifier is used in a 'direct' mapping

Description of the change

Fixes: lp:1322645

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'connector/tests/test_mapper.py'
2--- connector/tests/test_mapper.py 2013-11-12 09:02:25 +0000
3+++ connector/tests/test_mapper.py 2014-05-23 14:41:23 +0000
4@@ -558,3 +558,19 @@
5 'test': .5})]
6 }
7 self.assertEqual(map_record.values(for_create=True), expected)
8+
9+ def test_modifier_filter_field(self):
10+ """ A direct mapping with a modifier must still be considered from the list of fields """
11+ class MyMapper(ImportMapper):
12+ direct = [('field', 'field2'),
13+ ('no_field', 'no_field2'),
14+ (convert('name', int), 'out_name')]
15+
16+ env = mock.MagicMock()
17+ record = {'name': '300', 'field': 'value', 'no_field': 'no_value'}
18+ mapper = MyMapper(env)
19+ map_record = mapper.map_record(record)
20+ expected = {'out_name': 300, 'field2': 'value'}
21+ self.assertEqual(map_record.values(fields=['field', 'name']), expected)
22+ self.assertEqual(map_record.values(for_create=True,
23+ fields=['field', 'name']), expected)
24
25=== modified file 'connector/unit/mapper.py'
26--- connector/unit/mapper.py 2014-03-19 09:41:36 +0000
27+++ connector/unit/mapper.py 2014-05-23 14:41:23 +0000
28@@ -659,7 +659,14 @@
29 for_create = self.options.for_create
30 result = {}
31 for from_attr, to_attr in self.direct:
32- if (not fields or from_attr in fields):
33+ if callable(from_attr): # in a modifier
34+ # the name of the attribute is the first arg of the
35+ # closure
36+ attr_name = from_attr.__closure__[0].cell_contents
37+ else:
38+ attr_name = from_attr
39+
40+ if (not fields or attr_name in fields):
41 value = self._map_direct(map_record.source,
42 from_attr,
43 to_attr)