Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapper: add modifier to follow m2o relations #94

Merged
merged 2 commits into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions connector/tests/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
changed_by,
only_create,
convert,
follow_m2o_relations,
m2o_to_backend,
backend_to_m2o,
none,
Expand Down Expand Up @@ -502,6 +503,34 @@ def no_decorator(self):
set(['street', 'in_t', 'in_f', 'name', 'city', 'email']))


class test_mapper_recordsets(common.TransactionCase):
""" Test mapper with "real" records instead of mocks """

def setUp(self):
super(test_mapper_recordsets, self).setUp()
self.session = ConnectorSession(self.cr, self.uid)
self.backend = mock.Mock(wraps=Backend('x', version='y'),
name='backend')
backend_record = mock.Mock()
backend_record.get_backend.return_value = self.backend
self.connector_env = ConnectorEnvironment(
backend_record, self.session, 'res.partner')

def test_mapping_modifier_follow_m2o_relations(self):
""" Map with the follow_m2o_relations modifier """
class MyMapper(ImportMapper):
direct = [
(follow_m2o_relations('parent_id.name'), 'parent_name'),
]

partner = self.browse_ref('base.res_partner_address_4')
mapper = MyMapper(self.connector_env)
map_record = mapper.map_record(partner)
expected = {'parent_name': 'Agrolait'}
self.assertEqual(map_record.values(), expected)
self.assertEqual(map_record.values(for_create=True), expected)


class test_mapper_binding(common.TransactionCase):
""" Test Mapper with Bindings"""

Expand Down
22 changes: 22 additions & 0 deletions connector/unit/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ def modifier(self, record, to_attr):
return modifier


def follow_m2o_relations(field):
"""A modifier intended to be used on ``direct`` mappings.

'Follows' Many2one relations and return the final field value.

Examples:
Assuming model is ``product.product``::

direct = [
(follow_m2o_relations('product_tmpl_id.categ_id.name'), 'cat')]

:param field: field "path", using dots for relations as usual in Odoo
"""
def modifier(self, record, to_attr):
attrs = field.split('.')
value = record
for attr in attrs:
value = getattr(value, attr)
return value
return modifier


MappingDefinition = namedtuple('MappingDefinition',
['changed_by',
'only_create'])
Expand Down