Back to index

plone3  3.1.7
Transitions.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Transitions in a web-configurable workflow.
00014 
00015 $Id: Transitions.py 36475 2004-08-24 21:12:59Z yuppie $
00016 """
00017 
00018 from OFS.SimpleItem import SimpleItem
00019 from Globals import DTMLFile
00020 from Globals import PersistentMapping
00021 from Globals import InitializeClass
00022 from Acquisition import aq_inner
00023 from Acquisition import aq_parent
00024 from AccessControl import ClassSecurityInfo
00025 
00026 from ContainerTab import ContainerTab
00027 from Guard import Guard
00028 from permissions import ManagePortal
00029 from utils import _dtmldir
00030 from Expression import Expression
00031 
00032 TRIGGER_AUTOMATIC = 0
00033 TRIGGER_USER_ACTION = 1
00034 
00035 
00036 class TransitionDefinition (SimpleItem):
00037     """Transition definition"""
00038 
00039     meta_type = 'Workflow Transition'
00040 
00041     security = ClassSecurityInfo()
00042     security.declareObjectProtected(ManagePortal)
00043 
00044     title = ''
00045     description = ''
00046     new_state_id = ''
00047     trigger_type = TRIGGER_USER_ACTION
00048     guard = None
00049     actbox_name = ''
00050     actbox_url = ''
00051     actbox_category = 'workflow'
00052     var_exprs = None  # A mapping.
00053     script_name = None  # Executed before transition
00054     after_script_name = None  # Executed after transition
00055 
00056     manage_options = (
00057         {'label': 'Properties', 'action': 'manage_properties'},
00058         {'label': 'Variables', 'action': 'manage_variables'},
00059         )
00060 
00061     def __init__(self, id):
00062         self.id = id
00063 
00064     def getId(self):
00065         return self.id
00066 
00067     def getGuardSummary(self):
00068         res = None
00069         if self.guard is not None:
00070             res = self.guard.getSummary()
00071         return res
00072 
00073     def getGuard(self):
00074         if self.guard is not None:
00075             return self.guard
00076         else:
00077             return Guard().__of__(self)  # Create a temporary guard.
00078 
00079     def getVarExprText(self, id):
00080         if not self.var_exprs:
00081             return ''
00082         else:
00083             expr = self.var_exprs.get(id, None)
00084             if expr is not None:
00085                 return expr.text
00086             else:
00087                 return ''
00088 
00089     def getWorkflow(self):
00090         return aq_parent(aq_inner(aq_parent(aq_inner(self))))
00091 
00092     def getAvailableStateIds(self):
00093         return self.getWorkflow().states.keys()
00094 
00095     def getAvailableScriptIds(self):
00096         return self.getWorkflow().scripts.keys()
00097 
00098     def getAvailableVarIds(self):
00099         return self.getWorkflow().variables.keys()
00100 
00101     _properties_form = DTMLFile('transition_properties', _dtmldir)
00102 
00103     def manage_properties(self, REQUEST, manage_tabs_message=None):
00104         '''
00105         '''
00106         return self._properties_form(REQUEST,
00107                                      management_view='Properties',
00108                                      manage_tabs_message=manage_tabs_message,
00109                                      )
00110 
00111     def setProperties(self, title, new_state_id,
00112                       trigger_type=TRIGGER_USER_ACTION, script_name='',
00113                       after_script_name='',
00114                       actbox_name='', actbox_url='',
00115                       actbox_category='workflow',
00116                       props=None, REQUEST=None, description=''):
00117         '''
00118         '''
00119         self.title = str(title)
00120         self.description = str(description)
00121         self.new_state_id = str(new_state_id)
00122         self.trigger_type = int(trigger_type)
00123         self.script_name = str(script_name)
00124         self.after_script_name = str(after_script_name)
00125         g = Guard()
00126         if g.changeFromProperties(props or REQUEST):
00127             self.guard = g
00128         else:
00129             self.guard = None
00130         self.actbox_name = str(actbox_name)
00131         self.actbox_url = str(actbox_url)
00132         self.actbox_category = str(actbox_category)
00133         if REQUEST is not None:
00134             return self.manage_properties(REQUEST, 'Properties changed.')
00135 
00136     _variables_form = DTMLFile('transition_variables', _dtmldir)
00137 
00138     def manage_variables(self, REQUEST, manage_tabs_message=None):
00139         '''
00140         '''
00141         return self._variables_form(REQUEST,
00142                                      management_view='Variables',
00143                                      manage_tabs_message=manage_tabs_message,
00144                                      )
00145 
00146     def getVariableExprs(self):
00147         ''' get variable exprs for management UI
00148         '''
00149         ve = self.var_exprs
00150         if ve is None:
00151             return []
00152         else:
00153             ret = []
00154             for key in ve.keys():
00155                 ret.append((key,self.getVarExprText(key)))
00156             return ret
00157 
00158     def getWorkflowVariables(self):
00159         ''' get all variables that are available form
00160             workflow and not handled yet.
00161         '''
00162         wf_vars = self.getAvailableVarIds()
00163         if self.var_exprs is None:
00164                 return wf_vars
00165         ret = []
00166         for vid in wf_vars:
00167             if not self.var_exprs.has_key(vid):
00168                 ret.append(vid)
00169         return ret
00170 
00171     def addVariable(self, id, text, REQUEST=None):
00172         '''
00173         Add a variable expression.
00174         '''
00175         if self.var_exprs is None:
00176             self.var_exprs = PersistentMapping()
00177 
00178         expr = None
00179         if text:
00180           expr = Expression(str(text))
00181         self.var_exprs[id] = expr
00182 
00183         if REQUEST is not None:
00184             return self.manage_variables(REQUEST, 'Variable added.')
00185 
00186     def deleteVariables(self,ids=[],REQUEST=None):
00187         ''' delete a WorkflowVariable from State
00188         '''
00189         ve = self.var_exprs
00190         for id in ids:
00191             if ve.has_key(id):
00192                 del ve[id]
00193 
00194         if REQUEST is not None:
00195             return self.manage_variables(REQUEST, 'Variables deleted.')
00196 
00197     def setVariables(self, ids=[], REQUEST=None):
00198         ''' set values for Variables set by this state
00199         '''
00200         if self.var_exprs is None:
00201             self.var_exprs = PersistentMapping()
00202 
00203         ve = self.var_exprs
00204 
00205         if REQUEST is not None:
00206             for id in ve.keys():
00207                 fname = 'varexpr_%s' % id
00208 
00209                 val = REQUEST[fname]
00210                 expr = None
00211                 if val:
00212                     expr = Expression(str(REQUEST[fname]))
00213                 ve[id] = expr
00214 
00215             return self.manage_variables(REQUEST, 'Variables changed.')
00216 
00217 InitializeClass(TransitionDefinition)
00218 
00219 
00220 class Transitions (ContainerTab):
00221     """A container for transition definitions"""
00222 
00223     meta_type = 'Workflow Transitions'
00224 
00225     security = ClassSecurityInfo()
00226     security.declareObjectProtected(ManagePortal)
00227 
00228     all_meta_types = ({'name':TransitionDefinition.meta_type,
00229                        'action':'addTransition',
00230                        },)
00231 
00232     _manage_transitions = DTMLFile('transitions', _dtmldir)
00233 
00234     def manage_main(self, REQUEST, manage_tabs_message=None):
00235         '''
00236         '''
00237         return self._manage_transitions(
00238             REQUEST,
00239             management_view='Transitions',
00240             manage_tabs_message=manage_tabs_message,
00241             )
00242 
00243     def addTransition(self, id, REQUEST=None):
00244         '''
00245         '''
00246         tdef = TransitionDefinition(id)
00247         self._setObject(id, tdef)
00248         if REQUEST is not None:
00249             return self.manage_main(REQUEST, 'Transition added.')
00250 
00251     def deleteTransitions(self, ids, REQUEST=None):
00252         '''
00253         '''
00254         for id in ids:
00255             self._delObject(id)
00256         if REQUEST is not None:
00257             return self.manage_main(REQUEST, 'Transition(s) removed.')
00258 
00259 InitializeClass(Transitions)