Merge lp:~camptocamp/openerp-connector-magento/7.0-new-mapper-api-gbr into lp:~openerp-connector-core-editors/openerp-connector-magento/7.0

Proposed by Guewen Baconnier @ Camptocamp on 2013-11-09
Status: Merged
Approved by: Guewen Baconnier @ Camptocamp on 2014-01-14
Approved revision: 929
Merged at revision: 941
Proposed branch: lp:~camptocamp/openerp-connector-magento/7.0-new-mapper-api-gbr
Merge into: lp:~openerp-connector-core-editors/openerp-connector-magento/7.0
Diff against target: 235 lines (+50/-41)
4 files modified
magentoerpconnect/partner.py (+2/-2)
magentoerpconnect/sale.py (+14/-14)
magentoerpconnect/unit/export_synchronizer.py (+15/-9)
magentoerpconnect/unit/import_synchronizer.py (+19/-16)
To merge this branch: bzr merge lp:~camptocamp/openerp-connector-magento/7.0-new-mapper-api-gbr
Reviewer Review Type Date Requested Status
Joël Grand-Guillaume @ camptocamp code review, no tests 2013-11-09 Approve on 2013-12-12
Review via email: mp+194630@code.launchpad.net
To post a comment you must log in.

Branch is now sufficiently stable to be tested and used for development.

Hi,

Thanks for the contrib, as far as I can see, LGTM.

Regards,

Joël

