Back to index

plone3  3.1.7
States.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 """ States in a web-configurable workflow.
00014 
00015 $Id: States.py 77182 2007-06-28 17:25:27Z yuppie $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from AccessControl.requestmethod import postonly
00020 from Acquisition import aq_inner
00021 from Acquisition import aq_parent
00022 from Globals import DTMLFile
00023 from Globals import InitializeClass
00024 from Globals import PersistentMapping
00025 from OFS.SimpleItem import SimpleItem
00026 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00027 
00028 from ContainerTab import ContainerTab
00029 from permissions import ManagePortal
00030 from utils import _dtmldir
00031 
00032 
00033 class StateDefinition(SimpleItem):
00034     """State definition"""
00035 
00036     meta_type = 'Workflow State'
00037 
00038     manage_options = (
00039         {'label': 'Properties', 'action': 'manage_properties'},
00040         {'label': 'Permissions', 'action': 'manage_permissions'},
00041         {'label': 'Groups', 'action': 'manage_groups'},
00042         {'label': 'Variables', 'action': 'manage_variables'},
00043         )
00044 
00045     title = ''
00046     description = ''
00047     transitions = ()  # The ids of possible transitions.
00048     permission_roles = None  # { permission: [role] or (role,) }
00049     group_roles = None  # { group name : (role,) }
00050     var_values = None  # PersistentMapping if set.  Overrides transition exprs.
00051 
00052     security = ClassSecurityInfo()
00053     security.declareObjectProtected(ManagePortal)
00054 
00055     def __init__(self, id):
00056         self.id = id
00057 
00058     def getId(self):
00059         return self.id
00060 
00061     def getWorkflow(self):
00062         return aq_parent(aq_inner(aq_parent(aq_inner(self))))
00063 
00064     def getTransitions(self):
00065         return filter(self.getWorkflow().transitions.has_key,
00066                       self.transitions)
00067 
00068     def getTransitionTitle(self, tid):
00069         t = self.getWorkflow().transitions.get(tid, None)
00070         if t is not None:
00071             return t.title
00072         return ''
00073 
00074     def getAvailableTransitionIds(self):
00075         return self.getWorkflow().transitions.keys()
00076 
00077     def getAvailableVarIds(self):
00078         return self.getWorkflow().variables.keys()
00079 
00080     def getManagedPermissions(self):
00081         return list(self.getWorkflow().permissions)
00082 
00083     def getAvailableRoles(self):
00084         return self.getWorkflow().getAvailableRoles()
00085 
00086     def getPermissionInfo(self, p):
00087         """Returns the list of roles to be assigned to a permission.
00088         """
00089         roles = None
00090         if self.permission_roles:
00091             roles = self.permission_roles.get(p, None)
00092         if roles is None:
00093             return {'acquired':1, 'roles':[]}
00094         else:
00095             if isinstance(roles, tuple):
00096                 acq = 0
00097             else:
00098                 acq = 1
00099             return {'acquired':acq, 'roles':list(roles)}
00100 
00101     def getGroupInfo(self, group):
00102         """Returns the list of roles to be assigned to a group.
00103         """
00104         if self.group_roles:
00105             return self.group_roles.get(group, ())
00106         return ()
00107 
00108     _properties_form = DTMLFile('state_properties', _dtmldir)
00109 
00110     def manage_properties(self, REQUEST, manage_tabs_message=None):
00111         """Show state properties ZMI form."""
00112         return self._properties_form(REQUEST,
00113                                      management_view='Properties',
00114                                      manage_tabs_message=manage_tabs_message,
00115                                      )
00116 
00117     def setProperties(self, title='', transitions=(), REQUEST=None, description=''):
00118         """Set the properties for this State."""
00119         self.title = str(title)
00120         self.description = str(description)
00121         self.transitions = tuple(map(str, transitions))
00122         if REQUEST is not None:
00123             return self.manage_properties(REQUEST, 'Properties changed.')
00124 
00125 
00126     _variables_form = DTMLFile('state_variables', _dtmldir)
00127 
00128     def manage_variables(self, REQUEST, manage_tabs_message=None):
00129         """Show State variables ZMI form."""
00130         return self._variables_form(REQUEST,
00131                                      management_view='Variables',
00132                                      manage_tabs_message=manage_tabs_message,
00133                                      )
00134 
00135     def getVariableValues(self):
00136         """Get VariableValues for management UI."""
00137         vv = self.var_values
00138         if vv is None:
00139             return []
00140         else:
00141             return vv.items()
00142 
00143     def getWorkflowVariables(self):
00144         """Get all variables that are available from the workflow and
00145         not handled yet.
00146         """
00147         wf_vars = self.getAvailableVarIds()
00148         if self.var_values is None:
00149             return wf_vars
00150         ret = []
00151         for vid in wf_vars:
00152             if not self.var_values.has_key(vid):
00153                 ret.append(vid)
00154         return ret
00155 
00156     def addVariable(self,id,value,REQUEST=None):
00157         """Add a WorkflowVariable to State."""
00158         if self.var_values is None:
00159             self.var_values = PersistentMapping()
00160 
00161         self.var_values[id] = value
00162 
00163         if REQUEST is not None:
00164             return self.manage_variables(REQUEST, 'Variable added.')
00165 
00166     def deleteVariables(self,ids=[],REQUEST=None):
00167         """Delete a WorkflowVariable from State."""
00168         vv = self.var_values
00169         for id in ids:
00170             if vv.has_key(id):
00171                 del vv[id]
00172 
00173         if REQUEST is not None:
00174             return self.manage_variables(REQUEST, 'Variables deleted.')
00175 
00176     def setVariables(self, ids=[], REQUEST=None):
00177         """Set values for Variables set by this State."""
00178         if self.var_values is None:
00179             self.var_values = PersistentMapping()
00180 
00181         vv = self.var_values
00182 
00183         if REQUEST is not None:
00184             for id in vv.keys():
00185                 fname = 'varval_%s' % id
00186                 vv[id] = str(REQUEST[fname])
00187             return self.manage_variables(REQUEST, 'Variables changed.')
00188 
00189 
00190 
00191     _permissions_form = DTMLFile('state_permissions', _dtmldir)
00192 
00193     def manage_permissions(self, REQUEST, manage_tabs_message=None):
00194         """Present TTW UI for managing this State's permissions."""
00195         return self._permissions_form(REQUEST,
00196                                      management_view='Permissions',
00197                                      manage_tabs_message=manage_tabs_message,
00198                                      )
00199 
00200     @postonly
00201     def setPermissions(self, REQUEST):
00202         """Set the permissions in REQUEST for this State."""
00203         pr = self.permission_roles
00204         if pr is None:
00205             self.permission_roles = pr = PersistentMapping()
00206         pr.clear()
00207         for p in self.getManagedPermissions():
00208             roles = []
00209             acquired = REQUEST.get('acquire_' + p, 0)
00210             for r in self.getAvailableRoles():
00211                 if REQUEST.get('%s|%s' % (p, r), 0):
00212                     roles.append(r)
00213             roles.sort()
00214             if not acquired:
00215                 roles = tuple(roles)
00216             pr[p] = roles
00217         return self.manage_permissions(REQUEST, 'Permissions changed.')
00218 
00219     @postonly
00220     def setPermission(self, permission, acquired, roles, REQUEST=None):
00221         """Set a permission for this State."""
00222         pr = self.permission_roles
00223         if pr is None:
00224             self.permission_roles = pr = PersistentMapping()
00225         if acquired:
00226             roles = list(roles)
00227         else:
00228             roles = tuple(roles)
00229         pr[permission] = roles
00230 
00231     manage_groups = PageTemplateFile('state_groups.pt', _dtmldir)
00232 
00233     @postonly
00234     def setGroups(self, REQUEST, RESPONSE=None):
00235         """Set the group to role mappings in REQUEST for this State.
00236         """
00237         map = self.group_roles
00238         if map is None:
00239             self.group_roles = map = PersistentMapping()
00240         map.clear()
00241         all_roles = self.getWorkflow().getRoles()
00242         for group in self.getWorkflow().getGroups():
00243             roles = []
00244             for role in all_roles:
00245                 if REQUEST.get('%s|%s' % (group, role), 0):
00246                     roles.append(role)
00247             roles.sort()
00248             roles = tuple(roles)
00249             map[group] = roles
00250         if RESPONSE is not None:
00251             RESPONSE.redirect(
00252                 "%s/manage_groups?manage_tabs_message=Groups+changed."
00253                 % self.absolute_url())
00254 
00255 InitializeClass(StateDefinition)
00256 
00257 
00258 class States(ContainerTab):
00259     """A container for state definitions"""
00260 
00261     meta_type = 'Workflow States'
00262 
00263     security = ClassSecurityInfo()
00264     security.declareObjectProtected(ManagePortal)
00265 
00266     all_meta_types = ({'name':StateDefinition.meta_type,
00267                        'action':'addState',
00268                        },)
00269 
00270     _manage_states = DTMLFile('states', _dtmldir)
00271 
00272     def manage_main(self, REQUEST, manage_tabs_message=None):
00273         '''
00274         '''
00275         return self._manage_states(REQUEST,
00276                                    management_view='States',
00277                                    manage_tabs_message=manage_tabs_message,
00278                                    )
00279 
00280     def addState(self, id, REQUEST=None):
00281         '''
00282         '''
00283         sdef = StateDefinition(id)
00284         self._setObject(id, sdef)
00285         if REQUEST is not None:
00286             return self.manage_main(REQUEST, 'State added.')
00287 
00288     def deleteStates(self, ids, REQUEST=None):
00289         '''
00290         '''
00291         for id in ids:
00292             self._delObject(id)
00293         if REQUEST is not None:
00294             return self.manage_main(REQUEST, 'State(s) removed.')
00295 
00296     def setInitialState(self, id=None, ids=None, REQUEST=None):
00297         '''
00298         '''
00299         if not id:
00300             if len(ids) != 1:
00301                 raise ValueError, 'One and only one state must be selected'
00302             id = ids[0]
00303         id = str(id)
00304         aq_parent(aq_inner(self)).initial_state = id
00305         if REQUEST is not None:
00306             return self.manage_main(REQUEST, 'Initial state selected.')
00307 
00308 InitializeClass(States)