Merge lp:~therp-nl/magentoerpconnect/6.1-stable-process_categories_per_storeview into lp:magentoerpconnect/oerp6.1-stable

Proposed by Stefan Rijnhart (Opener)
Status: Needs review
Proposed branch: lp:~therp-nl/magentoerpconnect/6.1-stable-process_categories_per_storeview
Merge into: lp:magentoerpconnect/oerp6.1-stable
Diff against target: 183 lines (+100/-14)
5 files modified
magentoerpconnect/magerp_core.py (+45/-2)
magentoerpconnect/product.py (+47/-12)
magentoerpconnect/product_view.xml (+2/-0)
magentoerpconnect/sale.py (+2/-0)
magentoerpconnect/settings/1.5.0.0/product.category/external.mappinglines.template.csv (+4/-0)
To merge this branch: bzr merge lp:~therp-nl/magentoerpconnect/6.1-stable-process_categories_per_storeview
Reviewer Review Type Date Requested Status
MagentoERPConnect core editors Pending
Review via email: mp+170198@code.launchpad.net

Commit message

[IMP] Import and export categories per store view
    to import translations and to import and export active store views

Description of the change

So far, categories are imported without taking store views into account. This means that if translations are available in another storeview, such translations get lost. This branch fixes that by importing categories for every store view, taking its language setting into account.

Another issue is that the categories' activeness is being registered globally. An import/export round trip resets an alternate setting per store view. This branch fixes that using the same mechanism as for the language issue, and introducing a field on the category to store and manage the active store views. Because an empty value for this field is interpreted as 'active in all store views', this feature is backwards compatible. The 'active' flag keeps its global meaning practically speaking, because if set to False, it will set the category inactive for all store views, regardless of the value of the active store views field.

You may find that the code in this branch is manipulating and preprocessing data in a way that is not in the spirit of the external referential mapping framework, so I'd be curious to know if there is more 'convential' way to the same effect.

To post a comment you must log in.

Unmerged revisions

695. By Stefan Rijnhart (Opener)

[FIX] Line length and dubious spacing

694. By Stefan Rijnhart (Opener)

