[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' |
---|
[6637] | 20 | |
---|
| 21 | REGISTRATION_TRANSITIONS = ( |
---|
| 22 | Transition( |
---|
| 23 | transition_id = 'create', |
---|
| 24 | title = 'Create student', |
---|
| 25 | source = None, |
---|
| 26 | condition = NullCondition, |
---|
[6638] | 27 | msg = 'Student record created', |
---|
[6637] | 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', |
---|
[6638] | 40 | msg = 'Student record reset', |
---|
[6637] | 41 | source = ADMITTED, |
---|
| 42 | destination = CREATED), |
---|
[6720] | 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), |
---|
[6742] | 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), |
---|
[6637] | 113 | ) |
---|
| 114 | |
---|
[6722] | 115 | LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance') |
---|
| 116 | UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance') |
---|
[6720] | 117 | |
---|
[6637] | 118 | registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS) |
---|
| 119 | |
---|
| 120 | class 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 | |
---|
| 129 | class 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) |
---|
| 140 | def handle_student_transition_event(obj, event): |
---|
[6644] | 141 | """Append message to student history and log file when transition happened. |
---|
[6637] | 142 | """ |
---|
| 143 | msg = '%s' % event.transition.user_data['msg'] |
---|
| 144 | history = IObjectHistory(obj) |
---|
| 145 | history.addMessage(msg) |
---|
[6722] | 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 |
---|
[6742] | 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) |
---|
[6644] | 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 |
---|
[6637] | 164 | return |
---|