forked from OCA/connector-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_category.py
271 lines (223 loc) · 9.85 KB
/
product_category.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2013 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
import xmlrpclib
from openerp import models, fields
from openerp.addons.connector.unit.mapper import (mapping,
ImportMapper
)
from openerp.addons.connector.exception import (IDMissingInBackend,
MappingError,
)
from .unit.backend_adapter import (GenericAdapter,
MAGENTO_DATETIME_FORMAT,
)
from .unit.import_synchronizer import (DelayedBatchImporter,
MagentoImporter,
TranslationImporter,
AddCheckpoint,
)
from .backend import magento
_logger = logging.getLogger(__name__)
class MagentoProductCategory(models.Model):
_name = 'magento.product.category'
_inherit = 'magento.binding'
_inherits = {'product.category': 'openerp_id'}
_description = 'Magento Product Category'
openerp_id = fields.Many2one(comodel_name='product.category',
string='Product Category',
required=True,
ondelete='cascade')
description = fields.Text(translate=True)
magento_parent_id = fields.Many2one(
comodel_name='magento.product.category',
string='Magento Parent Category',
ondelete='cascade',
)
magento_child_ids = fields.One2many(
comodel_name='magento.product.category',
inverse_name='magento_parent_id',
string='Magento Child Categories',
)
class ProductCategory(models.Model):
_inherit = 'product.category'
magento_bind_ids = fields.One2many(
comodel_name='magento.product.category',
inverse_name='openerp_id',
string="Magento Bindings",
)
@magento
class ProductCategoryAdapter(GenericAdapter):
_model_name = 'magento.product.category'
_magento_model = 'catalog_category'
_admin_path = '/{model}/index/'
def _call(self, method, arguments):
try:
return super(ProductCategoryAdapter, self)._call(method, arguments)
except xmlrpclib.Fault as err:
# 101 is the error in the Magento API
# when the category does not exist
if err.faultCode == 102:
raise IDMissingInBackend
else:
raise
def search(self, filters=None, from_date=None, to_date=None):
""" Search records according to some criteria and return a
list of ids
:rtype: list
"""
if filters is None:
filters = {}
dt_fmt = MAGENTO_DATETIME_FORMAT
if from_date is not None:
filters.setdefault('updated_at', {})
# updated_at include the created records
filters['updated_at']['from'] = from_date.strftime(dt_fmt)
if to_date is not None:
filters.setdefault('updated_at', {})
filters['updated_at']['to'] = to_date.strftime(dt_fmt)
return self._call('oerp_catalog_category.search',
[filters] if filters else [{}])
def read(self, id, storeview_id=None, attributes=None):
""" Returns the information of a record
:rtype: dict
"""
return self._call('%s.info' % self._magento_model,
[int(id), storeview_id, attributes])
def tree(self, parent_id=None, storeview_id=None):
""" Returns a tree of product categories
:rtype: dict
"""
def filter_ids(tree):
children = {}
if tree['children']:
for node in tree['children']:
children.update(filter_ids(node))
category_id = {tree['category_id']: children}
return category_id
if parent_id:
parent_id = int(parent_id)
tree = self._call('%s.tree' % self._magento_model,
[parent_id, storeview_id])
return filter_ids(tree)
def move(self, categ_id, parent_id, after_categ_id=None):
return self._call('%s.move' % self._magento_model,
[categ_id, parent_id, after_categ_id])
def get_assigned_product(self, categ_id):
return self._call('%s.assignedProducts' % self._magento_model,
[categ_id])
def assign_product(self, categ_id, product_id, position=0):
return self._call('%s.assignProduct' % self._magento_model,
[categ_id, product_id, position, 'id'])
def update_product(self, categ_id, product_id, position=0):
return self._call('%s.updateProduct' % self._magento_model,
[categ_id, product_id, position, 'id'])
def remove_product(self, categ_id, product_id):
return self._call('%s.removeProduct' % self._magento_model,
[categ_id, product_id, 'id'])
@magento
class ProductCategoryBatchImporter(DelayedBatchImporter):
""" Import the Magento Product Categories.
For every product category in the list, a delayed job is created.
A priority is set on the jobs according to their level to rise the
chance to have the top level categories imported first.
"""
_model_name = ['magento.product.category']
def _import_record(self, magento_id, priority=None):
""" Delay a job for the import """
super(ProductCategoryBatchImporter, self)._import_record(
magento_id, priority=priority)
def run(self, filters=None):
""" Run the synchronization """
from_date = filters.pop('from_date', None)
to_date = filters.pop('to_date', None)
if from_date or to_date:
updated_ids = self.backend_adapter.search(filters,
from_date=from_date,
to_date=to_date)
else:
updated_ids = None
base_priority = 10
def import_nodes(tree, level=0):
for node_id, children in tree.iteritems():
# By changing the priority, the top level category has
# more chance to be imported before the childrens.
# However, importers have to ensure that their parent is
# there and import it if it doesn't exist
if updated_ids is None or node_id in updated_ids:
self._import_record(node_id, priority=base_priority+level)
import_nodes(children, level=level+1)
tree = self.backend_adapter.tree()
import_nodes(tree)
ProductCategoryBatchImport = ProductCategoryBatchImporter # deprecated
@magento
class ProductCategoryImporter(MagentoImporter):
_model_name = ['magento.product.category']
def _import_dependencies(self):
""" Import the dependencies for the record"""
record = self.magento_record
# import parent category
# the root category has a 0 parent_id
if record.get('parent_id'):
parent_id = record['parent_id']
if self.binder.to_openerp(parent_id) is None:
importer = self.unit_for(MagentoImporter)
importer.run(parent_id)
def _create(self, data):
openerp_binding = super(ProductCategoryImporter, self)._create(data)
checkpoint = self.unit_for(AddCheckpoint)
checkpoint.run(openerp_binding.id)
return openerp_binding
def _after_import(self, binding):
""" Hook called at the end of the import """
translation_importer = self.unit_for(TranslationImporter)
translation_importer.run(self.magento_id, binding.id)
ProductCategoryImport = ProductCategoryImporter # deprecated
@magento
class ProductCategoryImportMapper(ImportMapper):
_model_name = 'magento.product.category'
direct = [
('description', 'description'),
]
@mapping
def name(self, record):
if record['level'] == '0': # top level category; has no name
return {'name': self.backend_record.name}
if record['name']: # may be empty in storeviews
return {'name': record['name']}
@mapping
def magento_id(self, record):
return {'magento_id': record['category_id']}
@mapping
def backend_id(self, record):
return {'backend_id': self.backend_record.id}
@mapping
def parent_id(self, record):
if not record.get('parent_id'):
return
binder = self.binder_for()
category_id = binder.to_openerp(record['parent_id'], unwrap=True)
mag_cat_id = binder.to_openerp(record['parent_id'])
if category_id is None:
raise MappingError("The product category with "
"magento id %s is not imported." %
record['parent_id'])
return {'parent_id': category_id, 'magento_parent_id': mag_cat_id}