Back to index

plone3  3.1.7
workflow_policies.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## CMFPlacefulWorflow
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 Contributed by Jazkarta
00020 """
00021 __version__ = "$Revision:  $"
00022 # $Source:  $
00023 # $Id:  $
00024 __docformat__ = 'restructuredtext'
00025 from Products.GenericSetup.utils import exportObjects, importObjects
00026 from Products.GenericSetup.utils import XMLAdapterBase
00027 from Products.GenericSetup.OFSP.exportimport import FolderXMLAdapter
00028 
00029 from Products.CMFCore.utils import getToolByName
00030 from Products.CMFCore.exportimport.workflow import WorkflowToolXMLAdapter
00031 
00032 from Products.CMFPlacefulWorkflow.DefaultWorkflowPolicy import DEFAULT_CHAIN
00033 from Products.CMFPlacefulWorkflow.global_symbols import Log, LOG_DEBUG
00034 
00035 _marker = []
00036 
00037 class PlacefulWorkflowXMLAdapter(FolderXMLAdapter):
00038 
00039     _LOGGER_ID = 'placeful_workflow'
00040 
00041     body = property(XMLAdapterBase._exportBody,
00042                     XMLAdapterBase._importBody)
00043 
00044 class WorkflowPoliciesXMLAdapter(WorkflowToolXMLAdapter):
00045 
00046     _LOGGER_ID = 'placeful_workflow'
00047 
00048     @property
00049     def name(self):
00050         Log(LOG_DEBUG, self.context.id)
00051         return self.context.id
00052 
00053     def _extractChains(self):
00054         fragment = self._doc.createDocumentFragment()
00055         node = self._doc.createElement('bindings')
00056         child = self._doc.createElement('default')
00057         for workflow_id in self.context._default_chain or ():
00058             sub = self._doc.createElement('bound-workflow')
00059             sub.setAttribute('workflow_id', workflow_id)
00060             child.appendChild(sub)
00061         node.appendChild(child)
00062         if self.context._chains_by_type:
00063             typestool = getToolByName(self.context, 'portal_types')
00064             typeinfos = sorted(typestool.listTypeInfo(),
00065                                key=lambda type: type.getId())
00066             for ti in typeinfos:
00067                 type_id = ti.getId()
00068                 chain = self.context._chains_by_type.get(type_id, _marker)
00069                 child = self._doc.createElement('type')
00070                 if chain is _marker:
00071                     # If no chain is defined chain is acquired
00072                     continue
00073 
00074                 if chain == (DEFAULT_CHAIN,):
00075                     # If the type is using the default chain there's no chain
00076                     # to wait after the attribute
00077                     child.setAttribute('type_id', type_id)
00078                     child.setAttribute('default_chain', "true")
00079                     node.appendChild(child)
00080                     continue
00081 
00082                 child.setAttribute('type_id', type_id)
00083                 for workflow_id in chain:
00084                     sub = self._doc.createElement('bound-workflow')
00085                     sub.setAttribute('workflow_id', workflow_id)
00086                     child.appendChild(sub)
00087                 node.appendChild(child)
00088         fragment.appendChild(node)
00089         return fragment
00090 
00091     def _initChains(self, node):
00092         """ Import policies from XML
00093 
00094         Types specified are in two cases:
00095 
00096         - a default_chain attribute is present
00097 
00098         - zero or more workflows are presents then type take the chain in the
00099           same order
00100 
00101         For any types not specified, we do nothing and they will acquire their
00102         chain from another policy or from portal_workfow.
00103         """
00104         seen = set()
00105         for child in node.childNodes:
00106             if child.nodeName != 'bindings':
00107                 continue
00108             for sub in child.childNodes:
00109                 if sub.nodeName == 'default':
00110                     self.context.setDefaultChain(self._getChain(sub))
00111                 if sub.nodeName == 'type':
00112                     type_id = str(sub.getAttribute('type_id'))
00113                     assert type_id not in seen, (
00114                         'Type %s listed more than once' % type_id)
00115                     seen.add(type_id)
00116 
00117                     default = sub.getAttribute('default_chain')
00118                     chain = self._getChain(sub)
00119                     Log(LOG_DEBUG, default, chain)
00120                     assert not (default and chain), (
00121                         'Type %s is marked to use default but also '
00122                         'included a chain: %s' % (type_id, chain))
00123                     if default:
00124                         # omit from the policy to acquire
00125                         self.context.setChain(type_id, (DEFAULT_CHAIN,))
00126                     else:
00127                         self.context.setChain(type_id, chain)
00128 
00129 
00130     def _getChain(self, node):
00131         result = super(WorkflowPoliciesXMLAdapter,
00132                        self)._getChain(node)
00133         if result == '':
00134             return []
00135         return result.split(',')
00136 
00137 def importWorkflowPolicies(context):
00138     """Import workflow policies from the XML file.
00139     """
00140     site = context.getSite()
00141     tool = getToolByName(site, 'portal_placeful_workflow', None)
00142     if tool is not None:
00143         importObjects(tool, '', context)
00144 
00145 
00146 def exportWorkflowPolicies(context):
00147     """Export workflow policies as an XML file.
00148     """
00149     site = context.getSite()
00150     tool = getToolByName(site, 'portal_placeful_workflow', None)
00151     if tool is None:
00152         logger = context.getLogger('workflow_policies')
00153         logger.info('Nothing to export.')
00154         return
00155 
00156     exportObjects(tool, '', context)