Commit 72411b25 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_erp5: Add Upgrade Constraint to Migrate Region Categories

  Only edit if the document isMemberOf

  Include exceptional value (cabo verde) since the title is different
  between the removed object and the ones that was kept.
parent 18c940b1
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
<portal_type id="Certificate Authority Tool"> <portal_type id="Certificate Authority Tool">
<item>CertificateAuthorityToolConsistencyConstraint</item> <item>CertificateAuthorityToolConsistencyConstraint</item>
</portal_type> </portal_type>
<portal_type id="Category Tool">
<item>CategoryToolUpgraderRegionConstraint</item>
</portal_type>
<portal_type id="Contract Invitation Token"> <portal_type id="Contract Invitation Token">
<item>Reference</item> <item>Reference</item>
<item>SlapOSCloudContractAccounting</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 category.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:
if category.getTitle() == "Cape Verde":
new_category_list = new_category_map["Cabo Verde"]
else:
new_category_list = new_category_map[category.getTitle()]
if len(new_category_list) != 1:
raise ValueError('Cannot decide which one to migrate into (%s)' % \
[i.getRelativeUrl() for i in new_category_list])
new_category = new_category_list[0]
# XXX It could be relavant to use activities here
category.Category_updateRelatedRegionAndExpire(new_category)
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>
from zExceptions import Unauthorized
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, NegatedQuery
if REQUEST is not None:
raise Unauthorized
assert context.getPortalType() == "Category"
assert context.getIntIndex(0) == 0, "The context (%s) has bad int_index" % context.getRelativeUrl()
assert context.getRelativeUrl().startswith("region/"), \
"The context (%s) is not a region" % context.getRelativeUrl()
if context.getValidationState() == 'expired':
# skip, we seems to be rerunning the migration
# and some reindexation is going on.
return []
related_document_list = context.Base_getRelatedObjectList(**{
'portal_type': NegatedQuery(SimpleQuery(portal_type='Category')),
'category.category_strict_membership': 1})
for document in related_document_list:
# XXX Can we do better them use replace?
if document.isMemberOf(context.getRelativeUrl().replace("portal_categories/", "")):
document.edit(
region=new_category.getRelativeUrl(),
activate_kw={'tag': 'edit_%s' % (context.getRelativeUrl())})
else:
# Trigger reindex since the the member was edited
# use tag to ensure it happens after the edit is reindexed.
document.reindexObject(
activate_kw={'after_tag': 'edit_%s' % (context.getRelativeUrl())})
context.expire()
<?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>new_category, REQUEST=None, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Category_updateRelatedRegionAndExpire</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Catalog Tool | CatalogToolNoneCreationDateConstraint Catalog Tool | CatalogToolNoneCreationDateConstraint
Certificate Authority Tool | CertificateAuthorityToolConsistencyConstraint Certificate Authority Tool | CertificateAuthorityToolConsistencyConstraint
Category Tool | CategoryToolUpgraderRegionConstraint
Contract Invitation Token | Reference Contract Invitation Token | Reference
Contract Invitation Token | SlapOSCloudContractAccounting Contract Invitation Token | SlapOSCloudContractAccounting
Contract Invitation Token | Url Contract Invitation Token | Url
......
CatalogToolNoneCreationDateConstraint CatalogToolNoneCreationDateConstraint
CategoryToolUpgraderRegionConstraint
CertificateAuthorityToolConsistencyConstraint CertificateAuthorityToolConsistencyConstraint
GeographicOrganisation GeographicOrganisation
TemplateToolPASConsistencyConstraint
PreferenceSlapOSConstraintPreference PreferenceSlapOSConstraintPreference
ShacacheSystemPreference ShacacheSystemPreference
SlapOSModuleIdGeneratorConstraint SlapOSModuleIdGeneratorConstraint
TemplateToolBusinessApplicationModuleCategoryConstraint TemplateToolBusinessApplicationModuleCategoryConstraint
TemplateToolPASConsistencyConstraint
WebSiteModuleShacacheConstraint WebSiteModuleShacacheConstraint
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