vendor/CMF/1.6.1/DCWorkflow

view States.py @ 0:238bab7e7116

CMF 1.6.1 vendor import
author fguillaume
date Tue, 13 Jun 2006 14:57:59 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ States in a web-configurable workflow.
15 $Id: States.py 36910 2005-04-07 16:37:47Z yuppie $
16 """
18 from AccessControl import ClassSecurityInfo
19 from Acquisition import aq_inner
20 from Acquisition import aq_parent
21 from Globals import DTMLFile
22 from Globals import InitializeClass
23 from Globals import PersistentMapping
24 from OFS.SimpleItem import SimpleItem
25 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
27 from ContainerTab import ContainerTab
28 from permissions import ManagePortal
29 from utils import _dtmldir
32 class StateDefinition(SimpleItem):
33 """State definition"""
35 meta_type = 'Workflow State'
37 manage_options = (
38 {'label': 'Properties', 'action': 'manage_properties'},
39 {'label': 'Permissions', 'action': 'manage_permissions'},
40 {'label': 'Groups', 'action': 'manage_groups'},
41 {'label': 'Variables', 'action': 'manage_variables'},
42 )
44 title = ''
45 description = ''
46 transitions = () # The ids of possible transitions.
47 permission_roles = None # { permission: [role] or (role,) }
48 group_roles = None # { group name : (role,) }
49 var_values = None # PersistentMapping if set. Overrides transition exprs.
51 security = ClassSecurityInfo()
52 security.declareObjectProtected(ManagePortal)
54 def __init__(self, id):
55 self.id = id
57 def getId(self):
58 return self.id
60 def getWorkflow(self):
61 return aq_parent(aq_inner(aq_parent(aq_inner(self))))
63 def getTransitions(self):
64 return filter(self.getWorkflow().transitions.has_key,
65 self.transitions)
67 def getTransitionTitle(self, tid):
68 t = self.getWorkflow().transitions.get(tid, None)
69 if t is not None:
70 return t.title
71 return ''
73 def getAvailableTransitionIds(self):
74 return self.getWorkflow().transitions.keys()
76 def getAvailableVarIds(self):
77 return self.getWorkflow().variables.keys()
79 def getManagedPermissions(self):
80 return list(self.getWorkflow().permissions)
82 def getAvailableRoles(self):
83 return self.getWorkflow().getAvailableRoles()
85 def getPermissionInfo(self, p):
86 """Returns the list of roles to be assigned to a permission.
87 """
88 roles = None
89 if self.permission_roles:
90 roles = self.permission_roles.get(p, None)
91 if roles is None:
92 return {'acquired':1, 'roles':[]}
93 else:
94 if isinstance(roles, tuple):
95 acq = 0
96 else:
97 acq = 1
98 return {'acquired':acq, 'roles':list(roles)}
100 def getGroupInfo(self, group):
101 """Returns the list of roles to be assigned to a group.
102 """
103 if self.group_roles:
104 return self.group_roles.get(group, ())
105 return ()
107 _properties_form = DTMLFile('state_properties', _dtmldir)
109 def manage_properties(self, REQUEST, manage_tabs_message=None):
110 """Show state properties ZMI form."""
111 return self._properties_form(REQUEST,
112 management_view='Properties',
113 manage_tabs_message=manage_tabs_message,
114 )
116 def setProperties(self, title='', transitions=(), REQUEST=None, description=''):
117 """Set the properties for this State."""
118 self.title = str(title)
119 self.description = str(description)
120 self.transitions = tuple(map(str, transitions))
121 if REQUEST is not None:
122 return self.manage_properties(REQUEST, 'Properties changed.')
125 _variables_form = DTMLFile('state_variables', _dtmldir)
127 def manage_variables(self, REQUEST, manage_tabs_message=None):
128 """Show State variables ZMI form."""
129 return self._variables_form(REQUEST,
130 management_view='Variables',
131 manage_tabs_message=manage_tabs_message,
132 )
134 def getVariableValues(self):
135 """Get VariableValues for management UI."""
136 vv = self.var_values
137 if vv is None:
138 return []
139 else:
140 return vv.items()
142 def getWorkflowVariables(self):
143 """Get all variables that are available from the workflow and
144 not handled yet.
145 """
146 wf_vars = self.getAvailableVarIds()
147 if self.var_values is None:
148 return wf_vars
149 ret = []
150 for vid in wf_vars:
151 if not self.var_values.has_key(vid):
152 ret.append(vid)
153 return ret
155 def addVariable(self,id,value,REQUEST=None):
156 """Add a WorkflowVariable to State."""
157 if self.var_values is None:
158 self.var_values = PersistentMapping()
160 self.var_values[id] = value
162 if REQUEST is not None:
163 return self.manage_variables(REQUEST, 'Variable added.')
165 def deleteVariables(self,ids=[],REQUEST=None):
166 """Delete a WorkflowVariable from State."""
167 vv = self.var_values
168 for id in ids:
169 if vv.has_key(id):
170 del vv[id]
172 if REQUEST is not None:
173 return self.manage_variables(REQUEST, 'Variables deleted.')
175 def setVariables(self, ids=[], REQUEST=None):
176 """Set values for Variables set by this State."""
177 if self.var_values is None:
178 self.var_values = PersistentMapping()
180 vv = self.var_values
182 if REQUEST is not None:
183 for id in vv.keys():
184 fname = 'varval_%s' % id
185 vv[id] = str(REQUEST[fname])
186 return self.manage_variables(REQUEST, 'Variables changed.')
190 _permissions_form = DTMLFile('state_permissions', _dtmldir)
192 def manage_permissions(self, REQUEST, manage_tabs_message=None):
193 """Present TTW UI for managing this State's permissions."""
194 return self._permissions_form(REQUEST,
195 management_view='Permissions',
196 manage_tabs_message=manage_tabs_message,
197 )
199 def setPermissions(self, REQUEST):
200 """Set the permissions in REQUEST for this State."""
201 pr = self.permission_roles
202 if pr is None:
203 self.permission_roles = pr = PersistentMapping()
204 pr.clear()
205 for p in self.getManagedPermissions():
206 roles = []
207 acquired = REQUEST.get('acquire_' + p, 0)
208 for r in self.getAvailableRoles():
209 if REQUEST.get('%s|%s' % (p, r), 0):
210 roles.append(r)
211 roles.sort()
212 if not acquired:
213 roles = tuple(roles)
214 pr[p] = roles
215 return self.manage_permissions(REQUEST, 'Permissions changed.')
217 def setPermission(self, permission, acquired, roles):
218 """Set a permission for this State."""
219 pr = self.permission_roles
220 if pr is None:
221 self.permission_roles = pr = PersistentMapping()
222 if acquired:
223 roles = list(roles)
224 else:
225 roles = tuple(roles)
226 pr[permission] = roles
228 manage_groups = PageTemplateFile('state_groups.pt', _dtmldir)
230 def setGroups(self, REQUEST, RESPONSE=None):
231 """Set the group to role mappings in REQUEST for this State.
232 """
233 map = self.group_roles
234 if map is None:
235 self.group_roles = map = PersistentMapping()
236 map.clear()
237 all_roles = self.getWorkflow().getRoles()
238 for group in self.getWorkflow().getGroups():
239 roles = []
240 for role in all_roles:
241 if REQUEST.get('%s|%s' % (group, role), 0):
242 roles.append(role)
243 roles.sort()
244 roles = tuple(roles)
245 map[group] = roles
246 if RESPONSE is not None:
247 RESPONSE.redirect(
248 "%s/manage_groups?manage_tabs_message=Groups+changed."
249 % self.absolute_url())
251 InitializeClass(StateDefinition)
254 class States(ContainerTab):
255 """A container for state definitions"""
257 meta_type = 'Workflow States'
259 security = ClassSecurityInfo()
260 security.declareObjectProtected(ManagePortal)
262 all_meta_types = ({'name':StateDefinition.meta_type,
263 'action':'addState',
264 },)
266 _manage_states = DTMLFile('states', _dtmldir)
268 def manage_main(self, REQUEST, manage_tabs_message=None):
269 '''
270 '''
271 return self._manage_states(REQUEST,
272 management_view='States',
273 manage_tabs_message=manage_tabs_message,
274 )
276 def addState(self, id, REQUEST=None):
277 '''
278 '''
279 sdef = StateDefinition(id)
280 self._setObject(id, sdef)
281 if REQUEST is not None:
282 return self.manage_main(REQUEST, 'State added.')
284 def deleteStates(self, ids, REQUEST=None):
285 '''
286 '''
287 for id in ids:
288 self._delObject(id)
289 if REQUEST is not None:
290 return self.manage_main(REQUEST, 'State(s) removed.')
292 def setInitialState(self, id=None, ids=None, REQUEST=None):
293 '''
294 '''
295 if not id:
296 if len(ids) != 1:
297 raise ValueError, 'One and only one state must be selected'
298 id = ids[0]
299 id = str(id)
300 aq_parent(aq_inner(self)).initial_state = id
301 if REQUEST is not None:
302 return self.manage_main(REQUEST, 'Initial state selected.')
304 InitializeClass(States)