[IMP] Import and export categories per store view
 to import translations and to import and export active store views

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'magentoerpconnect/magerp_core.py'
2--- magentoerpconnect/magerp_core.py 2013-05-13 11:33:20 +0000
3+++ magentoerpconnect/magerp_core.py 2013-06-18 20:58:27 +0000
4@@ -99,10 +99,53 @@
5
6 @only_for_referential('magento')
7 def import_product_categories(self, cr, uid, ids, context=None):
8+ """
9+ Like in the case of product, get category info per storeview
10+ to get multilingual terminology and active storeviews and in OpenERP.
11+ """
12+
13 if context is None:
14 context = {}
15- context['conn_obj'] = self.external_connection(cr, uid, ids, context=context)
16- self.import_resources(cr, uid, ids, 'product.category', method='search_then_read_no_loop', context=context)
17+
18+ def get_ids(tree):
19+ ids = []
20+ if tree:
21+ ids.append(tree['category_id'])
22+ for child in tree['children']:
23+ ids += get_ids(child)
24+ return ids
25+
26+ for referential in self.browse(cr, uid, ids, context):
27+ category_obj = self.pool.get('product.category')
28+ external_session = ExternalSession(referential, referential)
29+ attr_conn = external_session.connection
30+
31+ tree = attr_conn.call('catalog_category.tree')
32+ ext_cat_ids = [tree['category_id']]
33+ for child in tree['children']:
34+ ext_cat_ids += get_ids(child)
35+
36+ ctx = context.copy()
37+ ctx['conn_obj'] = attr_conn
38+
39+ defaults={'magento_exportable': True}
40+ mapping = {'product.product': category_obj._get_mapping(
41+ cr, uid, referential.id, context=context)}
42+ for cat_id in ext_cat_ids:
43+ is_active = False
44+ for website in referential.shop_group_ids:
45+ for shop in website.shop_ids:
46+ for storeview in shop.storeview_ids:
47+ ctx.update({'lang': storeview.lang_id.code})
48+ category_info = external_session.connection.call(
49+ 'category.info', [cat_id, storeview.code, False])
50+ category_info['storeview_active'] = (
51+ storeview.id, category_info['is_active'] == '1')
52+ # Global value for is_active is true if it is for any storeview
53+ category_info['is_active'] = is_active or category_info['is_active'] == '1'
54+ category_obj._record_one_external_resource(
55+ cr, uid, external_session, category_info,
56+ defaults=defaults, mapping=mapping, context=ctx)
57 return True
58
59
60
61=== modified file 'magentoerpconnect/product.py'
62--- magentoerpconnect/product.py 2013-06-06 18:09:24 +0000
63+++ magentoerpconnect/product.py 2013-06-18 20:58:27 +0000
64@@ -136,11 +136,27 @@
65 main_lang = context['main_lang']
66 for resource_id, resource in resources.items():
67 #Move this part of code in a python lib
68- parent_id = resource[main_lang]['parent_id']
69- del resource[main_lang]['parent_id']
70- ext_id = external_session.connection.call('catalog_category.create', [parent_id, resource[main_lang]])
71- for storeview, lang in storeview_to_lang.items():
72- external_session.connection.call('catalog_category.update', [ext_id, resource[lang], storeview])
73+ storeviews = list(context['storeviews'])
74+ storeview_id, storeview, lang = storeviews.pop(0)
75+ is_active = resource[lang]['is_active']
76+ category_storeview_ids = resource[lang]['magento_storeview_ids']
77+ del resource[lang]['magento_storeview_ids']
78+ if (is_active and category_storeview_ids and
79+ storeview_id not in category_storeview_ids):
80+ resource[lang]['is_active'] = 0
81+ parent_id = resource[lang]['parent_id']
82+ del resource[lang]['parent_id']
83+ ext_id = external_session.connection.call(
84+ 'catalog_category.create', [parent_id, resource[lang], storeview])
85+ for storeview_id, storeview, lang in storeviews:
86+ del resource[lang]['magento_storeview_ids']
87+ if (is_active and category_storeview_ids and
88+ storeview_id not in category_storeview_ids):
89+ resource[lang]['is_active'] = 0
90+ else:
91+ resource[lang]['is_active'] = is_active
92+ external_session.connection.call(
93+ 'catalog_category.update', [ext_id, resource[lang], storeview])
94 ext_create_ids[resource_id] = ext_id
95 return ext_create_ids
96
97@@ -151,14 +167,26 @@
98 main_lang = context['main_lang']
99 for resource_id, resource in resources.items():
100 #Move this part of code in a python lib
101- ext_id = resource[main_lang]['ext_id']
102- del resource[main_lang]['ext_id']
103- parent_id = resource[main_lang]['parent_id']
104- del resource[main_lang]['parent_id']
105- external_session.connection.call('catalog_category.update', [ext_id, resource[main_lang], False])
106+ storeviews = list(context['storeviews'])
107+ storeview_id, storeview, lang = storeviews.pop(0)
108+ category_storeview_ids = resource[lang]['magento_storeview_ids']
109+ del resource[lang]['magento_storeview_ids']
110+ is_active = resource[lang]['is_active']
111+ if (is_active and category_storeview_ids and
112+ storeview_id not in category_storeview_ids):
113+ resource[lang]['is_active'] = 0
114+ ext_id = resource[lang]['ext_id']
115+ parent_id = resource[lang]['parent_id']
116+ del resource[lang]['parent_id']
117+ external_session.connection.call('catalog_category.update', [ext_id, resource[lang], storeview])
118 external_session.connection.call('oerp_catalog_category.move', [ext_id, parent_id])
119- for storeview, lang in storeview_to_lang.items():
120- del resource[lang]['ext_id']
121+ for storeview_id, storeview, lang in storeviews:
122+ del resource[lang]['magento_storeview_ids']
123+ if (is_active and category_storeview_ids and
124+ storeview_id not in category_storeview_ids):
125+ resource[lang]['is_active'] = 0
126+ else:
127+ resource[lang]['is_active'] = is_active
128 external_session.connection.call('catalog_category.update', [ext_id, resource[lang], storeview])
129 ext_update_ids[resource_id] = ext_id
130 return ext_update_ids
131@@ -193,6 +221,13 @@
132 'magerp_stamp':fields.datetime('Magento stamp'),
133 'include_in_menu': fields.boolean('Include in Navigation Menu'),
134 'page_layout': fields.many2one('magerp.product_category_attribute_options', 'Page Layout', domain="[('attribute_name', '=', 'page_layout')]"),
135+ 'magento_storeview_ids': fields.many2many(
136+ 'magerp.storeviews',
137+ 'magerp_category_storeview_rel',
138+ 'category_id', 'storeview_id',
139+ 'Magento Storeviews',
140+ help=('By default categories will be exported to every storeview. '
141+ 'If you want to export it only on some storeviews you can select them here')),
142 }
143
144 _defaults = {
145
146=== modified file 'magentoerpconnect/product_view.xml'
147--- magentoerpconnect/product_view.xml 2012-12-07 13:50:28 +0000
148+++ magentoerpconnect/product_view.xml 2013-06-18 20:58:27 +0000
149@@ -23,6 +23,8 @@
150 <field name="url_key" colspan="2" />
151 <field name="level" colspan="2" />
152 <field name="include_in_menu" colspan="2" />
153+ <separator string="Magento storeviews (leave empty for all)" colspan="2" />
154+ <field name="magento_storeview_ids" nolabel="1" colspan="2" />
155 </group>
156 <group col="1" colspan="1">
157 <separator string="Category Image" />
158
159=== modified file 'magentoerpconnect/sale.py'
160--- magentoerpconnect/sale.py 2013-05-13 11:36:53 +0000
161+++ magentoerpconnect/sale.py 2013-06-18 20:58:27 +0000
162@@ -63,7 +63,9 @@
163 context['main_lang'] = shop.referential_id.default_lang_id.code
164 context['lang_to_export'] = [shop.referential_id.default_lang_id.code]
165 context['storeview_to_lang'] = {}
166+ context['storeviews'] = []
167 for storeview in shop.storeview_ids:
168+ context['storeviews'].append((storeview.id, storeview.code, storeview.lang_id.code))
169 if storeview.lang_id and storeview.lang_id.code != shop.referential_id.default_lang_id.code:
170 context['storeview_to_lang'][storeview.code] = storeview.lang_id.code
171 if not storeview.lang_id.code in context['lang_to_export']:
172
173=== modified file 'magentoerpconnect/settings/1.5.0.0/product.category/external.mappinglines.template.csv'
174--- magentoerpconnect/settings/1.5.0.0/product.category/external.mappinglines.template.csv 2013-06-06 18:18:28 +0000
175+++ magentoerpconnect/settings/1.5.0.0/product.category/external.mappinglines.template.csv 2013-06-18 20:58:27 +0000
176@@ -69,3 +69,7 @@
177 result = [('image',resource['image_name'])]
178 else:
179 result=[]"
180+magentoerpconnect.mag1500_product_category_magento_storeview_ids,magentoerpconnect.mag1500_product_category,magento_storeview_ids,magentoerpconnect.field_product_category_magento_storeview_ids,in,function,list,,,"if resource.get('storeview_active'):
181+ storeview_id = resource['storeview_active'][0]
182+ command = resource['storeview_active'][1] and 4 or 3
183+ result = [('magento_storeview_ids', [(command, storeview_id)])]",