source: main/waeup.sirp/trunk/src/waeup/sirp/workflow.py @ 6353

Last change on this file since 6353 was 6353, checked in by uli, 13 years ago

Split workflow components (general use stuff goes to w.s.workflow), add some convenience stuff, ...)

File size: 3.8 KB
Line 
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"""
24import grok
25from hurry.workflow.interfaces import IWorkflow, MANUAL, InvalidTransitionError
26from hurry.workflow.workflow import (
27    Transition, Workflow, WorkflowVersions, WorkflowInfo,
28    NullCondition, nullCheckPermission)
29from zope.security.interfaces import NoInteraction
30from zope.security.management import getInteraction
31from waeup.sirp.interfaces import IWAeUPWorkflowInfo
32
33#: A transition that is not a real transition. Use it when you want a
34#: transition that does not change the state of the connected object.
35NULL_TRANSITION = Transition(
36    transition_id = '',
37    title = 'No transition',
38    source = None,
39    condition = NullCondition,
40    msg = '',
41    destination = None,)
42
43class WAeUPWorkflow(Workflow):
44    """A :mod:`hurry.workflow` workflow with more appropriate error
45       messages.
46    """
47    grok.provides(IWorkflow)
48
49    def getTransition(self, source, transition_id):
50        transition = self._id_transitions[transition_id]
51        if transition.source != source:
52            raise InvalidTransitionError(
53                "Transition '%s' requires '%s' as source state (is: '%s')" % (
54                    transition_id, transition.source, source))
55        return transition
56
57
58class WorkflowNullVersions(WorkflowVersions):
59    """A workflow versions manager that does not handle versions.
60
61    Sounds odd, but the default implementation of
62    :class:`hurry.workflow.workflow.WorkflowVersions` is a base
63    implementation that raises :exc:`NotImplemented` exceptions for
64    most of the methods defined below.
65
66    If we want to register a versionless workflow, an utility
67    implementing IWorkflowVersions is looked up nevertheless by
68    WorkflowInfo and WorkflowState components so we **have** to
69    provide workflow versions, even if we do not support versioned
70    workflows.
71
72    This implementation returns empty result sets for any requests,
73    but does not raise :exc:`NotImplemented`.
74    """
75    def getVersions(self, state, id):
76        return []
77
78    def getVersionsWithAutomaticTransitions(self):
79        return []
80
81    def hasVersion(self, id, state):
82        return False
83
84    def hasVersionId(self, id):
85        return False
86
87class WAeUPWorkflowInfo(WorkflowInfo):
88    """A workflow info that provides a convenience transition getter.
89    """
90
91    grok.provides(IWAeUPWorkflowInfo)
92
93    def getManualTransitions(self):
94        """Get allowed manual transitions.
95
96        Get a sorted list of tuples containing the `transition_id` and
97        `title` of each allowed transition.
98        """
99        try:
100            checkPermission = getInteraction().checkPermission
101        except NoInteraction:
102            checkPermission = nullCheckPermission
103        return [(transition.transition_id, transition.title)
104                for transition in sorted(self._getTransitions(MANUAL))
105                if transition.condition(self, self.context) and
106                checkPermission(transition.permission, self.context)]
Note: See TracBrowser for help on using the repository browser.