1 | ## $Id: workflow.py 7192 2011-11-25 07:15:50Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Workflow for applicants. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
22 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
23 | from waeup.sirp.applicants.interfaces import IApplicantBaseData |
---|
24 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
25 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
26 | from waeup.sirp.utils.helpers import get_current_principal |
---|
27 | |
---|
28 | INITIALIZED = 'initialized' |
---|
29 | STARTED = 'started' |
---|
30 | SUBMITTED = 'submitted' |
---|
31 | ADMITTED = 'admitted' |
---|
32 | NOT_ADMITTED = 'not admitted' |
---|
33 | CREATED = 'created' |
---|
34 | |
---|
35 | APPLICATION_TRANSITIONS = ( |
---|
36 | Transition( |
---|
37 | transition_id = 'init', |
---|
38 | title = 'Initialize application', |
---|
39 | source = None, |
---|
40 | condition = NullCondition, |
---|
41 | msg = 'Application initialized', |
---|
42 | destination = INITIALIZED), |
---|
43 | |
---|
44 | Transition( |
---|
45 | transition_id = 'start', |
---|
46 | title = 'Start application', |
---|
47 | msg = 'Application started', |
---|
48 | source = INITIALIZED, |
---|
49 | destination = STARTED), |
---|
50 | |
---|
51 | Transition( |
---|
52 | transition_id = 'submit', |
---|
53 | title = 'Submit application', |
---|
54 | msg = 'Application submitted', |
---|
55 | source = STARTED, |
---|
56 | destination = SUBMITTED), |
---|
57 | |
---|
58 | Transition( |
---|
59 | transition_id = 'admit', |
---|
60 | title = 'Admit applicant', |
---|
61 | msg = 'Applicant admitted', |
---|
62 | source = SUBMITTED, |
---|
63 | destination = ADMITTED), |
---|
64 | |
---|
65 | Transition( |
---|
66 | transition_id = 'refuse1', |
---|
67 | title = 'Refuse application', |
---|
68 | msg = 'Application refused', |
---|
69 | source = SUBMITTED, |
---|
70 | destination = NOT_ADMITTED), |
---|
71 | |
---|
72 | Transition( |
---|
73 | transition_id = 'refuse2', |
---|
74 | title = 'Refuse application', |
---|
75 | msg = 'Application refused', |
---|
76 | source = ADMITTED, |
---|
77 | destination = NOT_ADMITTED), |
---|
78 | |
---|
79 | Transition( |
---|
80 | transition_id = 'create', |
---|
81 | title = 'Create student record', |
---|
82 | msg = 'Student record created', |
---|
83 | source = ADMITTED, |
---|
84 | destination = CREATED), |
---|
85 | |
---|
86 | Transition( |
---|
87 | transition_id = 'reset1', |
---|
88 | title = 'Reset application', |
---|
89 | msg = 'Application reset', |
---|
90 | source = SUBMITTED, |
---|
91 | destination = STARTED), |
---|
92 | |
---|
93 | Transition( |
---|
94 | transition_id = 'reset2', |
---|
95 | title = 'Reset application', |
---|
96 | msg = 'Application reset', |
---|
97 | source = ADMITTED, |
---|
98 | destination = STARTED), |
---|
99 | |
---|
100 | Transition( |
---|
101 | transition_id = 'reset3', |
---|
102 | title = 'Reset application', |
---|
103 | msg = 'Application reset', |
---|
104 | source = NOT_ADMITTED, |
---|
105 | destination = STARTED), |
---|
106 | |
---|
107 | Transition( |
---|
108 | transition_id = 'reset4', |
---|
109 | title = 'Reset application', |
---|
110 | msg = 'Application reset', |
---|
111 | source = CREATED, |
---|
112 | destination = STARTED), |
---|
113 | ) |
---|
114 | |
---|
115 | application_workflow = WAeUPWorkflow(APPLICATION_TRANSITIONS) |
---|
116 | |
---|
117 | class ApplicationWorkflowState(WorkflowState, grok.Adapter): |
---|
118 | """An adapter to adapt Applicant objects to workflow states. |
---|
119 | """ |
---|
120 | grok.context(IApplicantBaseData) |
---|
121 | grok.provides(IWorkflowState) |
---|
122 | |
---|
123 | state_key = 'wf.application.state' |
---|
124 | state_id = 'wf.application.id' |
---|
125 | |
---|
126 | class ApplicationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
127 | """Adapter to adapt Applicant objects to workflow info objects. |
---|
128 | """ |
---|
129 | grok.context(IApplicantBaseData) |
---|
130 | grok.provides(IWAeUPWorkflowInfo) |
---|
131 | |
---|
132 | def __init__(self, context): |
---|
133 | self.context = context |
---|
134 | self.wf = application_workflow |
---|
135 | |
---|
136 | @grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent) |
---|
137 | def handle_applicant_transition_event(obj, event): |
---|
138 | """Append message to applicant history when transition happened. |
---|
139 | """ |
---|
140 | msg = '%s' % event.transition.user_data['msg'] |
---|
141 | history = IObjectHistory(obj) |
---|
142 | history.addMessage(msg) |
---|
143 | # In some tests we don't have a an applicants root or a user |
---|
144 | try: |
---|
145 | user = get_current_principal() |
---|
146 | applicants_root = grok.getSite()['applicants'] |
---|
147 | applicants_root.logger.info('%s - %s - %s' % (user.id,obj.access_code,msg)) |
---|
148 | except (TypeError, AttributeError): |
---|
149 | pass |
---|
150 | return |
---|