1 | ## |
---|
2 | ## workflow.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sat Jun 11 19:52:12 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """Workflow components of general use. |
---|
23 | """ |
---|
24 | import grok |
---|
25 | from hurry.workflow.interfaces import IWorkflow, MANUAL, InvalidTransitionError |
---|
26 | from hurry.workflow.workflow import ( |
---|
27 | Transition, Workflow, WorkflowVersions, WorkflowInfo, |
---|
28 | NullCondition, nullCheckPermission) |
---|
29 | from zope.security.interfaces import NoInteraction |
---|
30 | from zope.security.management import getInteraction |
---|
31 | from waeup.sirp.interfaces import IWAeUPWorkflowInfo |
---|
32 | |
---|
33 | class WAeUPWorkflow(Workflow): |
---|
34 | """A :mod:`hurry.workflow` workflow with more appropriate error |
---|
35 | messages. |
---|
36 | """ |
---|
37 | grok.provides(IWorkflow) |
---|
38 | |
---|
39 | def getTransition(self, source, transition_id): |
---|
40 | transition = self._id_transitions[transition_id] |
---|
41 | if transition.source != source: |
---|
42 | raise InvalidTransitionError( |
---|
43 | "Transition '%s' requires '%s' as source state (is: '%s')" % ( |
---|
44 | transition_id, transition.source, source)) |
---|
45 | return transition |
---|
46 | |
---|
47 | |
---|
48 | class WorkflowNullVersions(WorkflowVersions): |
---|
49 | """A workflow versions manager that does not handle versions. |
---|
50 | |
---|
51 | Sounds odd, but the default implementation of |
---|
52 | :class:`hurry.workflow.workflow.WorkflowVersions` is a base |
---|
53 | implementation that raises :exc:`NotImplemented` exceptions for |
---|
54 | most of the methods defined below. |
---|
55 | |
---|
56 | If we want to register a versionless workflow, an utility |
---|
57 | implementing IWorkflowVersions is looked up nevertheless by |
---|
58 | WorkflowInfo and WorkflowState components so we **have** to |
---|
59 | provide workflow versions, even if we do not support versioned |
---|
60 | workflows. |
---|
61 | |
---|
62 | This implementation returns empty result sets for any requests, |
---|
63 | but does not raise :exc:`NotImplemented`. |
---|
64 | """ |
---|
65 | def getVersions(self, state, id): |
---|
66 | return [] |
---|
67 | |
---|
68 | def getVersionsWithAutomaticTransitions(self): |
---|
69 | return [] |
---|
70 | |
---|
71 | def hasVersion(self, id, state): |
---|
72 | return False |
---|
73 | |
---|
74 | def hasVersionId(self, id): |
---|
75 | return False |
---|
76 | |
---|
77 | class WAeUPWorkflowInfo(WorkflowInfo): |
---|
78 | """A workflow info that provides a convenience transition getter. |
---|
79 | """ |
---|
80 | |
---|
81 | grok.provides(IWAeUPWorkflowInfo) |
---|
82 | |
---|
83 | def getManualTransitions(self): |
---|
84 | """Get allowed manual transitions. |
---|
85 | |
---|
86 | Get a sorted list of tuples containing the `transition_id` and |
---|
87 | `title` of each allowed transition. |
---|
88 | """ |
---|
89 | try: |
---|
90 | checkPermission = getInteraction().checkPermission |
---|
91 | except NoInteraction: |
---|
92 | checkPermission = nullCheckPermission |
---|
93 | return [(transition.transition_id, transition.title) |
---|
94 | for transition in sorted(self._getTransitions(MANUAL)) |
---|
95 | if transition.condition(self, self.context) and |
---|
96 | checkPermission(transition.permission, self.context)] |
---|