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