Commit 5ce72b86 authored by Ayush Tiwari's avatar Ayush Tiwari

erp5_catalog: Migration script to move catalog to new ERP5Catalog object.

What we expect during migration:
1. Copy objects as well as properties from SQLCatalog to ERP5Catalog
2. Migration of SQL Methods and Python Scripts as ERP5 objects
3. Update migration with filter_dict for methods

The migration function would now create portal_catalog based on
'Catalog Tool' portal_type and after that it'll migrate the objects
(catalog, SQL Method, Python Scripts) in it.

Also, update BusinessTemplate fucntions according to changes made after migration
parent 6902c508
......@@ -152,12 +152,12 @@ SEPARATELY_EXPORTED_PROPERTY_DICT = {
def _getCatalog(acquisition_context):
"""
Return the id of the SQLCatalog which correspond to the current BT.
Return the id of the Catalog which correspond to the current BT.
"""
catalog_method_id_list = acquisition_context.getTemplateCatalogMethodIdList()
if len(catalog_method_id_list) == 0:
try:
return acquisition_context.getPortalObject().portal_catalog.objectIds('SQLCatalog')[0]
return acquisition_context.getPortalObject().portal_catalog.objectIds()[0]
except IndexError:
return None
catalog_method_id = catalog_method_id_list[0]
......@@ -1461,7 +1461,7 @@ class ObjectTemplateItem(BaseTemplateItem):
# an object which cannot (e.g. External Method).
LOG('BusinessTemplate', WARNING,
'could not restore %r in %r' % (subobject_id, obj))
if obj.meta_type in ('Z SQL Method',):
if obj.meta_type in ('Z SQL Method', 'ERP5 SQL Method'):
fixZSQLMethod(portal, obj)
# portal transforms specific initialization
elif obj.meta_type in ('Transform', 'TransformsChain'):
......@@ -1953,7 +1953,7 @@ class SkinTemplateItem(ObjectTemplateItem):
if not force and update_dict.get(relative_url) == 'nothing':
continue
folder = self.unrestrictedResolveValue(p, relative_url)
for obj in folder.objectValues(spec=('Z SQL Method',)):
for obj in folder.objectValues(spec=('Z SQL Method', 'ERP5 SQL Method')):
fixZSQLMethod(p, obj)
if folder.aq_parent.meta_type == 'CMF Skins Tool':
registerSkinFolder(skin_tool, folder)
......@@ -2893,14 +2893,26 @@ class CatalogMethodTemplateItem(ObjectTemplateItem):
"""Extracts properties for a given method in the catalog.
Returns a mapping of property name -> boolean """
method_properties = PersistentMapping()
for prop in catalog._properties:
property_list = list(catalog._properties)
if catalog.meta_type == 'ERP5 Catalog':
property_list = list(catalog.propertyMap())
for prop in property_list:
if prop.get('select_variable') == 'getCatalogMethodIds':
# In case the properties are defined via property sheet 'Catalog', the
# object would have two IDs if it is of type 'selection' or
# 'multiple_selection': 'id' and 'base_id', usage of base_id is preferred
# while building objects as it maintains consistency between the old
# catalog and new erp5 catalog
if prop.has_key('base_id'):
id_key = 'base_id'
else:
id_key = 'id'
if prop['type'] == 'selection' and \
getattr(catalog, prop['id']) == method_id:
method_properties[prop['id']] = 1
getattr(catalog, prop[id_key]) == method_id:
method_properties[prop[id_key]] = 1
elif prop['type'] == 'multiple selection' and \
method_id in getattr(catalog, prop['id']):
method_properties[prop['id']] = 1
method_id in getattr(catalog, prop[id_key]):
method_properties[prop[id_key]] = 1
return method_properties
def build(self, context, **kw):
......@@ -2919,7 +2931,10 @@ class CatalogMethodTemplateItem(ObjectTemplateItem):
method_id = obj.id
self._method_properties[method_id] = self._extractMethodProperties(
catalog, method_id)
filter = catalog.filter_dict.get(method_id, {})
filter_dict = catalog.filter_dict
if catalog.meta_type == 'ERP5 Catalog':
filter_dict = catalog.getFilterDict()
filter = filter_dict.get(method_id, {})
self._is_filtered_archive[method_id] = filter.get('filtered', 0)
for method in catalog_method_filter_list:
property = method[8:-8]
......@@ -2978,6 +2993,26 @@ class CatalogMethodTemplateItem(ObjectTemplateItem):
force = kw.get('force')
values = []
# When the default catalog is of 'ERP5 Catalog' meta_type, its better to ..
# convert all the CatalogMethodTemplateItems in the current BT to the
# allowed types for ERP5 Catalog, i.e, to ERP5 SQLMethod and ERP5 Python Script
# and update the self._objects dict accordingly
if catalog.meta_type == 'ERP5 Catalog':
portal = self.getPortalObject()
# Will be modifying dict, so better to use .items()
# XXX: In python3 it should be .copy.items().
for path, obj in self._objects.items():
method = self.unrestrictedResolveValue(portal, path)
if method.meta_type == 'Z SQL Method':
self.convertSQLMethodToERP5SQLMethod(method)
if method.meta_type == 'Script (Python)':
self.convertPythonScriptToERP5PythonScript(method)
# Check what is better option to get the method object here,
# path resolving or getting object from erp5 catalog
method = self.unrestrictedResolveValue(portal, path)
new_obj = method.aq_base
self._objects[path] = new_obj
if force: # get all objects
values = self._objects.values()
else: # get only selected object
......@@ -3005,6 +3040,26 @@ class CatalogMethodTemplateItem(ObjectTemplateItem):
setattr(catalog, key, tuple(new_value))
# Restore filter
if catalog.meta_type == 'ERP5 Catalog':
method = catalog._getOb(method_id)
if self._is_filtered_archive.get(method_id, 0):
expression = self._filter_expression_archive[method_id]
if expression and expression.strip():
# only compile non-empty expressions
expr_instance = Expression(expression)
else:
expr_instance = None
method.setFiltered(1)
method.setExpression(expression)
method.setExpressionInstance(expr_instance)
method.setTypeList( \
self._filter_type_archive.get(method_id, ()))
method.setExpressionCacheKeyList( \
self._filter_expression_cache_key_archive.get(method_id, ()))
elif method_id in catalog.getFilterDict().keys():
method.setFiltered(0)
elif catalog.meta_type == 'SQLCatalog':
if self._is_filtered_archive.get(method_id, 0):
expression = self._filter_expression_archive[method_id]
if expression and expression.strip():
......@@ -3077,15 +3132,29 @@ class CatalogMethodTemplateItem(ObjectTemplateItem):
values.append(value)
for obj in values:
method_id = obj.id
property_list = list(catalog._properties)
if catalog.meta_type == 'ERP5 Catalog':
property_list = list(catalog.propertyMap())
# remove method references in portal_catalog
for catalog_prop in catalog._properties:
for catalog_prop in property_list:
if catalog_prop.get('select_variable') == 'getCatalogMethodIds'\
and catalog_prop['type'] == 'multiple selection':
old_value = getattr(catalog, catalog_prop['id'], ())
# In case the properties are defined via property sheet 'Catalog', the
# object would have two IDs if it is of type 'selection' or
# 'multiple_selection': 'id' and 'base_id', usage of base_id is preferred
# while building objects as it maintains consistency between the old
# catalog and new erp5 catalog
if catalog_prop.has_key('base_id'):
id_key = 'base_id'
else:
id_key = 'id'
old_value = getattr(catalog, catalog_prop[id_key], ())
if method_id in old_value:
new_value = list(old_value)
new_value.remove(method_id)
setattr(catalog, catalog_prop['id'], new_value)
# Better to set the attribute value as tuple as it would be consistent
# with both SQL Catalog and ERP5 Catalog.
setattr(catalog, catalog_prop[id_key], tuple(new_value))
if catalog.filter_dict.has_key(method_id):
del catalog.filter_dict[method_id]
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment