[6637] | 1 | """Workflow for students. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[9161] | 4 | from datetime import datetime |
---|
[7615] | 5 | from zope.component import getUtility |
---|
[6637] | 6 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
| 7 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
[7811] | 8 | from waeup.kofa.interfaces import ( |
---|
[7819] | 9 | IObjectHistory, IKofaWorkflowInfo, IKofaUtils, |
---|
[6990] | 10 | CREATED, ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
| 11 | REGISTERED, VALIDATED) |
---|
[7811] | 12 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7819] | 13 | from waeup.kofa.workflow import KofaWorkflow, KofaWorkflowInfo |
---|
[7811] | 14 | from waeup.kofa.students.interfaces import IStudent, IStudentsUtils |
---|
[9161] | 15 | from waeup.kofa.utils.helpers import get_current_principal |
---|
[6637] | 16 | |
---|
[7615] | 17 | |
---|
[7513] | 18 | IMPORTABLE_STATES = (ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
| 19 | REGISTERED, VALIDATED) |
---|
| 20 | |
---|
[9028] | 21 | FORBIDDEN_POSTGRAD_STATES = (RETURNING, REGISTERED, VALIDATED) |
---|
| 22 | |
---|
[6637] | 23 | REGISTRATION_TRANSITIONS = ( |
---|
| 24 | Transition( |
---|
| 25 | transition_id = 'create', |
---|
[7677] | 26 | title = _('Create student'), |
---|
[6637] | 27 | source = None, |
---|
| 28 | condition = NullCondition, |
---|
[9022] | 29 | msg = _('Record created'), |
---|
[6637] | 30 | destination = CREATED), |
---|
| 31 | |
---|
| 32 | Transition( |
---|
| 33 | transition_id = 'admit', |
---|
[7670] | 34 | title = _('Admit student'), |
---|
[9022] | 35 | msg = _('Admitted'), |
---|
[6637] | 36 | source = CREATED, |
---|
| 37 | destination = ADMITTED), |
---|
| 38 | |
---|
| 39 | Transition( |
---|
| 40 | transition_id = 'reset1', |
---|
[7677] | 41 | title = _('Reset student'), |
---|
[7996] | 42 | msg = _('Reset to initial state'), |
---|
[6637] | 43 | source = ADMITTED, |
---|
| 44 | destination = CREATED), |
---|
[6720] | 45 | |
---|
| 46 | Transition( |
---|
| 47 | transition_id = 'start_clearance', |
---|
[7677] | 48 | title = _('Start clearance'), |
---|
| 49 | msg = _('Clearance started'), |
---|
[6720] | 50 | source = ADMITTED, |
---|
| 51 | destination = CLEARANCE), |
---|
| 52 | |
---|
| 53 | Transition( |
---|
| 54 | transition_id = 'reset2', |
---|
[7677] | 55 | title = _('Reset to admitted'), |
---|
[7996] | 56 | msg = _("Reset to 'admitted'"), |
---|
[6720] | 57 | source = CLEARANCE, |
---|
| 58 | destination = ADMITTED), |
---|
| 59 | |
---|
| 60 | Transition( |
---|
| 61 | transition_id = 'request_clearance', |
---|
[7677] | 62 | title = _('Request clearance'), |
---|
| 63 | msg = _('Clearance requested'), |
---|
[6720] | 64 | source = CLEARANCE, |
---|
| 65 | destination = REQUESTED), |
---|
| 66 | |
---|
| 67 | Transition( |
---|
| 68 | transition_id = 'reset3', |
---|
[9022] | 69 | title = _('Reset to clearance started'), |
---|
| 70 | msg = _("Reset to 'clearance started'"), |
---|
[6720] | 71 | source = REQUESTED, |
---|
| 72 | destination = CLEARANCE), |
---|
| 73 | |
---|
| 74 | Transition( |
---|
| 75 | transition_id = 'clear', |
---|
[7677] | 76 | title = _('Clear student'), |
---|
| 77 | msg = _('Cleared'), |
---|
[6720] | 78 | source = REQUESTED, |
---|
| 79 | destination = CLEARED), |
---|
| 80 | |
---|
| 81 | Transition( |
---|
| 82 | transition_id = 'reset4', |
---|
[9022] | 83 | title = _('Reset to clearance started'), |
---|
| 84 | msg = _("Reset to 'clearance started'"), |
---|
[6720] | 85 | source = CLEARED, |
---|
| 86 | destination = CLEARANCE), |
---|
[6742] | 87 | |
---|
| 88 | Transition( |
---|
| 89 | transition_id = 'pay_first_school_fee', |
---|
[7677] | 90 | title = _('Pay school fee'), |
---|
[9022] | 91 | msg = _('First school fee payment made'), |
---|
[6742] | 92 | source = CLEARED, |
---|
| 93 | destination = PAID), |
---|
| 94 | |
---|
| 95 | Transition( |
---|
[8434] | 96 | transition_id = 'approve_first_school_fee', |
---|
| 97 | title = _('Approve payment'), |
---|
[9022] | 98 | msg = _('First school fee payment approved'), |
---|
[8434] | 99 | source = CLEARED, |
---|
| 100 | destination = PAID), |
---|
| 101 | |
---|
| 102 | Transition( |
---|
[6742] | 103 | transition_id = 'reset5', |
---|
[7677] | 104 | title = _('Reset to cleared'), |
---|
[7996] | 105 | msg = _("Reset to 'cleared'"), |
---|
[6742] | 106 | source = PAID, |
---|
| 107 | destination = CLEARED), |
---|
| 108 | |
---|
| 109 | Transition( |
---|
| 110 | transition_id = 'pay_school_fee', |
---|
[7677] | 111 | title = _('Pay school fee'), |
---|
[9022] | 112 | msg = _('School fee payment made'), |
---|
[6742] | 113 | source = RETURNING, |
---|
| 114 | destination = PAID), |
---|
| 115 | |
---|
| 116 | Transition( |
---|
[8471] | 117 | transition_id = 'pay_pg_fee', |
---|
[9022] | 118 | title = _('Pay PG school fee'), |
---|
| 119 | msg = _('PG school fee payment made'), |
---|
[8471] | 120 | source = PAID, |
---|
| 121 | destination = PAID), |
---|
| 122 | |
---|
| 123 | Transition( |
---|
[8434] | 124 | transition_id = 'approve_school_fee', |
---|
[9022] | 125 | title = _('Approve school fee payment'), |
---|
[8434] | 126 | msg = _('School fee payment approved'), |
---|
| 127 | source = RETURNING, |
---|
| 128 | destination = PAID), |
---|
| 129 | |
---|
| 130 | Transition( |
---|
[8471] | 131 | transition_id = 'approve_pg_fee', |
---|
[9022] | 132 | title = _('Approve PG school fee payment'), |
---|
| 133 | msg = _('PG school fee payment approved'), |
---|
[8471] | 134 | source = PAID, |
---|
| 135 | destination = PAID), |
---|
| 136 | |
---|
| 137 | Transition( |
---|
[6742] | 138 | transition_id = 'reset6', |
---|
[7677] | 139 | title = _('Reset to returning'), |
---|
[7996] | 140 | msg = _("Reset to 'returning'"), |
---|
[6742] | 141 | source = PAID, |
---|
| 142 | destination = RETURNING), |
---|
[6801] | 143 | |
---|
| 144 | Transition( |
---|
| 145 | transition_id = 'register_courses', |
---|
[7677] | 146 | title = _('Register courses'), |
---|
| 147 | msg = _('Courses registered'), |
---|
[6801] | 148 | source = PAID, |
---|
| 149 | destination = REGISTERED), |
---|
| 150 | |
---|
| 151 | Transition( |
---|
| 152 | transition_id = 'reset7', |
---|
[9022] | 153 | title = _('Reset to school fee paid'), |
---|
| 154 | msg = _("Reset to 'school fee paid'"), |
---|
[6801] | 155 | source = REGISTERED, |
---|
| 156 | destination = PAID), |
---|
| 157 | |
---|
| 158 | Transition( |
---|
| 159 | transition_id = 'validate_courses', |
---|
[7677] | 160 | title = _('Validate courses'), |
---|
| 161 | msg = _('Courses validated'), |
---|
[6801] | 162 | source = REGISTERED, |
---|
| 163 | destination = VALIDATED), |
---|
| 164 | |
---|
| 165 | Transition( |
---|
[9284] | 166 | transition_id = 'bypass_validation', |
---|
| 167 | title = _('Return and bypass validation'), |
---|
[9295] | 168 | msg = _("Course validation bypassed"), |
---|
[9284] | 169 | source = REGISTERED, |
---|
| 170 | destination = RETURNING), |
---|
| 171 | |
---|
| 172 | Transition( |
---|
[6801] | 173 | transition_id = 'reset8', |
---|
[9022] | 174 | title = _('Reset to school fee paid'), |
---|
| 175 | msg = _("Reset to 'school fee paid'"), |
---|
[6801] | 176 | source = VALIDATED, |
---|
| 177 | destination = PAID), |
---|
| 178 | |
---|
| 179 | Transition( |
---|
| 180 | transition_id = 'return', |
---|
[7677] | 181 | title = _('Return'), |
---|
[9284] | 182 | msg = _("Returned"), |
---|
[6801] | 183 | source = VALIDATED, |
---|
| 184 | destination = RETURNING), |
---|
| 185 | |
---|
| 186 | Transition( |
---|
| 187 | transition_id = 'reset9', |
---|
[9022] | 188 | title = _('Reset to courses validated'), |
---|
| 189 | msg = _("Reset to 'courses validated'"), |
---|
[6801] | 190 | source = RETURNING, |
---|
| 191 | destination = VALIDATED), |
---|
[6637] | 192 | ) |
---|
| 193 | |
---|
[8309] | 194 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
| 195 | |
---|
[9028] | 196 | FORBIDDEN_POSTGRAD_TRANS = ['reset6', 'register_courses'] |
---|
[6722] | 197 | LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance') |
---|
| 198 | UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance') |
---|
[6720] | 199 | |
---|
[7819] | 200 | registration_workflow = KofaWorkflow(REGISTRATION_TRANSITIONS) |
---|
[6637] | 201 | |
---|
| 202 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
| 203 | """An adapter to adapt Student objects to workflow states. |
---|
| 204 | """ |
---|
| 205 | grok.context(IStudent) |
---|
| 206 | grok.provides(IWorkflowState) |
---|
| 207 | |
---|
| 208 | state_key = 'wf.registration.state' |
---|
| 209 | state_id = 'wf.registration.id' |
---|
| 210 | |
---|
[7819] | 211 | class RegistrationWorkflowInfo(KofaWorkflowInfo, grok.Adapter): |
---|
[6637] | 212 | """Adapter to adapt Student objects to workflow info objects. |
---|
| 213 | """ |
---|
| 214 | grok.context(IStudent) |
---|
[7819] | 215 | grok.provides(IKofaWorkflowInfo) |
---|
[6637] | 216 | |
---|
| 217 | def __init__(self, context): |
---|
| 218 | self.context = context |
---|
| 219 | self.wf = registration_workflow |
---|
| 220 | |
---|
| 221 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
| 222 | def handle_student_transition_event(obj, event): |
---|
[6644] | 223 | """Append message to student history and log file when transition happened. |
---|
[7133] | 224 | |
---|
[7681] | 225 | Lock and unlock clearance form. |
---|
[9005] | 226 | Trigger actions after school fee payment. |
---|
[6637] | 227 | """ |
---|
[7679] | 228 | |
---|
| 229 | msg = event.transition.user_data['msg'] |
---|
[6637] | 230 | history = IObjectHistory(obj) |
---|
| 231 | history.addMessage(msg) |
---|
[6722] | 232 | if event.transition.transition_id in LOCK_CLEARANCE_TRANS: |
---|
| 233 | obj.clearance_locked = True |
---|
| 234 | if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS: |
---|
| 235 | obj.clearance_locked = False |
---|
[6742] | 236 | # School fee payment of returning students triggers the change of |
---|
| 237 | # current session, current level, and current verdict |
---|
[8434] | 238 | if event.transition.transition_id in ( |
---|
| 239 | 'pay_school_fee', 'approve_school_fee'): |
---|
[7615] | 240 | getUtility(IStudentsUtils).setReturningData(obj) |
---|
[8471] | 241 | elif event.transition.transition_id in ( |
---|
| 242 | 'pay_pg_fee', 'approve_pg_fee'): |
---|
| 243 | new_session = obj['studycourse'].current_session + 1 |
---|
| 244 | obj['studycourse'].current_session = new_session |
---|
[9161] | 245 | elif event.transition.transition_id == 'validate_courses': |
---|
| 246 | current_level = obj['studycourse'].current_level |
---|
| 247 | level_object = obj['studycourse'].get(str(current_level), None) |
---|
| 248 | if level_object is not None: |
---|
| 249 | user = get_current_principal() |
---|
| 250 | if user is None: |
---|
| 251 | usertitle = 'system' |
---|
| 252 | else: |
---|
| 253 | usertitle = getattr(user, 'public_name', None) |
---|
| 254 | if not usertitle: |
---|
| 255 | usertitle = user.title |
---|
| 256 | level_object.validated_by = usertitle |
---|
| 257 | level_object.validation_date = datetime.utcnow() |
---|
[9484] | 258 | elif event.transition.transition_id == 'clear': |
---|
[9486] | 259 | obj.officer_comment = None |
---|
[9161] | 260 | elif event.transition.transition_id == 'reset8': |
---|
| 261 | current_level = obj['studycourse'].current_level |
---|
| 262 | level_object = obj['studycourse'].get(str(current_level), None) |
---|
| 263 | if level_object is not None: |
---|
| 264 | level_object.validated_by = None |
---|
| 265 | level_object.validation_date = None |
---|
[7652] | 266 | # In some tests we don't have a students container |
---|
[6644] | 267 | try: |
---|
| 268 | students_container = grok.getSite()['students'] |
---|
[7652] | 269 | students_container.logger.info('%s - %s' % (obj.student_id,msg)) |
---|
[6644] | 270 | except (TypeError, AttributeError): |
---|
| 271 | pass |
---|
[6637] | 272 | return |
---|