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 | STARTED, CREATED, REQUESTED, APPROVED) |
---|
28 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
29 | from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo |
---|
30 | from waeup.ikoba.customers.interfaces import ICustomer, ICustomersUtils |
---|
31 | from waeup.ikoba.utils.helpers import get_current_principal |
---|
32 | |
---|
33 | |
---|
34 | IMPORTABLE_STATES = (STARTED, REQUESTED, APPROVED) |
---|
35 | |
---|
36 | REGISTRATION_TRANSITIONS = ( |
---|
37 | Transition( |
---|
38 | transition_id = 'create', |
---|
39 | title = _('Create customer'), |
---|
40 | source = None, |
---|
41 | condition = NullCondition, |
---|
42 | msg = _('Customer record created'), |
---|
43 | destination = CREATED), |
---|
44 | |
---|
45 | Transition( |
---|
46 | transition_id = 'start', |
---|
47 | title = _('Start registration'), |
---|
48 | source = CREATED, |
---|
49 | condition = NullCondition, |
---|
50 | msg = _('Customer registration started'), |
---|
51 | destination = STARTED), |
---|
52 | |
---|
53 | Transition( |
---|
54 | transition_id = 'request', |
---|
55 | title = _('Request registration'), |
---|
56 | msg = _('Customer registration requested'), |
---|
57 | source = STARTED, |
---|
58 | destination = REQUESTED), |
---|
59 | |
---|
60 | Transition( |
---|
61 | transition_id = 'approve', |
---|
62 | title = _('Approve customer'), |
---|
63 | msg = _('Customer registration approved'), |
---|
64 | source = REQUESTED, |
---|
65 | destination = APPROVED), |
---|
66 | |
---|
67 | Transition( |
---|
68 | transition_id = 'reset1', |
---|
69 | title = _('Reset customer'), |
---|
70 | msg = _('Reset to initial customer state'), |
---|
71 | source = APPROVED, |
---|
72 | destination = STARTED), |
---|
73 | |
---|
74 | Transition( |
---|
75 | transition_id = 'reset2', |
---|
76 | title = _('Reset to requested'), |
---|
77 | msg = _("Reset to 'requested'"), |
---|
78 | source = APPROVED, |
---|
79 | destination = REQUESTED), |
---|
80 | |
---|
81 | Transition( |
---|
82 | transition_id = 'reset3', |
---|
83 | title = _('Reset customer'), |
---|
84 | msg = _("Reset to initial state"), |
---|
85 | source = REQUESTED, |
---|
86 | destination = STARTED), |
---|
87 | |
---|
88 | ) |
---|
89 | |
---|
90 | |
---|
91 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
92 | |
---|
93 | registration_workflow = IkobaWorkflow(REGISTRATION_TRANSITIONS) |
---|
94 | |
---|
95 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
96 | """An adapter to adapt Customer objects to workflow states. |
---|
97 | """ |
---|
98 | grok.context(ICustomer) |
---|
99 | grok.provides(IWorkflowState) |
---|
100 | |
---|
101 | state_key = 'wf.registration.state' |
---|
102 | state_id = 'wf.registration.id' |
---|
103 | |
---|
104 | class RegistrationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
105 | """Adapter to adapt Customer objects to workflow info objects. |
---|
106 | """ |
---|
107 | grok.context(ICustomer) |
---|
108 | grok.provides(IIkobaWorkflowInfo) |
---|
109 | |
---|
110 | def __init__(self, context): |
---|
111 | self.context = context |
---|
112 | self.wf = registration_workflow |
---|
113 | |
---|
114 | @grok.subscribe(ICustomer, IWorkflowTransitionEvent) |
---|
115 | def handle_customer_transition_event(obj, event): |
---|
116 | """Append message to customer history and log file when transition happened. |
---|
117 | |
---|
118 | Lock and unlock clearance form. |
---|
119 | Trigger actions after school fee payment. |
---|
120 | """ |
---|
121 | |
---|
122 | msg = event.transition.user_data['msg'] |
---|
123 | history = IObjectHistory(obj) |
---|
124 | history.addMessage(msg) |
---|
125 | try: |
---|
126 | customers_container = grok.getSite()['customers'] |
---|
127 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
128 | except (TypeError, AttributeError): |
---|
129 | pass |
---|
130 | return |
---|