[6637] | 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.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
| 8 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
[6644] | 9 | from waeup.sirp.utils.helpers import get_current_principal |
---|
[6742] | 10 | from waeup.sirp.students.interfaces import IStudent |
---|
| 11 | from waeup.sirp.students.utils import set_returning_data |
---|
[6637] | 12 | |
---|
| 13 | CREATED = 'created' |
---|
| 14 | ADMITTED = 'admitted' |
---|
[6723] | 15 | CLEARANCE = 'clearance started' |
---|
| 16 | REQUESTED = 'clearance requested' |
---|
[6720] | 17 | CLEARED = 'cleared' |
---|
[6742] | 18 | PAID = 'school fee paid' |
---|
| 19 | RETURNING = 'returning' |
---|
[6801] | 20 | REGISTERED = 'courses registered' |
---|
| 21 | VALIDATED = 'courses validated' |
---|
[6637] | 22 | |
---|
| 23 | REGISTRATION_TRANSITIONS = ( |
---|
| 24 | Transition( |
---|
| 25 | transition_id = 'create', |
---|
| 26 | title = 'Create student', |
---|
| 27 | source = None, |
---|
| 28 | condition = NullCondition, |
---|
[6638] | 29 | msg = 'Student record created', |
---|
[6637] | 30 | destination = CREATED), |
---|
| 31 | |
---|
| 32 | Transition( |
---|
| 33 | transition_id = 'admit', |
---|
| 34 | title = 'Admit student', |
---|
| 35 | msg = 'Student admitted', |
---|
| 36 | source = CREATED, |
---|
| 37 | destination = ADMITTED), |
---|
| 38 | |
---|
| 39 | Transition( |
---|
| 40 | transition_id = 'reset1', |
---|
| 41 | title = 'Reset student', |
---|
[6638] | 42 | msg = 'Student record reset', |
---|
[6637] | 43 | source = ADMITTED, |
---|
| 44 | destination = CREATED), |
---|
[6720] | 45 | |
---|
| 46 | Transition( |
---|
| 47 | transition_id = 'start_clearance', |
---|
| 48 | title = 'Start clearance', |
---|
| 49 | msg = 'Clearance started', |
---|
| 50 | source = ADMITTED, |
---|
| 51 | destination = CLEARANCE), |
---|
| 52 | |
---|
| 53 | Transition( |
---|
| 54 | transition_id = 'reset2', |
---|
| 55 | title = 'Reset to admitted', |
---|
| 56 | msg = 'Student record reset to admitted', |
---|
| 57 | source = CLEARANCE, |
---|
| 58 | destination = ADMITTED), |
---|
| 59 | |
---|
| 60 | Transition( |
---|
| 61 | transition_id = 'request_clearance', |
---|
| 62 | title = 'Request clearance', |
---|
| 63 | msg = 'Clearance requested', |
---|
| 64 | source = CLEARANCE, |
---|
| 65 | destination = REQUESTED), |
---|
| 66 | |
---|
| 67 | Transition( |
---|
| 68 | transition_id = 'reset3', |
---|
| 69 | title = 'Reset to clearance', |
---|
| 70 | msg = 'Student record reset to clearance', |
---|
| 71 | source = REQUESTED, |
---|
| 72 | destination = CLEARANCE), |
---|
| 73 | |
---|
| 74 | Transition( |
---|
| 75 | transition_id = 'clear', |
---|
| 76 | title = 'Clear student', |
---|
| 77 | msg = 'Cleared', |
---|
| 78 | source = REQUESTED, |
---|
| 79 | destination = CLEARED), |
---|
| 80 | |
---|
| 81 | Transition( |
---|
| 82 | transition_id = 'reset4', |
---|
| 83 | title = 'Reset to clearance', |
---|
| 84 | msg = 'Student record reset to clearance', |
---|
| 85 | source = CLEARED, |
---|
| 86 | destination = CLEARANCE), |
---|
[6742] | 87 | |
---|
| 88 | Transition( |
---|
| 89 | transition_id = 'pay_first_school_fee', |
---|
| 90 | title = 'Pay school fee', |
---|
| 91 | msg = 'School fee paid', |
---|
| 92 | source = CLEARED, |
---|
| 93 | destination = PAID), |
---|
| 94 | |
---|
| 95 | Transition( |
---|
| 96 | transition_id = 'reset5', |
---|
| 97 | title = 'Reset to cleared', |
---|
| 98 | msg = 'Student record reset to cleared', |
---|
| 99 | source = PAID, |
---|
| 100 | destination = CLEARED), |
---|
| 101 | |
---|
| 102 | Transition( |
---|
| 103 | transition_id = 'pay_school_fee', |
---|
| 104 | title = 'Pay school fee', |
---|
| 105 | msg = 'School fee paid', |
---|
| 106 | source = RETURNING, |
---|
| 107 | destination = PAID), |
---|
| 108 | |
---|
| 109 | Transition( |
---|
| 110 | transition_id = 'reset6', |
---|
| 111 | title = 'Reset to returning', |
---|
| 112 | msg = 'Student record reset to returning', |
---|
| 113 | source = PAID, |
---|
| 114 | destination = RETURNING), |
---|
[6801] | 115 | |
---|
| 116 | Transition( |
---|
| 117 | transition_id = 'register_courses', |
---|
| 118 | title = 'Register courses', |
---|
| 119 | msg = 'Courses registered', |
---|
| 120 | source = PAID, |
---|
| 121 | destination = REGISTERED), |
---|
| 122 | |
---|
| 123 | Transition( |
---|
| 124 | transition_id = 'reset7', |
---|
| 125 | title = 'Reset to paid', |
---|
| 126 | msg = 'Student record reset to paid', |
---|
| 127 | source = REGISTERED, |
---|
| 128 | destination = PAID), |
---|
| 129 | |
---|
| 130 | Transition( |
---|
| 131 | transition_id = 'validate_courses', |
---|
| 132 | title = 'Validate courses', |
---|
| 133 | msg = 'Courses validated', |
---|
| 134 | source = REGISTERED, |
---|
| 135 | destination = VALIDATED), |
---|
| 136 | |
---|
| 137 | Transition( |
---|
| 138 | transition_id = 'reset8', |
---|
| 139 | title = 'Reset to paid', |
---|
| 140 | msg = 'Student record reset to paid', |
---|
| 141 | source = VALIDATED, |
---|
| 142 | destination = PAID), |
---|
| 143 | |
---|
| 144 | Transition( |
---|
| 145 | transition_id = 'return', |
---|
| 146 | title = 'Return', |
---|
| 147 | msg = 'Returned', |
---|
| 148 | source = VALIDATED, |
---|
| 149 | destination = RETURNING), |
---|
| 150 | |
---|
| 151 | Transition( |
---|
| 152 | transition_id = 'reset9', |
---|
| 153 | title = 'Reset to validated', |
---|
| 154 | msg = 'Student record reset to validated', |
---|
| 155 | source = RETURNING, |
---|
| 156 | destination = VALIDATED), |
---|
[6637] | 157 | ) |
---|
| 158 | |
---|
[6722] | 159 | LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance') |
---|
| 160 | UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance') |
---|
[6720] | 161 | |
---|
[6637] | 162 | registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS) |
---|
| 163 | |
---|
| 164 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
| 165 | """An adapter to adapt Student objects to workflow states. |
---|
| 166 | """ |
---|
| 167 | grok.context(IStudent) |
---|
| 168 | grok.provides(IWorkflowState) |
---|
| 169 | |
---|
| 170 | state_key = 'wf.registration.state' |
---|
| 171 | state_id = 'wf.registration.id' |
---|
| 172 | |
---|
| 173 | class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
| 174 | """Adapter to adapt Student objects to workflow info objects. |
---|
| 175 | """ |
---|
| 176 | grok.context(IStudent) |
---|
| 177 | grok.provides(IWAeUPWorkflowInfo) |
---|
| 178 | |
---|
| 179 | def __init__(self, context): |
---|
| 180 | self.context = context |
---|
| 181 | self.wf = registration_workflow |
---|
| 182 | |
---|
| 183 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
| 184 | def handle_student_transition_event(obj, event): |
---|
[6644] | 185 | """Append message to student history and log file when transition happened. |
---|
[6637] | 186 | """ |
---|
| 187 | msg = '%s' % event.transition.user_data['msg'] |
---|
| 188 | history = IObjectHistory(obj) |
---|
| 189 | history.addMessage(msg) |
---|
[6722] | 190 | if event.transition.transition_id in LOCK_CLEARANCE_TRANS: |
---|
| 191 | obj.clearance_locked = True |
---|
| 192 | if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS: |
---|
| 193 | obj.clearance_locked = False |
---|
[6742] | 194 | # Student data don't change after first-time payment |
---|
| 195 | if event.transition.transition_id == 'pay_first_school_fee': |
---|
| 196 | pass |
---|
| 197 | # School fee payment of returning students triggers the change of |
---|
| 198 | # current session, current level, and current verdict |
---|
| 199 | if event.transition.transition_id == 'pay_school_fee': |
---|
| 200 | set_returning_data(obj) |
---|
[6644] | 201 | # In some tests we don't have a students container or a user |
---|
| 202 | try: |
---|
| 203 | user = get_current_principal() |
---|
| 204 | students_container = grok.getSite()['students'] |
---|
| 205 | students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg)) |
---|
| 206 | except (TypeError, AttributeError): |
---|
| 207 | pass |
---|
[6637] | 208 | return |
---|