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

Last change on this file since 7580 was 7535, checked in by Henrik Bettermann, 13 years ago

Beautify history strings.

  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1"""Workflow for students.
2"""
3import grok
4from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
5from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
6from waeup.sirp.interfaces import (
7    IObjectHistory, ISIRPWorkflowInfo,
8    CREATED, ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING,
9    REGISTERED, VALIDATED)
10from waeup.sirp.workflow import SIRPWorkflow, SIRPWorkflowInfo
11from waeup.sirp.utils.helpers import get_current_principal
12from waeup.sirp.students.interfaces import IStudent
13from waeup.sirp.students.utils import set_returning_data
14
15IMPORTABLE_STATES = (ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING,
16    REGISTERED, VALIDATED)
17
18REGISTRATION_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    Transition(
84        transition_id = 'pay_first_school_fee',
85        title = 'Pay school fee',
86        msg = 'School fee paid',
87        source = CLEARED,
88        destination = PAID),
89
90    Transition(
91        transition_id = 'reset5',
92        title = 'Reset to cleared',
93        msg = "Student record reset to 'cleared'",
94        source = PAID,
95        destination = CLEARED),
96
97    Transition(
98        transition_id = 'pay_school_fee',
99        title = 'Pay school fee',
100        msg = 'School fee paid',
101        source = RETURNING,
102        destination = PAID),
103
104    Transition(
105        transition_id = 'reset6',
106        title = 'Reset to returning',
107        msg = "Student record reset to 'returning'",
108        source = PAID,
109        destination = RETURNING),
110
111    Transition(
112        transition_id = 'register_courses',
113        title = 'Register courses',
114        msg = 'Courses registered',
115        source = PAID,
116        destination = REGISTERED),
117
118    Transition(
119        transition_id = 'reset7',
120        title = 'Reset to paid',
121        msg = "Student record reset to 'paid'",
122        source = REGISTERED,
123        destination = PAID),
124
125    Transition(
126        transition_id = 'validate_courses',
127        title = 'Validate courses',
128        msg = 'Courses validated',
129        source = REGISTERED,
130        destination = VALIDATED),
131
132    Transition(
133        transition_id = 'reset8',
134        title = 'Reset to paid',
135        msg = "Student record reset to 'paid'",
136        source = VALIDATED,
137        destination = PAID),
138
139    Transition(
140        transition_id = 'return',
141        title = 'Return',
142        msg = 'Returned',
143        source = VALIDATED,
144        destination = RETURNING),
145
146    Transition(
147        transition_id = 'reset9',
148        title = 'Reset to validated',
149        msg = "Student record reset to 'validated'",
150        source = RETURNING,
151        destination = VALIDATED),
152    )
153
154LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance')
155UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance')
156
157registration_workflow = SIRPWorkflow(REGISTRATION_TRANSITIONS)
158
159class RegistrationWorkflowState(WorkflowState, grok.Adapter):
160    """An adapter to adapt Student objects to workflow states.
161    """
162    grok.context(IStudent)
163    grok.provides(IWorkflowState)
164
165    state_key = 'wf.registration.state'
166    state_id = 'wf.registration.id'
167
168class RegistrationWorkflowInfo(SIRPWorkflowInfo, grok.Adapter):
169    """Adapter to adapt Student objects to workflow info objects.
170    """
171    grok.context(IStudent)
172    grok.provides(ISIRPWorkflowInfo)
173
174    def __init__(self, context):
175        self.context = context
176        self.wf = registration_workflow
177
178@grok.subscribe(IStudent, IWorkflowTransitionEvent)
179def handle_student_transition_event(obj, event):
180    """Append message to student history and log file when transition happened.
181
182      Lock and unlock clearance form.
183    """
184    msg = '%s' % event.transition.user_data['msg']
185    history = IObjectHistory(obj)
186    history.addMessage(msg)
187    if event.transition.transition_id in LOCK_CLEARANCE_TRANS:
188        obj.clearance_locked = True
189    if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS:
190        obj.clearance_locked = False
191    # Student data don't change after first-time payment
192    if event.transition.transition_id == 'pay_first_school_fee':
193        pass
194    # School fee payment of returning students triggers the change of
195    # current session, current level, and current verdict
196    if event.transition.transition_id == 'pay_school_fee':
197        set_returning_data(obj)
198    # In some tests we don't have a students container or a user
199    try:
200        user = get_current_principal()
201        students_container = grok.getSite()['students']
202        students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg))
203    except (TypeError, AttributeError):
204        pass
205    return
Note: See TracBrowser for help on using the repository browser.