From c20a0ea8fceaf96bfbee37dd8b63ebf4390ab3c0 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby <nicolas@nexedi.com> Date: Wed, 1 Dec 2010 14:14:13 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@40959 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5Type/Base.py | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py index 45e3b7be4d..184a70e557 100644 --- a/product/ERP5Type/Base.py +++ b/product/ERP5Type/Base.py @@ -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: -- 2.30.9