[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.students.interfaces import IStudent |
---|
| 8 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
| 9 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
| 10 | |
---|
| 11 | CREATED = 'created' |
---|
| 12 | ADMITTED = 'admitted' |
---|
| 13 | |
---|
| 14 | REGISTRATION_TRANSITIONS = ( |
---|
| 15 | Transition( |
---|
| 16 | transition_id = 'create', |
---|
| 17 | title = 'Create student', |
---|
| 18 | source = None, |
---|
| 19 | condition = NullCondition, |
---|
[6638] | 20 | msg = 'Student record created', |
---|
[6637] | 21 | destination = CREATED), |
---|
| 22 | |
---|
| 23 | Transition( |
---|
| 24 | transition_id = 'admit', |
---|
| 25 | title = 'Admit student', |
---|
| 26 | msg = 'Student admitted', |
---|
| 27 | source = CREATED, |
---|
| 28 | destination = ADMITTED), |
---|
| 29 | |
---|
| 30 | Transition( |
---|
| 31 | transition_id = 'reset1', |
---|
| 32 | title = 'Reset student', |
---|
[6638] | 33 | msg = 'Student record reset', |
---|
[6637] | 34 | source = ADMITTED, |
---|
| 35 | destination = CREATED), |
---|
| 36 | ) |
---|
| 37 | |
---|
| 38 | registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS) |
---|
| 39 | |
---|
| 40 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
| 41 | """An adapter to adapt Student objects to workflow states. |
---|
| 42 | """ |
---|
| 43 | grok.context(IStudent) |
---|
| 44 | grok.provides(IWorkflowState) |
---|
| 45 | |
---|
| 46 | state_key = 'wf.registration.state' |
---|
| 47 | state_id = 'wf.registration.id' |
---|
| 48 | |
---|
| 49 | class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
| 50 | """Adapter to adapt Student objects to workflow info objects. |
---|
| 51 | """ |
---|
| 52 | grok.context(IStudent) |
---|
| 53 | grok.provides(IWAeUPWorkflowInfo) |
---|
| 54 | |
---|
| 55 | def __init__(self, context): |
---|
| 56 | self.context = context |
---|
| 57 | self.wf = registration_workflow |
---|
| 58 | |
---|
| 59 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
| 60 | def handle_student_transition_event(obj, event): |
---|
| 61 | """Append message to student history when transition happened. |
---|
| 62 | """ |
---|
| 63 | msg = '%s' % event.transition.user_data['msg'] |
---|
| 64 | history = IObjectHistory(obj) |
---|
| 65 | history.addMessage(msg) |
---|
| 66 | return |
---|