[Erp5-report] r40959 nicolas - /erp5/trunk/products/ERP5Type/Base.py

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Dec 1 15:14:13 CET 2010


Author: nicolas
Date: Wed Dec  1 15:14:13 2010
New Revision: 40959

URL: http://svn.erp5.org?rev=40959&view=rev
Log:
migratePortalType will create new document with expected portal_type and delete the current one (ie. self)

Before returning new one, workflow states will be synchronised (if possible).
Related relations will be keept up to date.




Modified:
    erp5/trunk/products/ERP5Type/Base.py

Modified: erp5/trunk/products/ERP5Type/Base.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Base.py?rev=40959&r1=40958&r2=40959&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Base.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/Base.py [utf8] Wed Dec  1 15:14:13 2010
@@ -3844,6 +3844,62 @@ class Base( CopyContainer,
   def isItem(self):
     return self.portal_type in self.getPortalItemTypeList()
 
+  security.declareProtected(Permissions.DeletePortalContent,
+                            'migratePortalType')
+  def migratePortalType(self, portal_type):
+    """
+    Recreate document by recomputing inputted parameters with help of
+    contribution tool.
+
+    Use an Unrestricted method to edit related relations on other objects.
+    """
+    if self.getPortalType() == portal_type:
+      raise TypeError, 'Can not migrate a document to same portal_type'
+    if not portal_type:
+      raise TypeError, 'Missing portal_type value'
+
+    # Reingestion requested with portal_type.
+    input_kw = {}
+    input_kw['portal_type'] = portal_type
+    for property_id in self.propertyIds():
+      if property_id not in ('portal_type', 'uid', 'id',) \
+            and self.hasProperty(property_id):
+        input_kw[property_id] = self.getProperty(property_id)
+    if getattr(self, 'hasUrlString', None) is not None and self.hasUrlString():
+      # URL is not stored on document
+      # pass required properties for portal_contributions.newContent
+      input_kw['url'] = self.asURL()
+
+    # Use meta transition to jump from one state to another
+    # without existing transitions.
+    from Products.ERP5.InteractionWorkflow import InteractionWorkflowDefinition
+    portal = self.getPortalObject()
+    workflow_tool = portal.portal_workflow
+    worflow_variable_list = []
+    for workflow in workflow_tool.getWorkflowsFor(self):
+      if not isinstance(workflow, InteractionWorkflowDefinition):
+        worflow_variable_list.append(self.getProperty(workflow.state_var))
+
+    # then restart ingestion with new portal_type
+    # XXX Contribution Tool accept only document which are containing
+    # at least the couple data and filename or one url
+    portal_contributions = portal.portal_contributions
+    new_document = portal_contributions.newContent(**input_kw)
+
+    # Meta transitions
+    for state in worflow_variable_list:
+      if workflow_tool._isJumpToStatePossibleFor(new_document, state):
+        workflow_tool._jumpToStateFor(new_document, state)
+
+    # Update relations
+    UnrestrictedMethod(self.updateRelatedContent)(self.getRelativeUrl(),
+                                                  new_document.getRelativeUrl())
+
+    # Delete actual content
+    self.getParentValue()._delObject(self.getId())
+
+    return new_document
+
 InitializeClass(Base)
 
 try:



More information about the Erp5-report mailing list