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

Last change on this file since 6316 was 6316, checked in by uli, 14 years ago

Clean up:

  • Cut overlong lines
  • Remove trailing whitespace
  • Pack all imports from one package into one statement
  • Move (unneeded) late import to top of file.
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1"""Workflow for applicants.
2"""
3import grok
4from hurry.workflow.workflow import (
5    Transition, Workflow, WorkflowVersions, WorkflowInfo, WorkflowState,
6    NullCondition)
7from hurry.workflow.interfaces import (
8    IWorkflow, IWorkflowState, IWorkflowInfo, IWorkflowVersions,
9    InvalidTransitionError)
10from waeup.sirp.applicants.interfaces import IApplicantBaseData
11
12
13INITIALIZED = 'initialized'
14STARTED = 'started'
15SUBMITTED = 'submitted'
16ADMITTED = 'admitted'
17NOT_ADMITTED = 'not admitted'
18CREATED = 'created'
19
20def create_workflow():
21    init_transition = Transition(
22        transition_id = 'init',
23        title = 'Initialize application',
24        source = None,
25        condition = NullCondition,
26        destination = INITIALIZED)
27
28    start_transition = Transition(
29        transition_id = 'start',
30        title = 'Start application',
31        source = INITIALIZED,
32        destination = STARTED)
33
34    submit_transition = Transition(
35        transition_id = 'submit',
36        title = 'Submit application',
37        source = STARTED,
38        destination = SUBMITTED)
39
40    admit_transition = Transition(
41        transition_id = 'admit',
42        title = 'Admit applicant',
43        source = SUBMITTED,
44        destination = ADMITTED)
45
46    refuse1_transition = Transition(
47        transition_id = 'refuse1',
48        title = 'Refuse application',
49        source = SUBMITTED,
50        destination = NOT_ADMITTED)
51
52    refuse2_transition = Transition(
53        transition_id = 'refuse2',
54        title = 'Refuse application',
55        source = ADMITTED,
56        destination = NOT_ADMITTED)
57
58    create_transition = Transition(
59        transition_id = 'create',
60        title = 'Create student record',
61        source = ADMITTED,
62        destination = CREATED)
63
64    reset1_transition = Transition(
65        transition_id = 'reset1',
66        title = 'Reset application',
67        source = SUBMITTED,
68        destination = STARTED)
69
70    reset2_transition = Transition(
71        transition_id = 'reset2',
72        title = 'Reset application',
73        source = ADMITTED,
74        destination = STARTED)
75
76    reset3_transition = Transition(
77        transition_id = 'reset3',
78        title = 'Reset application',
79        source = NOT_ADMITTED,
80        destination = STARTED)
81
82    reset4_transition = Transition(
83        transition_id = 'reset4',
84        title = 'Reset application',
85        source = CREATED,
86        destination = STARTED)
87
88    return [init_transition, start_transition, submit_transition,
89            admit_transition, create_transition, refuse1_transition,
90            refuse2_transition, reset1_transition, reset2_transition,
91            reset3_transition, reset4_transition]
92
93
94class ApplicationWorkflow(Workflow):
95    """A hurry.workflow Workflow with more appropriate error messages.
96    """
97    grok.provides(IWorkflow)
98    def __init__(self):
99        super(Workflow, self).__init__()
100        self.refresh(create_workflow())
101
102    def getTransition(self, source, transition_id):
103        transition = self._id_transitions[transition_id]
104        if transition.source != source:
105            raise InvalidTransitionError(
106                "Transition '%s' requires '%s' as source state (is: '%s')" % (
107                    transition_id, transition.source, source))
108        return transition
109
110
111class ApplicationWorkflowNullVersions(WorkflowVersions):
112    """A workflow versions manager that does not handle versions.
113
114    Sounds odd, but the default implementation of
115    :class:`hurry.workflow.workflow.WorkflowVersions` is a base
116    implementation that raises :exc:`NotImplemented` exceptions for
117    most of the methods defined below.
118
119    If we want to register a versionless workflow, an utility
120    implementing IWorkflowVersions is looked up nevertheless by
121    WorkflowInfo and WorkflowState components so we **have** to
122    provide workflow versions, even if we do not support versioned
123    workflows.
124
125    This implementation returns empty result sets for any requests,
126    but does not raise :exc:`NotImplemented`.
127    """
128    def getVersions(self, state, id):
129        return []
130
131    def getVersionsWithAutomaticTransitions(self):
132        return []
133
134    def hasVersion(self, id, state):
135        return False
136
137    def hasVersionId(self, id):
138        return False
139
140# Register global utilities for workflows and workflow versions...
141grok.global_utility(ApplicationWorkflow, IWorkflow)
142grok.global_utility(ApplicationWorkflowNullVersions, IWorkflowVersions)
143
144class ApplicationState(grok.Adapter, WorkflowState):
145    """An adapter to adapt Applicant objects to workflow states.
146    """
147    grok.context(IApplicantBaseData)
148    grok.provides(IWorkflowState)
149
150class ApplicationInfo(grok.Adapter, WorkflowInfo):
151    """Adapter to adapt Applicant objects to workflow info objects.
152    """
153    grok.context(IApplicantBaseData)
154    grok.provides(IWorkflowInfo)
Note: See TracBrowser for help on using the repository browser.