1 | """Workflow for students. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from datetime import datetime |
---|
5 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
6 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
7 | from waeup.sirp.students.interfaces import IStudent |
---|
8 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
9 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
10 | from waeup.sirp.utils.helpers import get_current_principal |
---|
11 | |
---|
12 | CREATED = 'created' |
---|
13 | ADMITTED = 'admitted' |
---|
14 | CLEARANCE = 'clearance started' |
---|
15 | REQUESTED = 'clearance requested' |
---|
16 | CLEARED = 'cleared' |
---|
17 | |
---|
18 | REGISTRATION_TRANSITIONS = ( |
---|
19 | Transition( |
---|
20 | transition_id = 'create', |
---|
21 | title = 'Create student', |
---|
22 | source = None, |
---|
23 | condition = NullCondition, |
---|
24 | msg = 'Student record created', |
---|
25 | destination = CREATED), |
---|
26 | |
---|
27 | Transition( |
---|
28 | transition_id = 'admit', |
---|
29 | title = 'Admit student', |
---|
30 | msg = 'Student admitted', |
---|
31 | source = CREATED, |
---|
32 | destination = ADMITTED), |
---|
33 | |
---|
34 | Transition( |
---|
35 | transition_id = 'reset1', |
---|
36 | title = 'Reset student', |
---|
37 | msg = 'Student record reset', |
---|
38 | source = ADMITTED, |
---|
39 | destination = CREATED), |
---|
40 | |
---|
41 | Transition( |
---|
42 | transition_id = 'start_clearance', |
---|
43 | title = 'Start clearance', |
---|
44 | msg = 'Clearance started', |
---|
45 | source = ADMITTED, |
---|
46 | destination = CLEARANCE), |
---|
47 | |
---|
48 | Transition( |
---|
49 | transition_id = 'reset2', |
---|
50 | title = 'Reset to admitted', |
---|
51 | msg = 'Student record reset to admitted', |
---|
52 | source = CLEARANCE, |
---|
53 | destination = ADMITTED), |
---|
54 | |
---|
55 | Transition( |
---|
56 | transition_id = 'request_clearance', |
---|
57 | title = 'Request clearance', |
---|
58 | msg = 'Clearance requested', |
---|
59 | source = CLEARANCE, |
---|
60 | destination = REQUESTED), |
---|
61 | |
---|
62 | Transition( |
---|
63 | transition_id = 'reset3', |
---|
64 | title = 'Reset to clearance', |
---|
65 | msg = 'Student record reset to clearance', |
---|
66 | source = REQUESTED, |
---|
67 | destination = CLEARANCE), |
---|
68 | |
---|
69 | Transition( |
---|
70 | transition_id = 'clear', |
---|
71 | title = 'Clear student', |
---|
72 | msg = 'Cleared', |
---|
73 | source = REQUESTED, |
---|
74 | destination = CLEARED), |
---|
75 | |
---|
76 | Transition( |
---|
77 | transition_id = 'reset4', |
---|
78 | title = 'Reset to clearance', |
---|
79 | msg = 'Student record reset to clearance', |
---|
80 | source = CLEARED, |
---|
81 | destination = CLEARANCE), |
---|
82 | ) |
---|
83 | |
---|
84 | LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance') |
---|
85 | UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance') |
---|
86 | |
---|
87 | registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS) |
---|
88 | |
---|
89 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
90 | """An adapter to adapt Student objects to workflow states. |
---|
91 | """ |
---|
92 | grok.context(IStudent) |
---|
93 | grok.provides(IWorkflowState) |
---|
94 | |
---|
95 | state_key = 'wf.registration.state' |
---|
96 | state_id = 'wf.registration.id' |
---|
97 | |
---|
98 | class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
99 | """Adapter to adapt Student objects to workflow info objects. |
---|
100 | """ |
---|
101 | grok.context(IStudent) |
---|
102 | grok.provides(IWAeUPWorkflowInfo) |
---|
103 | |
---|
104 | def __init__(self, context): |
---|
105 | self.context = context |
---|
106 | self.wf = registration_workflow |
---|
107 | |
---|
108 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
109 | def handle_student_transition_event(obj, event): |
---|
110 | """Append message to student history and log file when transition happened. |
---|
111 | """ |
---|
112 | msg = '%s' % event.transition.user_data['msg'] |
---|
113 | history = IObjectHistory(obj) |
---|
114 | history.addMessage(msg) |
---|
115 | if event.transition.transition_id in LOCK_CLEARANCE_TRANS: |
---|
116 | obj.clearance_locked = True |
---|
117 | if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS: |
---|
118 | obj.clearance_locked = False |
---|
119 | # In some tests we don't have a students container or a user |
---|
120 | try: |
---|
121 | user = get_current_principal() |
---|
122 | students_container = grok.getSite()['students'] |
---|
123 | students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg)) |
---|
124 | except (TypeError, AttributeError): |
---|
125 | pass |
---|
126 | return |
---|