Commit a6e65f17 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_erp5: Add Upgrade Constraint to Migrate Region Categories

parent 670974e0
......@@ -5,6 +5,9 @@
<portal_type id="Certificate Authority Tool">
<item>CertificateAuthorityToolConsistencyConstraint</item>
</portal_type>
<portal_type id="Category Tool">
<item>CategoryToolUpgraderRegionConstraint</item>
</portal_type>
<portal_type id="Contract Invitation Token">
<item>Reference</item>
<item>SlapOSCloudContractAccounting</item>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Property Sheet" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_count</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>_mt_index</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
<item>
<key> <string>_tree</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Migrate and unify the constraints related to Region changes\n
</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>CategoryToolUpgraderRegionConstraint</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Property Sheet</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Length" module="BTrees.Length"/>
</pickle>
<pickle> <int>0</int> </pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="OOBTree" module="BTrees.OOBTree"/>
</pickle>
<pickle>
<none/>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="OOBTree" module="BTrees.OOBTree"/>
</pickle>
<pickle>
<none/>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Script Constraint" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_identity_criterion</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>_range_criterion</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>constraint_type/post_upgrade</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string encoding="base64">Cg==</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>region_migration_upgrade_constraint</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Script Constraint</string> </value>
</item>
<item>
<key> <string>script_id</string> </key>
<value> <string>CategoryTool_checkRegionMigrationConsistency</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from zExceptions import Unauthorized
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, NegatedQuery
if REQUEST is not None:
raise Unauthorized
# check if is consistent
portal = context.getPortalObject()
not_expired_category_list = portal.portal_catalog(
portal_type="Category",
path="%portal_categories/region/%",
validation_state=NegatedQuery(SimpleQuery(validation_state='expired'))
)
# Filter the list, since search for None dont work properly.
category_to_migrate_list = [c for c in not_expired_category_list if c.getIntIndex(0) < 1]
if not len(category_to_migrate_list):
# Skip everything is consistent
return []
if fixit:
# Build a Map to not search inside the loop, this makes upgrade faster
new_category_map = {}
for category in not_expired_category_list:
if c.getIntIndex(0) < 1 or category.getValidationState() == 'expired':
continue
title = category.getTitle()
if title not in new_category_map:
new_category_map[title] = [category]
else:
new_category_map[title].append(category)
message_list = []
for category in category_to_migrate_list:
if category.getIntIndex() == 1:
raise ValueError("Selected the wrong thing")
if category.getValidationState() == 'expired':
# skip, we seems to be rerunning the migration
# and some reindexation is going on.
continue
message_list.append(
"%s requires migration (int_index: %s, validation_state: %s)" % (
category.getRelativeUrl(), category.getIntIndex(), category.getValidationState())
)
if fixit:
new_category_list = new_category_map[category.getTitle()]
if len(new_category_list) != 1:
raise ValueError('Cannot decide which one to migrate into (%s options)' % len(new_category_list))
new_category = new_category_list[0]
# Can we make it cheaper
related_document_list = category.Base_getRelatedObjectList(**{
'portal_type': NegatedQuery(SimpleQuery(portal_type='Category')),
'category.category_strict_membership': 1})
for document in related_document_list:
# does it actually has the category?
document.edit(region=new_category)
document.reindexObject()
# 1) Search for the all related objects
# 2) Migrate all related objects
# 3) reindex everything
# 4) Expire the category
category.expire()
message_list.append(
"%s migrated and expired (int_index: %s, validation_state: %s)" % (
category.getRelativeUrl(), category.getIntIndex(), category.getValidationState())
)
return message_list
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>fixit=False, REQUEST=None, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>CategoryTool_checkRegionMigrationConsistency</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Catalog Tool | CatalogToolNoneCreationDateConstraint
Certificate Authority Tool | CertificateAuthorityToolConsistencyConstraint
Category Tool | CategoryToolUpgraderRegionConstraint
Contract Invitation Token | Reference
Contract Invitation Token | SlapOSCloudContractAccounting
Contract Invitation Token | Url
......
CatalogToolNoneCreationDateConstraint
CategoryToolUpgraderRegionConstraint
CertificateAuthorityToolConsistencyConstraint
GeographicOrganisation
TemplateToolPASConsistencyConstraint
PreferenceSlapOSConstraintPreference
ShacacheSystemPreference
SlapOSModuleIdGeneratorConstraint
TemplateToolBusinessApplicationModuleCategoryConstraint
TemplateToolPASConsistencyConstraint
WebSiteModuleShacacheConstraint
\ No newline at end of file
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