1 | """Workflow for applicants. |
---|
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.applicants.interfaces import IApplicantBaseData |
---|
8 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
9 | from waeup.sirp.workflow import ( |
---|
10 | WAeUPWorkflow, WAeUPWorkflowInfo, NULL_TRANSITION) |
---|
11 | |
---|
12 | INITIALIZED = 'initialized' |
---|
13 | STARTED = 'started' |
---|
14 | SUBMITTED = 'submitted' |
---|
15 | ADMITTED = 'admitted' |
---|
16 | NOT_ADMITTED = 'not admitted' |
---|
17 | CREATED = 'created' |
---|
18 | |
---|
19 | APPLICATION_TRANSITIONS = ( |
---|
20 | NULL_TRANSITION, |
---|
21 | |
---|
22 | Transition( |
---|
23 | transition_id = 'init', |
---|
24 | title = 'Initialize application', |
---|
25 | source = None, |
---|
26 | condition = NullCondition, |
---|
27 | msg = 'application process initialized', |
---|
28 | destination = INITIALIZED), |
---|
29 | |
---|
30 | Transition( |
---|
31 | transition_id = 'start', |
---|
32 | title = 'Start application', |
---|
33 | msg = 'application process started', |
---|
34 | source = INITIALIZED, |
---|
35 | destination = STARTED), |
---|
36 | |
---|
37 | Transition( |
---|
38 | transition_id = 'submit', |
---|
39 | title = 'Submit application', |
---|
40 | msg = 'application record submitted', |
---|
41 | source = STARTED, |
---|
42 | destination = SUBMITTED), |
---|
43 | |
---|
44 | Transition( |
---|
45 | transition_id = 'admit', |
---|
46 | title = 'Admit applicant', |
---|
47 | msg = 'applicant admitted', |
---|
48 | source = SUBMITTED, |
---|
49 | destination = ADMITTED), |
---|
50 | |
---|
51 | Transition( |
---|
52 | transition_id = 'refuse1', |
---|
53 | title = 'Refuse application', |
---|
54 | msg = 'application refused', |
---|
55 | source = SUBMITTED, |
---|
56 | destination = NOT_ADMITTED), |
---|
57 | |
---|
58 | Transition( |
---|
59 | transition_id = 'refuse2', |
---|
60 | title = 'Refuse application', |
---|
61 | msg = 'application refused', |
---|
62 | source = ADMITTED, |
---|
63 | destination = NOT_ADMITTED), |
---|
64 | |
---|
65 | Transition( |
---|
66 | transition_id = 'create', |
---|
67 | title = 'Create student record', |
---|
68 | msg = 'student record created', |
---|
69 | source = ADMITTED, |
---|
70 | destination = CREATED), |
---|
71 | |
---|
72 | Transition( |
---|
73 | transition_id = 'reset1', |
---|
74 | title = 'Reset application', |
---|
75 | msg = 'application record reset', |
---|
76 | source = SUBMITTED, |
---|
77 | destination = STARTED), |
---|
78 | |
---|
79 | Transition( |
---|
80 | transition_id = 'reset2', |
---|
81 | title = 'Reset application', |
---|
82 | msg = 'application record reset', |
---|
83 | source = ADMITTED, |
---|
84 | destination = STARTED), |
---|
85 | |
---|
86 | Transition( |
---|
87 | transition_id = 'reset3', |
---|
88 | title = 'Reset application', |
---|
89 | msg = 'application record reset', |
---|
90 | source = NOT_ADMITTED, |
---|
91 | destination = STARTED), |
---|
92 | |
---|
93 | Transition( |
---|
94 | transition_id = 'reset4', |
---|
95 | title = 'Reset application', |
---|
96 | msg = 'application record reset', |
---|
97 | source = CREATED, |
---|
98 | destination = STARTED), |
---|
99 | ) |
---|
100 | |
---|
101 | application_workflow = WAeUPWorkflow(APPLICATION_TRANSITIONS) |
---|
102 | |
---|
103 | class ApplicationWorkflowState(WorkflowState, grok.Adapter): |
---|
104 | """An adapter to adapt Applicant objects to workflow states. |
---|
105 | """ |
---|
106 | grok.context(IApplicantBaseData) |
---|
107 | grok.provides(IWorkflowState) |
---|
108 | |
---|
109 | state_key = 'wf.application.state' |
---|
110 | state_id = 'wf.application.id' |
---|
111 | |
---|
112 | class ApplicationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
113 | """Adapter to adapt Applicant objects to workflow info objects. |
---|
114 | """ |
---|
115 | grok.context(IApplicantBaseData) |
---|
116 | grok.provides(IWAeUPWorkflowInfo) |
---|
117 | |
---|
118 | def __init__(self, context): |
---|
119 | self.context = context |
---|
120 | self.wf = application_workflow |
---|
121 | |
---|
122 | @grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent) |
---|
123 | def handle_applicant_transition_event(obj, event): |
---|
124 | """Append message to applicant when transition happened. |
---|
125 | """ |
---|
126 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
127 | msg = '%s - %s (new state: %s)' % ( |
---|
128 | timestamp, event.transition.user_data['msg'], event.destination) |
---|
129 | history = IObjectHistory(obj) |
---|
130 | history.addMessage(msg) |
---|
131 | return |
---|