1 | """Workflow for applicants. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry.workflow.workflow import Transition, Workflow, WorkflowVersions |
---|
5 | from hurry.workflow.workflow import WorkflowInfo, WorkflowState, NullCondition |
---|
6 | from hurry.workflow.interfaces import MANUAL, AUTOMATIC, SYSTEM |
---|
7 | from hurry.workflow.interfaces import IWorkflow, IWorkflowState, IWorkflowInfo |
---|
8 | from hurry.workflow.interfaces import IWorkflowVersions |
---|
9 | from waeup.sirp.applicants.interfaces import IApplicantBaseData |
---|
10 | |
---|
11 | INITIALIZED = 'initialized' |
---|
12 | STARTED = 'started' |
---|
13 | SUBMITTED = 'submitted' |
---|
14 | ADMITTED = 'admitted' |
---|
15 | NOT_ADMITTED = 'not admitted' |
---|
16 | CREATED = 'created' |
---|
17 | |
---|
18 | def create_workflow(): |
---|
19 | init_transition = Transition( |
---|
20 | transition_id = 'init', |
---|
21 | title = 'Initialize application', |
---|
22 | source = None, |
---|
23 | condition = NullCondition, |
---|
24 | destination = INITIALIZED) |
---|
25 | |
---|
26 | start_transition = Transition( |
---|
27 | transition_id = 'start', |
---|
28 | title = 'Start application', |
---|
29 | source = INITIALIZED, |
---|
30 | destination = STARTED) |
---|
31 | |
---|
32 | submit_transition = Transition( |
---|
33 | transition_id = 'submit', |
---|
34 | title = 'Submit application', |
---|
35 | source = STARTED, |
---|
36 | destination = SUBMITTED) |
---|
37 | |
---|
38 | admit_transition = Transition( |
---|
39 | transition_id = 'admit', |
---|
40 | title = 'Admit applicant', |
---|
41 | source = SUBMITTED, |
---|
42 | destination = ADMITTED) |
---|
43 | |
---|
44 | refuse1_transition = Transition( |
---|
45 | transition_id = 'refuse1', |
---|
46 | title = 'Refuse application', |
---|
47 | source = SUBMITTED, |
---|
48 | destination = NOT_ADMITTED) |
---|
49 | |
---|
50 | refuse2_transition = Transition( |
---|
51 | transition_id = 'refuse2', |
---|
52 | title = 'Refuse application', |
---|
53 | source = ADMITTED, |
---|
54 | destination = NOT_ADMITTED) |
---|
55 | |
---|
56 | create_transition = Transition( |
---|
57 | transition_id = 'create', |
---|
58 | title = 'Create student record', |
---|
59 | source = ADMITTED, |
---|
60 | destination = CREATED) |
---|
61 | |
---|
62 | reset1_transition = Transition( |
---|
63 | transition_id = 'reset1', |
---|
64 | title = 'Reset application', |
---|
65 | source = SUBMITTED, |
---|
66 | destination = STARTED) |
---|
67 | |
---|
68 | reset2_transition = Transition( |
---|
69 | transition_id = 'reset2', |
---|
70 | title = 'Reset application', |
---|
71 | source = ADMITTED, |
---|
72 | destination = STARTED) |
---|
73 | |
---|
74 | reset3_transition = Transition( |
---|
75 | transition_id = 'reset3', |
---|
76 | title = 'Reset application', |
---|
77 | source = NOT_ADMITTED, |
---|
78 | destination = STARTED) |
---|
79 | |
---|
80 | reset4_transition = Transition( |
---|
81 | transition_id = 'reset4', |
---|
82 | title = 'Reset application', |
---|
83 | source = CREATED, |
---|
84 | destination = STARTED) |
---|
85 | |
---|
86 | return [init_transition, start_transition, submit_transition, admit_transition, |
---|
87 | create_transition, refuse1_transition, refuse2_transition, reset1_transition, |
---|
88 | reset2_transition, reset3_transition, reset4_transition] |
---|
89 | |
---|
90 | |
---|
91 | class ApplicationWorkflow(Workflow): |
---|
92 | """A hurry.workflow Workflow with more appropriate error messages. |
---|
93 | """ |
---|
94 | grok.provides(IWorkflow) |
---|
95 | def __init__(self): |
---|
96 | super(Workflow, self).__init__() |
---|
97 | self.refresh(create_workflow()) |
---|
98 | |
---|
99 | def getTransition(self, source, transition_id): |
---|
100 | from hurry.workflow.interfaces import\ |
---|
101 | InvalidTransitionError, ConditionFailedError |
---|
102 | |
---|
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 | |
---|
111 | class 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... |
---|
141 | grok.global_utility(ApplicationWorkflow, IWorkflow) |
---|
142 | grok.global_utility(ApplicationWorkflowNullVersions, IWorkflowVersions) |
---|
143 | |
---|
144 | class ApplicationState(grok.Adapter, WorkflowState): |
---|
145 | """An adapter to adapt Applicant objects to workflow states. |
---|
146 | """ |
---|
147 | grok.context(IApplicantBaseData) |
---|
148 | grok.provides(IWorkflowState) |
---|
149 | |
---|
150 | class ApplicationInfo(grok.Adapter, WorkflowInfo): |
---|
151 | """Adapter to adapt Applicant objects to workflow info objects. |
---|
152 | """ |
---|
153 | grok.context(IApplicantBaseData) |
---|
154 | grok.provides(IWorkflowInfo) |
---|