1 | ## $Id: batching.py 11891 2014-10-28 20:02:45Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 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 customers. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from datetime import datetime |
---|
22 | from zope.component import getUtility |
---|
23 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
24 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
25 | from waeup.ikoba.interfaces import ( |
---|
26 | IObjectHistory, IIkobaWorkflowInfo, IIkobaUtils, |
---|
27 | CREATED, ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
28 | REGISTERED, VALIDATED, GRADUATED, TRANSCRIPT) |
---|
29 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
30 | from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo |
---|
31 | from waeup.ikoba.customers.interfaces import ICustomer, ICustomersUtils |
---|
32 | from waeup.ikoba.utils.helpers import get_current_principal |
---|
33 | |
---|
34 | |
---|
35 | IMPORTABLE_STATES = (ADMITTED, CLEARANCE, REQUESTED, CLEARED, PAID, RETURNING, |
---|
36 | REGISTERED, VALIDATED, GRADUATED) |
---|
37 | |
---|
38 | REGISTRATION_TRANSITIONS = ( |
---|
39 | Transition( |
---|
40 | transition_id = 'create', |
---|
41 | title = _('Create customer'), |
---|
42 | source = None, |
---|
43 | condition = NullCondition, |
---|
44 | msg = _('Record created'), |
---|
45 | destination = CREATED), |
---|
46 | |
---|
47 | Transition( |
---|
48 | transition_id = 'admit', |
---|
49 | title = _('Admit customer'), |
---|
50 | msg = _('Admitted'), |
---|
51 | source = CREATED, |
---|
52 | destination = ADMITTED), |
---|
53 | |
---|
54 | Transition( |
---|
55 | transition_id = 'reset1', |
---|
56 | title = _('Reset customer'), |
---|
57 | msg = _('Reset to initial state'), |
---|
58 | source = ADMITTED, |
---|
59 | destination = CREATED), |
---|
60 | |
---|
61 | Transition( |
---|
62 | transition_id = 'start_clearance', |
---|
63 | title = _('Start clearance'), |
---|
64 | msg = _('Clearance started'), |
---|
65 | source = ADMITTED, |
---|
66 | destination = CLEARANCE), |
---|
67 | |
---|
68 | Transition( |
---|
69 | transition_id = 'reset2', |
---|
70 | title = _('Reset to admitted'), |
---|
71 | msg = _("Reset to 'admitted'"), |
---|
72 | source = CLEARANCE, |
---|
73 | destination = ADMITTED), |
---|
74 | |
---|
75 | Transition( |
---|
76 | transition_id = 'request_clearance', |
---|
77 | title = _('Request clearance'), |
---|
78 | msg = _('Clearance requested'), |
---|
79 | source = CLEARANCE, |
---|
80 | destination = REQUESTED), |
---|
81 | |
---|
82 | Transition( |
---|
83 | transition_id = 'reset3', |
---|
84 | title = _('Reset to clearance started'), |
---|
85 | msg = _("Reset to 'clearance started'"), |
---|
86 | source = REQUESTED, |
---|
87 | destination = CLEARANCE), |
---|
88 | |
---|
89 | Transition( |
---|
90 | transition_id = 'clear', |
---|
91 | title = _('Clear customer'), |
---|
92 | msg = _('Cleared'), |
---|
93 | source = REQUESTED, |
---|
94 | destination = CLEARED), |
---|
95 | |
---|
96 | Transition( |
---|
97 | transition_id = 'reset4', |
---|
98 | title = _('Reset to clearance started'), |
---|
99 | msg = _("Reset to 'clearance started'"), |
---|
100 | source = CLEARED, |
---|
101 | destination = CLEARANCE), |
---|
102 | |
---|
103 | Transition( |
---|
104 | transition_id = 'pay_first_school_fee', |
---|
105 | title = _('Pay school fee'), |
---|
106 | msg = _('First school fee payment made'), |
---|
107 | source = CLEARED, |
---|
108 | destination = PAID), |
---|
109 | |
---|
110 | Transition( |
---|
111 | transition_id = 'approve_first_school_fee', |
---|
112 | title = _('Approve payment'), |
---|
113 | msg = _('First school fee payment approved'), |
---|
114 | source = CLEARED, |
---|
115 | destination = PAID), |
---|
116 | |
---|
117 | Transition( |
---|
118 | transition_id = 'reset5', |
---|
119 | title = _('Reset to cleared'), |
---|
120 | msg = _("Reset to 'cleared'"), |
---|
121 | source = PAID, |
---|
122 | destination = CLEARED), |
---|
123 | |
---|
124 | Transition( |
---|
125 | transition_id = 'pay_school_fee', |
---|
126 | title = _('Pay school fee'), |
---|
127 | msg = _('School fee payment made'), |
---|
128 | source = RETURNING, |
---|
129 | destination = PAID), |
---|
130 | |
---|
131 | Transition( |
---|
132 | transition_id = 'pay_pg_fee', |
---|
133 | title = _('Pay PG school fee'), |
---|
134 | msg = _('PG school fee payment made'), |
---|
135 | source = PAID, |
---|
136 | destination = PAID), |
---|
137 | |
---|
138 | Transition( |
---|
139 | transition_id = 'approve_school_fee', |
---|
140 | title = _('Approve school fee payment'), |
---|
141 | msg = _('School fee payment approved'), |
---|
142 | source = RETURNING, |
---|
143 | destination = PAID), |
---|
144 | |
---|
145 | Transition( |
---|
146 | transition_id = 'approve_pg_fee', |
---|
147 | title = _('Approve PG school fee payment'), |
---|
148 | msg = _('PG school fee payment approved'), |
---|
149 | source = PAID, |
---|
150 | destination = PAID), |
---|
151 | |
---|
152 | Transition( |
---|
153 | transition_id = 'reset6', |
---|
154 | title = _('Reset to returning'), |
---|
155 | msg = _("Reset to 'returning'"), |
---|
156 | source = PAID, |
---|
157 | destination = RETURNING), |
---|
158 | |
---|
159 | Transition( |
---|
160 | transition_id = 'register_courses', |
---|
161 | title = _('Register courses'), |
---|
162 | msg = _('Courses registered'), |
---|
163 | source = PAID, |
---|
164 | destination = REGISTERED), |
---|
165 | |
---|
166 | Transition( |
---|
167 | transition_id = 'reset7', |
---|
168 | title = _('Reset to school fee paid'), |
---|
169 | msg = _("Reset to 'school fee paid'"), |
---|
170 | source = REGISTERED, |
---|
171 | destination = PAID), |
---|
172 | |
---|
173 | Transition( |
---|
174 | transition_id = 'validate_courses', |
---|
175 | title = _('Validate courses'), |
---|
176 | msg = _('Courses validated'), |
---|
177 | source = REGISTERED, |
---|
178 | destination = VALIDATED), |
---|
179 | |
---|
180 | Transition( |
---|
181 | transition_id = 'bypass_validation', |
---|
182 | title = _('Return and bypass validation'), |
---|
183 | msg = _("Course validation bypassed"), |
---|
184 | source = REGISTERED, |
---|
185 | destination = RETURNING), |
---|
186 | |
---|
187 | Transition( |
---|
188 | transition_id = 'reset8', |
---|
189 | title = _('Reset to school fee paid'), |
---|
190 | msg = _("Reset to 'school fee paid'"), |
---|
191 | source = VALIDATED, |
---|
192 | destination = PAID), |
---|
193 | |
---|
194 | Transition( |
---|
195 | transition_id = 'return', |
---|
196 | title = _('Return'), |
---|
197 | msg = _("Returned"), |
---|
198 | source = VALIDATED, |
---|
199 | destination = RETURNING), |
---|
200 | |
---|
201 | Transition( |
---|
202 | transition_id = 'reset9', |
---|
203 | title = _('Reset to courses validated'), |
---|
204 | msg = _("Reset to 'courses validated'"), |
---|
205 | source = RETURNING, |
---|
206 | destination = VALIDATED), |
---|
207 | ) |
---|
208 | |
---|
209 | |
---|
210 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
211 | |
---|
212 | registration_workflow = IkobaWorkflow(REGISTRATION_TRANSITIONS) |
---|
213 | |
---|
214 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
215 | """An adapter to adapt Customer objects to workflow states. |
---|
216 | """ |
---|
217 | grok.context(ICustomer) |
---|
218 | grok.provides(IWorkflowState) |
---|
219 | |
---|
220 | state_key = 'wf.registration.state' |
---|
221 | state_id = 'wf.registration.id' |
---|
222 | |
---|
223 | class RegistrationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
224 | """Adapter to adapt Customer objects to workflow info objects. |
---|
225 | """ |
---|
226 | grok.context(ICustomer) |
---|
227 | grok.provides(IIkobaWorkflowInfo) |
---|
228 | |
---|
229 | def __init__(self, context): |
---|
230 | self.context = context |
---|
231 | self.wf = registration_workflow |
---|
232 | |
---|
233 | @grok.subscribe(ICustomer, IWorkflowTransitionEvent) |
---|
234 | def handle_customer_transition_event(obj, event): |
---|
235 | """Append message to customer history and log file when transition happened. |
---|
236 | |
---|
237 | Lock and unlock clearance form. |
---|
238 | Trigger actions after school fee payment. |
---|
239 | """ |
---|
240 | |
---|
241 | msg = event.transition.user_data['msg'] |
---|
242 | history = IObjectHistory(obj) |
---|
243 | history.addMessage(msg) |
---|
244 | try: |
---|
245 | customers_container = grok.getSite()['customers'] |
---|
246 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
247 | except (TypeError, AttributeError): |
---|
248 | pass |
---|
249 | return |
---|