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

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

Add school fee payment transitions.

Implement set_returning_data function which is triggered when payment transition is fired (returning students only).

Save integer values in session fields. This eases the numerical processing of session values.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1"""Workflow for students.
2"""
3import grok
4from datetime import datetime
5from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
6from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
7from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo
8from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo
9from waeup.sirp.utils.helpers import get_current_principal
10from waeup.sirp.students.interfaces import IStudent
11from waeup.sirp.students.utils import set_returning_data
12
13CREATED = 'created'
14ADMITTED = 'admitted'
15CLEARANCE = 'clearance started'
16REQUESTED = 'clearance requested'
17CLEARED = 'cleared'
18PAID = 'school fee paid'
19RETURNING = 'returning'
20
21REGISTRATION_TRANSITIONS = (
22    Transition(
23        transition_id = 'create',
24        title = 'Create student',
25        source = None,
26        condition = NullCondition,
27        msg = 'Student record created',
28        destination = CREATED),
29
30    Transition(
31        transition_id = 'admit',
32        title = 'Admit student',
33        msg = 'Student admitted',
34        source = CREATED,
35        destination = ADMITTED),
36
37    Transition(
38        transition_id = 'reset1',
39        title = 'Reset student',
40        msg = 'Student record reset',
41        source = ADMITTED,
42        destination = CREATED),
43
44    Transition(
45        transition_id = 'start_clearance',
46        title = 'Start clearance',
47        msg = 'Clearance started',
48        source = ADMITTED,
49        destination = CLEARANCE),
50
51    Transition(
52        transition_id = 'reset2',
53        title = 'Reset to admitted',
54        msg = 'Student record reset to admitted',
55        source = CLEARANCE,
56        destination = ADMITTED),
57
58    Transition(
59        transition_id = 'request_clearance',
60        title = 'Request clearance',
61        msg = 'Clearance requested',
62        source = CLEARANCE,
63        destination = REQUESTED),
64
65    Transition(
66        transition_id = 'reset3',
67        title = 'Reset to clearance',
68        msg = 'Student record reset to clearance',
69        source = REQUESTED,
70        destination = CLEARANCE),
71
72    Transition(
73        transition_id = 'clear',
74        title = 'Clear student',
75        msg = 'Cleared',
76        source = REQUESTED,
77        destination = CLEARED),
78
79    Transition(
80        transition_id = 'reset4',
81        title = 'Reset to clearance',
82        msg = 'Student record reset to clearance',
83        source = CLEARED,
84        destination = CLEARANCE),
85
86    Transition(
87        transition_id = 'pay_first_school_fee',
88        title = 'Pay school fee',
89        msg = 'School fee paid',
90        source = CLEARED,
91        destination = PAID),
92
93    Transition(
94        transition_id = 'reset5',
95        title = 'Reset to cleared',
96        msg = 'Student record reset to cleared',
97        source = PAID,
98        destination = CLEARED),
99
100    Transition(
101        transition_id = 'pay_school_fee',
102        title = 'Pay school fee',
103        msg = 'School fee paid',
104        source = RETURNING,
105        destination = PAID),
106
107    Transition(
108        transition_id = 'reset6',
109        title = 'Reset to returning',
110        msg = 'Student record reset to returning',
111        source = PAID,
112        destination = RETURNING),
113    )
114
115LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance')
116UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance')
117
118registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS)
119
120class RegistrationWorkflowState(WorkflowState, grok.Adapter):
121    """An adapter to adapt Student objects to workflow states.
122    """
123    grok.context(IStudent)
124    grok.provides(IWorkflowState)
125
126    state_key = 'wf.registration.state'
127    state_id = 'wf.registration.id'
128
129class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
130    """Adapter to adapt Student objects to workflow info objects.
131    """
132    grok.context(IStudent)
133    grok.provides(IWAeUPWorkflowInfo)
134
135    def __init__(self, context):
136        self.context = context
137        self.wf = registration_workflow
138
139@grok.subscribe(IStudent, IWorkflowTransitionEvent)
140def handle_student_transition_event(obj, event):
141    """Append message to student history and log file when transition happened.
142    """
143    msg = '%s' % event.transition.user_data['msg']
144    history = IObjectHistory(obj)
145    history.addMessage(msg)
146    if event.transition.transition_id in LOCK_CLEARANCE_TRANS:
147        obj.clearance_locked = True
148    if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS:
149        obj.clearance_locked = False
150    # Student data don't change after first-time payment
151    if event.transition.transition_id == 'pay_first_school_fee':
152        pass
153    # School fee payment of returning students triggers the change of
154    # current session, current level, and current verdict
155    if event.transition.transition_id == 'pay_school_fee':
156        set_returning_data(obj)
157    # In some tests we don't have a students container or a user
158    try:
159        user = get_current_principal()
160        students_container = grok.getSite()['students']
161        students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg))
162    except (TypeError, AttributeError):
163        pass
164    return
Note: See TracBrowser for help on using the repository browser.