1 | ## $Id: workflow.py 15402 2019-05-07 08:07:52Z 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 for students. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from datetime import datetime |
---|
22 | from zope.component import getUtility |
---|
23 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
24 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
25 | from waeup.kofa.interfaces import ( |
---|
26 | IObjectHistory, IKofaWorkflowInfo, IKofaUtils, |
---|
27 | CREATED, ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
28 | REGISTERED, VALIDATED, GRADUATED, TRANSREQ, TRANSVAL, TRANSREL, |
---|
29 | IExtFileStore) |
---|
30 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
31 | from waeup.kofa.workflow import KofaWorkflow, KofaWorkflowInfo |
---|
32 | from waeup.kofa.students.interfaces import IStudent, IStudentsUtils |
---|
33 | from waeup.kofa.utils.helpers import get_current_principal |
---|
34 | |
---|
35 | |
---|
36 | IMPORTABLE_STATES = (ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
37 | REGISTERED, VALIDATED, GRADUATED) |
---|
38 | |
---|
39 | FORBIDDEN_POSTGRAD_STATES = (RETURNING, REGISTERED, VALIDATED) |
---|
40 | |
---|
41 | REGISTRATION_TRANSITIONS = ( |
---|
42 | Transition( |
---|
43 | transition_id = 'create', |
---|
44 | title = _('Create student'), |
---|
45 | source = None, |
---|
46 | condition = NullCondition, |
---|
47 | msg = _('Record created'), |
---|
48 | destination = CREATED), |
---|
49 | |
---|
50 | Transition( |
---|
51 | transition_id = 'admit', |
---|
52 | title = _('Admit student'), |
---|
53 | msg = _('Admitted'), |
---|
54 | source = CREATED, |
---|
55 | destination = ADMITTED), |
---|
56 | |
---|
57 | Transition( |
---|
58 | transition_id = 'reset1', |
---|
59 | title = _('Reset student'), |
---|
60 | msg = _('Reset to initial state'), |
---|
61 | source = ADMITTED, |
---|
62 | destination = CREATED), |
---|
63 | |
---|
64 | Transition( |
---|
65 | transition_id = 'start_clearance', |
---|
66 | title = _('Start clearance'), |
---|
67 | msg = _('Clearance started'), |
---|
68 | source = ADMITTED, |
---|
69 | destination = CLEARANCE), |
---|
70 | |
---|
71 | Transition( |
---|
72 | transition_id = 'reset2', |
---|
73 | title = _('Reset to admitted'), |
---|
74 | msg = _("Reset to 'admitted'"), |
---|
75 | source = CLEARANCE, |
---|
76 | destination = ADMITTED), |
---|
77 | |
---|
78 | Transition( |
---|
79 | transition_id = 'request_clearance', |
---|
80 | title = _('Request clearance'), |
---|
81 | msg = _('Clearance requested'), |
---|
82 | source = CLEARANCE, |
---|
83 | destination = REQUESTED), |
---|
84 | |
---|
85 | Transition( |
---|
86 | transition_id = 'reset3', |
---|
87 | title = _('Reset to clearance started'), |
---|
88 | msg = _("Reset to 'clearance started'"), |
---|
89 | source = REQUESTED, |
---|
90 | destination = CLEARANCE), |
---|
91 | |
---|
92 | Transition( |
---|
93 | transition_id = 'clear', |
---|
94 | title = _('Clear student'), |
---|
95 | msg = _('Cleared'), |
---|
96 | source = REQUESTED, |
---|
97 | destination = CLEARED), |
---|
98 | |
---|
99 | Transition( |
---|
100 | transition_id = 'reset4', |
---|
101 | title = _('Reset to clearance started'), |
---|
102 | msg = _("Reset to 'clearance started'"), |
---|
103 | source = CLEARED, |
---|
104 | destination = CLEARANCE), |
---|
105 | |
---|
106 | Transition( |
---|
107 | transition_id = 'pay_first_school_fee', |
---|
108 | title = _('Pay school fee'), |
---|
109 | msg = _('First school fee payment made'), |
---|
110 | source = CLEARED, |
---|
111 | destination = PAID), |
---|
112 | |
---|
113 | Transition( |
---|
114 | transition_id = 'approve_first_school_fee', |
---|
115 | title = _('Approve payment'), |
---|
116 | msg = _('First school fee payment approved'), |
---|
117 | source = CLEARED, |
---|
118 | destination = PAID), |
---|
119 | |
---|
120 | Transition( |
---|
121 | transition_id = 'reset5', |
---|
122 | title = _('Reset to cleared'), |
---|
123 | msg = _("Reset to 'cleared'"), |
---|
124 | source = PAID, |
---|
125 | destination = CLEARED), |
---|
126 | |
---|
127 | Transition( |
---|
128 | transition_id = 'pay_school_fee', |
---|
129 | title = _('Pay school fee'), |
---|
130 | msg = _('School fee payment made'), |
---|
131 | source = RETURNING, |
---|
132 | destination = PAID), |
---|
133 | |
---|
134 | Transition( |
---|
135 | transition_id = 'pay_pg_fee', |
---|
136 | title = _('Pay PG school fee'), |
---|
137 | msg = _('PG school fee payment made'), |
---|
138 | source = PAID, |
---|
139 | destination = PAID), |
---|
140 | |
---|
141 | Transition( |
---|
142 | transition_id = 'approve_school_fee', |
---|
143 | title = _('Approve school fee payment'), |
---|
144 | msg = _('School fee payment approved'), |
---|
145 | source = RETURNING, |
---|
146 | destination = PAID), |
---|
147 | |
---|
148 | Transition( |
---|
149 | transition_id = 'approve_pg_fee', |
---|
150 | title = _('Approve PG school fee payment'), |
---|
151 | msg = _('PG school fee payment approved'), |
---|
152 | source = PAID, |
---|
153 | destination = PAID), |
---|
154 | |
---|
155 | Transition( |
---|
156 | transition_id = 'reset6', |
---|
157 | title = _('Reset to returning'), |
---|
158 | msg = _("Reset to 'returning'"), |
---|
159 | source = PAID, |
---|
160 | destination = RETURNING), |
---|
161 | |
---|
162 | Transition( |
---|
163 | transition_id = 'register_courses', |
---|
164 | title = _('Register courses'), |
---|
165 | msg = _('Courses registered'), |
---|
166 | source = PAID, |
---|
167 | destination = REGISTERED), |
---|
168 | |
---|
169 | Transition( |
---|
170 | transition_id = 'reset7', |
---|
171 | title = _('Unregister courses'), |
---|
172 | msg = _("Courses unregistered"), |
---|
173 | source = REGISTERED, |
---|
174 | destination = PAID), |
---|
175 | |
---|
176 | Transition( |
---|
177 | transition_id = 'validate_courses', |
---|
178 | title = _('Validate courses'), |
---|
179 | msg = _('Courses validated'), |
---|
180 | source = REGISTERED, |
---|
181 | destination = VALIDATED), |
---|
182 | |
---|
183 | Transition( |
---|
184 | transition_id = 'bypass_validation', |
---|
185 | title = _('Return and bypass validation'), |
---|
186 | msg = _("Course validation bypassed"), |
---|
187 | source = REGISTERED, |
---|
188 | destination = RETURNING), |
---|
189 | |
---|
190 | Transition( |
---|
191 | transition_id = 'reset8', |
---|
192 | title = _('Reset to school fee paid'), |
---|
193 | msg = _("Reset to 'school fee paid'"), |
---|
194 | source = VALIDATED, |
---|
195 | destination = PAID), |
---|
196 | |
---|
197 | Transition( |
---|
198 | transition_id = 'return', |
---|
199 | title = _('Return'), |
---|
200 | msg = _("Returned"), |
---|
201 | source = VALIDATED, |
---|
202 | destination = RETURNING), |
---|
203 | |
---|
204 | Transition( |
---|
205 | transition_id = 'reset9', |
---|
206 | title = _('Reset to courses validated'), |
---|
207 | msg = _("Reset to 'courses validated'"), |
---|
208 | source = RETURNING, |
---|
209 | destination = VALIDATED), |
---|
210 | |
---|
211 | # There is no transition to graduated. |
---|
212 | |
---|
213 | Transition( |
---|
214 | transition_id = 'request_transcript', |
---|
215 | title = _('Request transript'), |
---|
216 | msg = _("Transcript requested"), |
---|
217 | source = GRADUATED, |
---|
218 | destination = TRANSREQ), |
---|
219 | |
---|
220 | Transition( |
---|
221 | transition_id = 'reset10', |
---|
222 | title = _('Reject transcript request'), |
---|
223 | msg = _("Transcript request rejected"), |
---|
224 | source = TRANSREQ, |
---|
225 | destination = GRADUATED), |
---|
226 | |
---|
227 | Transition( |
---|
228 | transition_id = 'validate_transcript', |
---|
229 | title = _('Validate transcript'), |
---|
230 | msg = _("Transcript validated"), |
---|
231 | source = TRANSREQ, |
---|
232 | destination = TRANSVAL), |
---|
233 | |
---|
234 | Transition( |
---|
235 | transition_id = 'release_transcript', |
---|
236 | title = _('Release transcript'), |
---|
237 | msg = _("Transcript released"), |
---|
238 | source = TRANSVAL, |
---|
239 | destination = TRANSREL), |
---|
240 | |
---|
241 | Transition( |
---|
242 | transition_id = 'reset11', |
---|
243 | title = _('Reset to graduated'), |
---|
244 | msg = _("Transcript process reset"), |
---|
245 | source = TRANSREL, |
---|
246 | destination = GRADUATED), |
---|
247 | ) |
---|
248 | |
---|
249 | |
---|
250 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
251 | |
---|
252 | FORBIDDEN_POSTGRAD_TRANS = ['reset6', 'register_courses'] |
---|
253 | |
---|
254 | registration_workflow = KofaWorkflow(REGISTRATION_TRANSITIONS) |
---|
255 | |
---|
256 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
257 | """An adapter to adapt Student objects to workflow states. |
---|
258 | """ |
---|
259 | grok.context(IStudent) |
---|
260 | grok.provides(IWorkflowState) |
---|
261 | |
---|
262 | state_key = 'wf.registration.state' |
---|
263 | state_id = 'wf.registration.id' |
---|
264 | |
---|
265 | class RegistrationWorkflowInfo(KofaWorkflowInfo, grok.Adapter): |
---|
266 | """Adapter to adapt Student objects to workflow info objects. |
---|
267 | """ |
---|
268 | grok.context(IStudent) |
---|
269 | grok.provides(IKofaWorkflowInfo) |
---|
270 | |
---|
271 | def __init__(self, context): |
---|
272 | self.context = context |
---|
273 | self.wf = registration_workflow |
---|
274 | |
---|
275 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
276 | def handle_student_transition_event(obj, event): |
---|
277 | """Append message to student history and log file when transition happened. |
---|
278 | |
---|
279 | Lock and unlock clearance form. |
---|
280 | Trigger actions after school fee payment. |
---|
281 | """ |
---|
282 | |
---|
283 | msg = event.transition.user_data['msg'] |
---|
284 | history = IObjectHistory(obj) |
---|
285 | history.addMessage(msg) |
---|
286 | # School fee payment of returning students triggers the change of |
---|
287 | # current session, current level, and current verdict |
---|
288 | if event.transition.transition_id in ( |
---|
289 | 'pay_school_fee', 'approve_school_fee'): |
---|
290 | getUtility(IStudentsUtils).setReturningData(obj) |
---|
291 | elif event.transition.transition_id in ( |
---|
292 | 'pay_pg_fee', 'approve_pg_fee'): |
---|
293 | new_session = obj['studycourse'].current_session + 1 |
---|
294 | obj['studycourse'].current_session = new_session |
---|
295 | elif event.transition.transition_id == 'validate_courses': |
---|
296 | current_level = obj['studycourse'].current_level |
---|
297 | level_object = obj['studycourse'].get(str(current_level), None) |
---|
298 | if level_object is not None: |
---|
299 | user = get_current_principal() |
---|
300 | if user is None: |
---|
301 | usertitle = 'system' |
---|
302 | else: |
---|
303 | usertitle = getattr(user, 'public_name', None) |
---|
304 | if not usertitle: |
---|
305 | usertitle = user.title |
---|
306 | level_object.validated_by = usertitle |
---|
307 | level_object.validation_date = datetime.utcnow() |
---|
308 | elif event.transition.transition_id == 'clear': |
---|
309 | obj.officer_comment = None |
---|
310 | elif event.transition.transition_id == 'reset8': |
---|
311 | current_level = obj['studycourse'].current_level |
---|
312 | level_object = obj['studycourse'].get(str(current_level), None) |
---|
313 | if level_object is not None: |
---|
314 | level_object.validated_by = None |
---|
315 | level_object.validation_date = None |
---|
316 | elif event.transition.transition_id == 'reset11': |
---|
317 | transcript_file = getUtility(IExtFileStore).getFileByContext( |
---|
318 | obj, attr='final_transcript') |
---|
319 | if transcript_file: |
---|
320 | getUtility(IExtFileStore).deleteFileByContext( |
---|
321 | obj, attr='final_transcript') |
---|
322 | obj['studycourse'].transcript_comment = None |
---|
323 | obj['studycourse'].transcript_signees = None |
---|
324 | # In some tests we don't have a students container |
---|
325 | try: |
---|
326 | students_container = grok.getSite()['students'] |
---|
327 | students_container.logger.info('%s - %s' % (obj.student_id,msg)) |
---|
328 | except (TypeError, AttributeError): |
---|
329 | pass |
---|
330 | return |
---|