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 | from waeup.sirp.utils.helpers import get_current_principal |
---|
11 | |
---|
12 | CREATED = 'created' |
---|
13 | ADMITTED = 'admitted' |
---|
14 | |
---|
15 | REGISTRATION_TRANSITIONS = ( |
---|
16 | Transition( |
---|
17 | transition_id = 'create', |
---|
18 | title = 'Create student', |
---|
19 | source = None, |
---|
20 | condition = NullCondition, |
---|
21 | msg = 'Student record created', |
---|
22 | destination = CREATED), |
---|
23 | |
---|
24 | Transition( |
---|
25 | transition_id = 'admit', |
---|
26 | title = 'Admit student', |
---|
27 | msg = 'Student admitted', |
---|
28 | source = CREATED, |
---|
29 | destination = ADMITTED), |
---|
30 | |
---|
31 | Transition( |
---|
32 | transition_id = 'reset1', |
---|
33 | title = 'Reset student', |
---|
34 | msg = 'Student record reset', |
---|
35 | source = ADMITTED, |
---|
36 | destination = CREATED), |
---|
37 | ) |
---|
38 | |
---|
39 | registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS) |
---|
40 | |
---|
41 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
42 | """An adapter to adapt Student objects to workflow states. |
---|
43 | """ |
---|
44 | grok.context(IStudent) |
---|
45 | grok.provides(IWorkflowState) |
---|
46 | |
---|
47 | state_key = 'wf.registration.state' |
---|
48 | state_id = 'wf.registration.id' |
---|
49 | |
---|
50 | class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
51 | """Adapter to adapt Student objects to workflow info objects. |
---|
52 | """ |
---|
53 | grok.context(IStudent) |
---|
54 | grok.provides(IWAeUPWorkflowInfo) |
---|
55 | |
---|
56 | def __init__(self, context): |
---|
57 | self.context = context |
---|
58 | self.wf = registration_workflow |
---|
59 | |
---|
60 | @grok.subscribe(IStudent, IWorkflowTransitionEvent) |
---|
61 | def handle_student_transition_event(obj, event): |
---|
62 | """Append message to student history and log file when transition happened. |
---|
63 | """ |
---|
64 | msg = '%s' % event.transition.user_data['msg'] |
---|
65 | history = IObjectHistory(obj) |
---|
66 | history.addMessage(msg) |
---|
67 | # In some tests we don't have a students container or a user |
---|
68 | try: |
---|
69 | user = get_current_principal() |
---|
70 | students_container = grok.getSite()['students'] |
---|
71 | students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg)) |
---|
72 | except (TypeError, AttributeError): |
---|
73 | pass |
---|
74 | return |
---|