review: Approve (code review, no tests)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'magentoerpconnect/partner.py'
2--- magentoerpconnect/partner.py 2013-08-05 09:42:06 +0000
3+++ magentoerpconnect/partner.py 2013-11-09 22:36:08 +0000
4@@ -451,10 +451,10 @@
5 # and use the name of the company for the name
6 company_mapper = get_unit(CompanyImportMapper,
7 'magento.res.partner')
8- company_mapper.convert(magento_record)
9+ map_record = company_mapper.map_record(magento_record)
10 self.session.write('magento.res.partner',
11 partner_binding_id,
12- company_mapper.data)
13+ map_record.values())
14 else:
15 # for B2C individual customers, merge with the main
16 # partner
17
18=== modified file 'magentoerpconnect/sale.py'
19--- magentoerpconnect/sale.py 2013-11-08 14:16:29 +0000
20+++ magentoerpconnect/sale.py 2013-11-09 22:36:08 +0000
21@@ -563,11 +563,11 @@
22 'website_id': record.get('website_id'),
23 }
24 mapper = self.get_connector_unit_for_model(PartnerImportMapper,
25- 'magento.res.partner')
26- mapper.convert(customer_record)
27- oe_record = mapper.data_for_create
28- oe_record['guest_customer'] = True
29- partner_bind_id = sess.create('magento.res.partner', oe_record)
30+ 'magento.res.partner')
31+ map_record = mapper.map_record(customer_record)
32+ map_record.update(guest_customer=True)
33+ partner_bind_id = sess.create('magento.res.partner',
34+ map_record.values(for_create=True))
35 partner_binder.bind(guest_customer_id,
36 partner_bind_id)
37 else:
38@@ -608,10 +608,10 @@
39 'magento.address')
40
41 def create_address(address_record):
42- addr_mapper.convert(address_record)
43- oe_address = addr_mapper.data_for_create
44- oe_address.update(addresses_defaults)
45- address_bind_id = sess.create('magento.address', oe_address)
46+ map_record = addr_mapper.map_record(address_record)
47+ map_record.update(addresses_defaults)
48+ address_bind_id = sess.create('magento.address',
49+ map_record.values(for_create=True))
50 return sess.read('magento.address',
51 address_bind_id,
52 ['openerp_id'])['openerp_id'][0]
53@@ -671,14 +671,14 @@
54 children = [('items', 'magento_order_line_ids', 'magento.sale.order.line'),
55 ]
56
57- def _after_mapping(self, result):
58+ def finalize(self, map_record, values):
59 sess = self.session
60 # TODO: refactor: do no longer store the transient fields in the
61 # result, use a ConnectorUnit to create the lines
62 result = sess.pool['sale.order']._convert_special_fields(sess.cr,
63 sess.uid,
64- result,
65- result['magento_order_line_ids'],
66+ values,
67+ values['magento_order_line_ids'],
68 sess.context)
69 # remove transient fields otherwise OpenERP will raise a warning
70 # or even fail to create the record because the fields do not
71@@ -689,7 +689,7 @@
72 result.pop('gift_certificates_amount', None)
73 result.pop('gift_certificates_code', None)
74 onchange = self.get_connector_unit_for_model(SaleOrderOnChange)
75- return onchange.play(result, result['magento_order_line_ids'])
76+ return onchange.play(values, values['magento_order_line_ids'])
77
78 @mapping
79 def name(self, record):
80@@ -754,7 +754,7 @@
81 ifield = record.get('shipping_method')
82 if not ifield:
83 return
84-
85+
86 carrier_ids = session.search('delivery.carrier',
87 [('magento_code', '=', ifield)])
88 if carrier_ids:
89
90=== modified file 'magentoerpconnect/unit/export_synchronizer.py'
91--- magentoerpconnect/unit/export_synchronizer.py 2013-05-01 12:12:58 +0000
92+++ magentoerpconnect/unit/export_synchronizer.py 2013-11-09 22:36:08 +0000
93@@ -139,9 +139,9 @@
94 """ Export the dependencies for the record"""
95 return
96
97- def _map_data(self, fields=None):
98+ def _map_data(self):
99 """ Convert the external record to OpenERP """
100- self.mapper.convert(self.binding_record, fields=fields)
101+ return self.mapper.map_record(self.binding_record)
102
103 def _validate_data(self, data):
104 """ Check if the values to import are correct
105@@ -153,13 +153,23 @@
106 """
107 return
108
109+ def _create_data(self, map_record, fields=None):
110+ return map_record.values(for_create=True, fields=fields)
111+
112 def _create(self, data):
113 """ Create the Magento record """
114+ # special check on data before export
115+ self._validate_data(data)
116 return self.backend_adapter.create(data)
117
118+ def _update_data(self, map_record, fields=None):
119+ return map_record.values(fields=fields)
120+
121 def _update(self, data):
122 """ Update an Magento record """
123 assert self.magento_id
124+ # special check on data before export
125+ self._validate_data(data)
126 self.backend_adapter.write(self.magento_id, data)
127
128 def _run(self, fields=None):
129@@ -176,21 +186,17 @@
130 # export the missing linked resources
131 self._export_dependencies()
132
133- self._map_data(fields=fields)
134+ map_record = self._map_data()
135
136 if self.magento_id:
137- record = self.mapper.data
138+ record = self._update_data(map_record, fields=fields)
139 if not record:
140 return _('Nothing to export.')
141- # special check on data before export
142- self._validate_data(record)
143 self._update(record)
144 else:
145- record = self.mapper.data_for_create
146+ record = self._create_data(map_record, fields=fields)
147 if not record:
148 return _('Nothing to export.')
149- # special check on data before export
150- self._validate_data(record)
151 self.magento_id = self._create(record)
152 return _('Record exported with ID %s on Magento.') % self.magento_id
153
154
155=== modified file 'magentoerpconnect/unit/import_synchronizer.py'
156--- magentoerpconnect/unit/import_synchronizer.py 2013-10-31 08:53:35 +0000
157+++ magentoerpconnect/unit/import_synchronizer.py 2013-11-09 22:36:08 +0000
158@@ -92,9 +92,11 @@
159 return
160
161 def _map_data(self):
162- """ Call the convert on the Mapper so the converted record can
163- be obtained using mapper.data or mapper.data_for_create"""
164- self.mapper.convert(self.magento_record)
165+ """ Returns an instance of
166+ :py:class:`~openerp.addons.connector.unit.mapper.MapRecord`
167+
168+ """
169+ return self.mapper.map_record(self.magento_record)
170
171 def _validate_data(self, data):
172 """ Check if the values to import are correct
173@@ -124,21 +126,26 @@
174 """Return the binding id from the magento id"""
175 return self.binder.to_openerp(self.magento_id)
176
177- def _context(self):
178- context = self.session.context.copy()
179- context['connector_no_export'] = True
180- return context
181+ def _create_data(self, map_record):
182+ return map_record.values(for_create=True)
183
184 def _create(self, data):
185 """ Create the OpenERP record """
186+ # special check on data before import
187+ self._validate_data(data)
188 with self.session.change_context({'connector_no_export': True}):
189 binding_id = self.session.create(self.model._name, data)
190 _logger.debug('%s %d created from magento %s',
191 self.model._name, binding_id, self.magento_id)
192 return binding_id
193
194+ def _update_data(self, map_record):
195+ return map_record.values()
196+
197 def _update(self, binding_id, data):
198 """ Update an OpenERP record """
199+ # special check on data before import
200+ self._validate_data(data)
201 with self.session.change_context({'connector_no_export': True}):
202 self.session.write(self.model._name, binding_id, data)
203 _logger.debug('%s %d updated from magento %s',
204@@ -173,17 +180,13 @@
205 # import the missing linked resources
206 self._import_dependencies()
207
208- self._map_data()
209+ map_record = self._map_data()
210
211 if binding_id:
212- record = self.mapper.data
213- # special check on data before import
214- self._validate_data(record)
215+ record = self._update_data(map_record)
216 self._update(binding_id, record)
217 else:
218- record = self.mapper.data_for_create
219- # special check on data before import
220- self._validate_data(record)
221+ record = self._create_data(map_record)
222 binding_id = self._create(record)
223
224 self.binder.bind(self.magento_id, binding_id)
225@@ -282,8 +285,8 @@
226
227 for storeview in lang_storeviews:
228 lang_record = self._get_magento_data(storeview.magento_id)
229- self.mapper.convert(lang_record)
230- record = self.mapper.data
231+ map_record = self.mapper.map_record(lang_record)
232+ record = map_record.values()
233
234 data = dict((field, value) for field, value in record.iteritems()
235 if field in translatable_fields)