Back to index

plone3  3.1.7
PlacefulWorkflowTool.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## CMFPlacefulWorkflow
00003 ## Copyright (C)2005 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 PlacefulWorkflowTool main class
00020 """
00021 __version__ = "$Revision: 62441 $"
00022 # $Source: /cvsroot/ingeniweb/CMFPlacefulWorkflow/PlacefulWorkflowTool.py,v $
00023 # $Id: PlacefulWorkflowTool.py 62441 2008-04-10 20:03:21Z encolpe $
00024 __docformat__ = 'restructuredtext'
00025 
00026 from os.path import join as path_join
00027 
00028 from Acquisition import aq_base
00029 from AccessControl.requestmethod import postonly
00030 from OFS.Folder import Folder
00031 from OFS.ObjectManager import IFAwareObjectManager
00032 from AccessControl import ClassSecurityInfo
00033 from Globals import InitializeClass, package_home
00034 
00035 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00036 
00037 from zope.interface import implements
00038 
00039 from Products.CMFCore.utils import getToolByName, UniqueObject
00040 from Products.CMFCore.permissions import ManagePortal
00041 from Products.CMFPlone.migrations.migration_util import safeEditProperty
00042 from Products.CMFCore.utils import registerToolInterface
00043 
00044 
00045 from interfaces import IPlacefulWorkflowTool
00046 from interfaces.PlacefulWorkflow import IPlacefulWorkflowTool as z2IPlacefulWorkflowTool
00047 
00048 
00049 WorkflowPolicyConfig_id = ".wf_policy_config"
00050 
00051 def addPlacefulWorkflowTool(self,REQUEST={}):
00052     """
00053     Factory method for the Placeful Workflow Tool
00054     """
00055     id='portal_placeful_workflow'
00056     pwt=PlacefulWorkflowTool()
00057     self._setObject(id, pwt, set_owner=0)
00058     getattr(self, id)._post_init()
00059     if REQUEST:
00060         return REQUEST.RESPONSE.redirect(self.absolute_url() + '/manage_main')
00061 
00062 class PlacefulWorkflowTool(UniqueObject, Folder, IFAwareObjectManager):
00063     """
00064     PlacefulWorkflow Tool
00065     """
00066 
00067     id = 'portal_placeful_workflow'
00068     meta_type = 'Placeful Workflow Tool'
00069 
00070     __implements__ = (z2IPlacefulWorkflowTool,)
00071     implements(IPlacefulWorkflowTool)
00072 
00073     _actions = []
00074 
00075     security = ClassSecurityInfo()
00076 
00077 
00078     manage_options=Folder.manage_options
00079 
00080     def __init__(self):
00081         # Properties to be edited by site manager
00082         safeEditProperty(self, 'max_chain_length', 1, data_type='int')
00083 
00084     _manage_addWorkflowPolicyForm = PageTemplateFile(path_join('www', 'add_workflow_policy'), globals())
00085 
00086     security.declareProtected( ManagePortal, 'manage_addWorkflowPolicyForm')
00087     def manage_addWorkflowPolicyForm(self, REQUEST):
00088 
00089         """ Form for adding workflow policies.
00090         """
00091         wfpt = []
00092         for key in _workflow_policy_factories.keys():
00093             wfpt.append(key)
00094         wfpt.sort()
00095         return self._manage_addWorkflowPolicyForm(REQUEST, workflow_policy_types=wfpt)
00096 
00097     security.declareProtected( ManagePortal, 'manage_addWorkflowPolicy')
00098     def manage_addWorkflowPolicy(self, id,
00099                                  workflow_policy_type='default_workflow_policy (Simple Policy)',
00100                                  duplicate_id='empty',
00101                                  RESPONSE=None,
00102                                  REQUEST=None):
00103         """ Adds a workflow policies from the registered types.
00104         """
00105         if id in ('empty', 'portal_workflow'):
00106             raise ValueError, "'%s' is reserved. Please choose another id." % id
00107 
00108         factory = _workflow_policy_factories[workflow_policy_type]
00109         ob = factory(id)
00110         self._setObject(id, ob)
00111 
00112         if duplicate_id and duplicate_id != 'empty':
00113             types_tool = getToolByName(self, 'portal_types')
00114             new_wp = self.getWorkflowPolicyById(id)
00115 
00116             if duplicate_id == 'portal_workflow':
00117                 wf_tool = getToolByName(self, 'portal_workflow')
00118 
00119                 new_wp.setDefaultChain(wf_tool._default_chain)
00120 
00121                 for ptype in types_tool.objectIds():
00122                     chain = wf_tool.getChainForPortalType(ptype, managescreen=True)
00123                     if chain:
00124                         new_wp.setChain(ptype, chain)
00125 
00126             else:
00127                 orig_wp = self.getWorkflowPolicyById(duplicate_id)
00128                 new_wp.setDefaultChain(orig_wp.getDefaultChain('Document'))
00129 
00130                 for ptype in types_tool.objectIds():
00131                     chain = orig_wp.getChainFor(ptype, managescreen=True)
00132                     if chain:
00133                         new_wp.setChain(ptype, chain)
00134 
00135         if RESPONSE is not None:
00136             RESPONSE.redirect(self.absolute_url() +
00137                               '/manage_main?management_view=Contents')
00138     manage_addWorkflowPolicy = postonly(manage_addWorkflowPolicy)
00139 
00140     def all_meta_types(self):
00141         return (
00142             {'name': 'WorkflowPolicy',
00143              'action': 'manage_addWorkflowPolicyForm',
00144              'permission': ManagePortal },)
00145 
00146     security.declareProtected( ManagePortal, 'getWorkflowPolicyById')
00147     def getWorkflowPolicyById(self, wfp_id):
00148 
00149         """ Retrieve a given workflow policy.
00150         """
00151         policy=None
00152         if wfp_id != None:
00153             wfp = getattr(self, wfp_id, None)
00154             if wfp !=None:
00155                 if getattr(wfp, '_isAWorkflowPolicy', 0):
00156                     policy = wfp
00157         return policy
00158 
00159     security.declareProtected( ManagePortal, 'getWorkflowPolicyIds')
00160     def getWorkflowPolicies(self):
00161         """ Return the list of workflow policies.
00162         """
00163         wfps = []
00164         for obj_name, obj in self.objectItems():
00165             if getattr(obj, '_isAWorkflowPolicy', 0):
00166                 wfps.append(obj)
00167         return tuple(wfps)
00168 
00169     security.declareProtected( ManagePortal, 'getWorkflowPolicyIds')
00170     def getWorkflowPolicyIds(self):
00171 
00172         """ Return the list of workflow policy ids.
00173         """
00174         wfp_ids = []
00175 
00176         for obj_name, obj in self.objectItems():
00177             if getattr(obj, '_isAWorkflowPolicy', 0):
00178                 wfp_ids.append(obj_name)
00179 
00180         return tuple(wfp_ids)
00181 
00182 
00183     security.declareProtected( ManagePortal, 'getWorkflowPolicyConfig')
00184     def getWorkflowPolicyConfig(self, ob):
00185         local_config = None
00186         some_config = getattr(ob, WorkflowPolicyConfig_id, None)
00187         if some_config is not None:
00188             # Was it here or did we acquire?
00189             if hasattr(aq_base(ob), WorkflowPolicyConfig_id):
00190                 local_config = some_config
00191         return local_config
00192 
00193     def _post_init(self):
00194         """
00195         _post_init(self) => called from manage_add method, acquired within ZODB (__init__ is not)
00196         """
00197         pass
00198 
00199     #
00200     #   portal_workflow_policy implementation.
00201     #
00202 
00203     def getMaxChainLength(self):
00204         """Return the max workflow chain length"""
00205         max_chain_length = self.getProperty('max_chain_length')
00206         return max_chain_length
00207 
00208     def setMaxChainLength(self, max_chain_length):
00209         """Set the max workflow chain length"""
00210         safeEditProperty(self, 'max_chain_length', max_chain_length, data_type='int')
00211 
00212 _workflow_policy_factories = {}
00213 
00214 def _makeWorkflowPolicyFactoryKey(factory, id=None, title=None):
00215     # The factory should take one argument, id.
00216     if id is None:
00217         id = getattr(factory, 'id', '') or getattr(factory, 'meta_type', '')
00218     if title is None:
00219         title = getattr(factory, 'title', '')
00220     key = id
00221     if title:
00222         key = key + ' (%s)' % title
00223     return key
00224 
00225 def addWorkflowPolicyFactory(factory, id=None, title=None):
00226     key = _makeWorkflowPolicyFactoryKey( factory, id, title )
00227     _workflow_policy_factories[key] = factory
00228 
00229 def _removeWorkflowPolicyFactory( factory, id=None, title=None ):
00230     """ Make teardown in unitcase cleaner. """
00231     key = _makeWorkflowPolicyFactoryKey( factory, id, title )
00232     try:
00233         del _workflow_policy_factories[key]
00234     except KeyError:
00235         pass
00236 
00237 InitializeClass(PlacefulWorkflowTool)
00238 registerToolInterface('portal_placeful_workflow', IPlacefulWorkflowTool)