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