1 | ## $Id: workflow.py 11997 2014-11-19 17:02:48Z 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 = 'reject', |
---|
69 | title = _('Reject customer'), |
---|
70 | msg = _('Customer registration rejected'), |
---|
71 | source = REQUESTED, |
---|
72 | destination = STARTED), |
---|
73 | |
---|
74 | Transition( |
---|
75 | transition_id = 'reset1', |
---|
76 | title = _('Reset customer'), |
---|
77 | msg = _('Reset to initial customer state'), |
---|
78 | source = APPROVED, |
---|
79 | destination = STARTED), |
---|
80 | |
---|
81 | Transition( |
---|
82 | transition_id = 'reset2', |
---|
83 | title = _('Reset to requested'), |
---|
84 | msg = _("Reset to 'requested'"), |
---|
85 | source = APPROVED, |
---|
86 | destination = REQUESTED), |
---|
87 | |
---|
88 | Transition( |
---|
89 | transition_id = 'reset3', |
---|
90 | title = _('Reset customer'), |
---|
91 | msg = _("Reset to initial state"), |
---|
92 | source = REQUESTED, |
---|
93 | destination = STARTED), |
---|
94 | |
---|
95 | ) |
---|
96 | |
---|
97 | |
---|
98 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
99 | |
---|
100 | registration_workflow = IkobaWorkflow(REGISTRATION_TRANSITIONS) |
---|
101 | |
---|
102 | |
---|
103 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
104 | """An adapter to adapt Customer objects to workflow states. |
---|
105 | """ |
---|
106 | grok.context(ICustomer) |
---|
107 | grok.provides(IWorkflowState) |
---|
108 | |
---|
109 | state_key = 'wf.registration.state' |
---|
110 | state_id = 'wf.registration.id' |
---|
111 | |
---|
112 | |
---|
113 | class RegistrationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
114 | """Adapter to adapt Customer objects to workflow info objects. |
---|
115 | """ |
---|
116 | grok.context(ICustomer) |
---|
117 | grok.provides(IIkobaWorkflowInfo) |
---|
118 | |
---|
119 | def __init__(self, context): |
---|
120 | self.context = context |
---|
121 | self.wf = registration_workflow |
---|
122 | |
---|
123 | |
---|
124 | @grok.subscribe(ICustomer, IWorkflowTransitionEvent) |
---|
125 | def handle_customer_transition_event(obj, event): |
---|
126 | """Append message to customer history and log file when transition happened. |
---|
127 | """ |
---|
128 | |
---|
129 | msg = event.transition.user_data['msg'] |
---|
130 | history = IObjectHistory(obj) |
---|
131 | history.addMessage(msg) |
---|
132 | try: |
---|
133 | customers_container = grok.getSite()['customers'] |
---|
134 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
135 | except (TypeError, AttributeError): |
---|
136 | pass |
---|
137 | return |
---|