1 | ## $Id: workflow.py 7687 2012-02-23 10:18:10Z 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 zope.component import getUtility |
---|
22 | from zope.i18n import translate |
---|
23 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
24 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
25 | from waeup.sirp.applicants.interfaces import IApplicantBaseData |
---|
26 | from waeup.sirp.interfaces import IObjectHistory, ISIRPWorkflowInfo, ISIRPUtils |
---|
27 | from waeup.sirp.interfaces import MessageFactory as _ |
---|
28 | from waeup.sirp.workflow import SIRPWorkflow, SIRPWorkflowInfo |
---|
29 | |
---|
30 | INITIALIZED = 'initialized' |
---|
31 | STARTED = 'started' |
---|
32 | PAID = 'paid' |
---|
33 | SUBMITTED = 'submitted' |
---|
34 | ADMITTED = 'admitted' |
---|
35 | NOT_ADMITTED = 'not admitted' |
---|
36 | CREATED = 'created' |
---|
37 | |
---|
38 | application_states_dict = { |
---|
39 | INITIALIZED: _('initialized'), |
---|
40 | STARTED: _('started'), |
---|
41 | PAID: _('paid'), |
---|
42 | SUBMITTED: _('submitted'), |
---|
43 | ADMITTED: _('admitted'), |
---|
44 | NOT_ADMITTED: _('not admitted'), |
---|
45 | CREATED: _('created'), |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | APPLICATION_TRANSITIONS = ( |
---|
50 | Transition( |
---|
51 | transition_id = 'init', |
---|
52 | title = _('Initialize application'), |
---|
53 | source = None, |
---|
54 | condition = NullCondition, |
---|
55 | msg = _('Application initialized'), |
---|
56 | destination = INITIALIZED), |
---|
57 | |
---|
58 | Transition( |
---|
59 | transition_id = 'start', |
---|
60 | title = _('Start application'), |
---|
61 | msg = _('Application started'), |
---|
62 | source = INITIALIZED, |
---|
63 | destination = STARTED), |
---|
64 | |
---|
65 | Transition( |
---|
66 | transition_id = 'pay', |
---|
67 | title = _('Pay acceptance fee'), |
---|
68 | msg = _('Fee paid'), |
---|
69 | source = STARTED, |
---|
70 | destination = PAID), |
---|
71 | |
---|
72 | Transition( |
---|
73 | transition_id = 'submit', |
---|
74 | title = _('Submit application'), |
---|
75 | msg = _('Application submitted'), |
---|
76 | source = PAID, |
---|
77 | destination = SUBMITTED), |
---|
78 | |
---|
79 | Transition( |
---|
80 | transition_id = 'admit', |
---|
81 | title = _('Admit applicant'), |
---|
82 | msg = _('Applicant admitted'), |
---|
83 | source = SUBMITTED, |
---|
84 | destination = ADMITTED), |
---|
85 | |
---|
86 | Transition( |
---|
87 | transition_id = 'refuse1', |
---|
88 | title = _('Refuse application'), |
---|
89 | msg = _('Application refused'), |
---|
90 | source = SUBMITTED, |
---|
91 | destination = NOT_ADMITTED), |
---|
92 | |
---|
93 | Transition( |
---|
94 | transition_id = 'refuse2', |
---|
95 | title = _('Refuse application'), |
---|
96 | msg = _('Application refused'), |
---|
97 | source = ADMITTED, |
---|
98 | destination = NOT_ADMITTED), |
---|
99 | |
---|
100 | Transition( |
---|
101 | transition_id = 'create', |
---|
102 | title = _('Create student record'), |
---|
103 | msg = _('Student record created'), |
---|
104 | source = ADMITTED, |
---|
105 | destination = CREATED), |
---|
106 | |
---|
107 | Transition( |
---|
108 | transition_id = 'reset1', |
---|
109 | title = _('Reset application to started'), |
---|
110 | msg = _('Application reset'), |
---|
111 | source = SUBMITTED, |
---|
112 | destination = STARTED), |
---|
113 | |
---|
114 | Transition( |
---|
115 | transition_id = 'reset2', |
---|
116 | title = _('Reset application to started'), |
---|
117 | msg = _('Application reset'), |
---|
118 | source = ADMITTED, |
---|
119 | destination = STARTED), |
---|
120 | |
---|
121 | Transition( |
---|
122 | transition_id = 'reset3', |
---|
123 | title = _('Reset application to started'), |
---|
124 | msg = _('Application reset'), |
---|
125 | source = NOT_ADMITTED, |
---|
126 | destination = STARTED), |
---|
127 | |
---|
128 | Transition( |
---|
129 | transition_id = 'reset4', |
---|
130 | title = _('Reset application to started'), |
---|
131 | msg = _('Application reset'), |
---|
132 | source = CREATED, |
---|
133 | destination = STARTED), |
---|
134 | Transition( |
---|
135 | transition_id = 'reset5', |
---|
136 | title = _('Reset application to paid'), |
---|
137 | msg = _('Application reset'), |
---|
138 | source = SUBMITTED, |
---|
139 | destination = PAID), |
---|
140 | ) |
---|
141 | |
---|
142 | application_workflow = SIRPWorkflow(APPLICATION_TRANSITIONS) |
---|
143 | |
---|
144 | class ApplicationWorkflowState(WorkflowState, grok.Adapter): |
---|
145 | """An adapter to adapt Applicant objects to workflow states. |
---|
146 | """ |
---|
147 | grok.context(IApplicantBaseData) |
---|
148 | grok.provides(IWorkflowState) |
---|
149 | |
---|
150 | state_key = 'wf.application.state' |
---|
151 | state_id = 'wf.application.id' |
---|
152 | |
---|
153 | class ApplicationWorkflowInfo(SIRPWorkflowInfo, grok.Adapter): |
---|
154 | """Adapter to adapt Applicant objects to workflow info objects. |
---|
155 | """ |
---|
156 | grok.context(IApplicantBaseData) |
---|
157 | grok.provides(ISIRPWorkflowInfo) |
---|
158 | |
---|
159 | def __init__(self, context): |
---|
160 | self.context = context |
---|
161 | self.wf = application_workflow |
---|
162 | |
---|
163 | @grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent) |
---|
164 | def handle_applicant_transition_event(obj, event): |
---|
165 | """Append message to applicant history when transition happened. |
---|
166 | """ |
---|
167 | msg = event.transition.user_data['msg'] |
---|
168 | portal_language = getUtility(ISIRPUtils).PORTAL_LANGUAGE |
---|
169 | if portal_language != 'en': |
---|
170 | msg = translate(msg,'waeup.sirp',target_language=portal_language) |
---|
171 | history = IObjectHistory(obj) |
---|
172 | history.addMessage(msg) |
---|
173 | # In some tests we don't have a an applicants root or a user |
---|
174 | try: |
---|
175 | applicants_root = grok.getSite()['applicants'] |
---|
176 | applicants_root.logger.info('%s - %s' % (obj.applicant_id,msg)) |
---|
177 | except (TypeError, AttributeError): |
---|
178 | pass |
---|
179 | return |
---|