[12089] | 1 | ## $Id: workflow.py 12097 2014-11-30 20:49:22Z henrik $ |
---|
[11958] | 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, |
---|
[12089] | 27 | STARTED, CREATED, REQUESTED, APPROVED, |
---|
| 28 | SUBMITTED, VERIFIED, REJECTED, EXPIRED) |
---|
[11958] | 29 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 30 | from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo |
---|
[12089] | 31 | from waeup.ikoba.customers.interfaces import ( |
---|
| 32 | ICustomer, ICustomersUtils, |
---|
[12097] | 33 | IContract) |
---|
[11958] | 34 | from waeup.ikoba.utils.helpers import get_current_principal |
---|
[12089] | 35 | from waeup.ikoba.documents.workflow import VERIFICATION_TRANSITIONS |
---|
[11958] | 36 | |
---|
[12089] | 37 | # Customer workflow |
---|
[11958] | 38 | |
---|
[12089] | 39 | IMPORTABLE_REGISTRATION_STATES = (STARTED, REQUESTED, APPROVED) |
---|
[11958] | 40 | |
---|
| 41 | REGISTRATION_TRANSITIONS = ( |
---|
| 42 | Transition( |
---|
| 43 | transition_id = 'create', |
---|
| 44 | title = _('Create customer'), |
---|
| 45 | source = None, |
---|
| 46 | condition = NullCondition, |
---|
[11964] | 47 | msg = _('Customer record created'), |
---|
[11958] | 48 | destination = CREATED), |
---|
| 49 | |
---|
| 50 | Transition( |
---|
[11967] | 51 | transition_id = 'start', |
---|
| 52 | title = _('Start registration'), |
---|
| 53 | source = CREATED, |
---|
| 54 | condition = NullCondition, |
---|
| 55 | msg = _('Customer registration started'), |
---|
| 56 | destination = STARTED), |
---|
| 57 | |
---|
| 58 | Transition( |
---|
[11964] | 59 | transition_id = 'request', |
---|
| 60 | title = _('Request registration'), |
---|
| 61 | msg = _('Customer registration requested'), |
---|
[11967] | 62 | source = STARTED, |
---|
| 63 | destination = REQUESTED), |
---|
[11958] | 64 | |
---|
| 65 | Transition( |
---|
[11964] | 66 | transition_id = 'approve', |
---|
| 67 | title = _('Approve customer'), |
---|
| 68 | msg = _('Customer registration approved'), |
---|
| 69 | source = REQUESTED, |
---|
| 70 | destination = APPROVED), |
---|
| 71 | |
---|
| 72 | Transition( |
---|
[11971] | 73 | transition_id = 'reject', |
---|
| 74 | title = _('Reject customer'), |
---|
| 75 | msg = _('Customer registration rejected'), |
---|
| 76 | source = REQUESTED, |
---|
| 77 | destination = STARTED), |
---|
| 78 | |
---|
| 79 | Transition( |
---|
[11958] | 80 | transition_id = 'reset1', |
---|
| 81 | title = _('Reset customer'), |
---|
[11964] | 82 | msg = _('Reset to initial customer state'), |
---|
| 83 | source = APPROVED, |
---|
[11967] | 84 | destination = STARTED), |
---|
[11958] | 85 | |
---|
| 86 | Transition( |
---|
| 87 | transition_id = 'reset2', |
---|
[11964] | 88 | title = _('Reset to requested'), |
---|
| 89 | msg = _("Reset to 'requested'"), |
---|
| 90 | source = APPROVED, |
---|
[11958] | 91 | destination = REQUESTED), |
---|
| 92 | |
---|
| 93 | Transition( |
---|
| 94 | transition_id = 'reset3', |
---|
[11964] | 95 | title = _('Reset customer'), |
---|
| 96 | msg = _("Reset to initial state"), |
---|
[11958] | 97 | source = REQUESTED, |
---|
[11967] | 98 | destination = STARTED), |
---|
[11958] | 99 | |
---|
| 100 | ) |
---|
| 101 | |
---|
| 102 | |
---|
[12089] | 103 | IMPORTABLE_REGISTRATION_TRANSITIONS = [i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
[11958] | 104 | |
---|
| 105 | registration_workflow = IkobaWorkflow(REGISTRATION_TRANSITIONS) |
---|
| 106 | |
---|
[11985] | 107 | |
---|
[11958] | 108 | class RegistrationWorkflowState(WorkflowState, grok.Adapter): |
---|
| 109 | """An adapter to adapt Customer objects to workflow states. |
---|
| 110 | """ |
---|
| 111 | grok.context(ICustomer) |
---|
| 112 | grok.provides(IWorkflowState) |
---|
| 113 | |
---|
| 114 | state_key = 'wf.registration.state' |
---|
| 115 | state_id = 'wf.registration.id' |
---|
| 116 | |
---|
[11985] | 117 | |
---|
[11958] | 118 | class RegistrationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
| 119 | """Adapter to adapt Customer objects to workflow info objects. |
---|
| 120 | """ |
---|
| 121 | grok.context(ICustomer) |
---|
| 122 | grok.provides(IIkobaWorkflowInfo) |
---|
| 123 | |
---|
| 124 | def __init__(self, context): |
---|
| 125 | self.context = context |
---|
| 126 | self.wf = registration_workflow |
---|
| 127 | |
---|
[11985] | 128 | |
---|
[11958] | 129 | @grok.subscribe(ICustomer, IWorkflowTransitionEvent) |
---|
| 130 | def handle_customer_transition_event(obj, event): |
---|
| 131 | """Append message to customer history and log file when transition happened. |
---|
| 132 | """ |
---|
| 133 | |
---|
| 134 | msg = event.transition.user_data['msg'] |
---|
| 135 | history = IObjectHistory(obj) |
---|
| 136 | history.addMessage(msg) |
---|
| 137 | try: |
---|
| 138 | customers_container = grok.getSite()['customers'] |
---|
| 139 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
| 140 | except (TypeError, AttributeError): |
---|
| 141 | pass |
---|
| 142 | return |
---|
[12089] | 143 | |
---|
[12097] | 144 | # Contract workflow (the same as verification workflow) |
---|
[12089] | 145 | |
---|
[12097] | 146 | IMPORTABLE_CONTRACT_STATES = (CREATED, SUBMITTED, APPROVED, REJECTED, EXPIRED) |
---|
[12089] | 147 | |
---|
[12097] | 148 | CONTRACT_TRANSITIONS = ( |
---|
[12089] | 149 | Transition( |
---|
| 150 | transition_id = 'create', |
---|
[12097] | 151 | title = _('Create contract record'), |
---|
[12089] | 152 | source = None, |
---|
| 153 | condition = NullCondition, |
---|
[12097] | 154 | msg = _('Contract record created'), |
---|
[12089] | 155 | destination = CREATED), |
---|
| 156 | |
---|
| 157 | Transition( |
---|
| 158 | transition_id = 'submit', |
---|
| 159 | title = _('Submit for approval'), |
---|
| 160 | msg = _('Submitted for approval'), |
---|
| 161 | source = CREATED, |
---|
| 162 | destination = SUBMITTED), |
---|
| 163 | |
---|
| 164 | Transition( |
---|
| 165 | transition_id = 'approve', |
---|
| 166 | title = _('Approve'), |
---|
| 167 | msg = _('Approved'), |
---|
| 168 | source = SUBMITTED, |
---|
| 169 | destination = APPROVED), |
---|
| 170 | |
---|
| 171 | Transition( |
---|
| 172 | transition_id = 'reject', |
---|
| 173 | title = _('Reject'), |
---|
| 174 | msg = _('REJECTED'), |
---|
| 175 | source = SUBMITTED, |
---|
| 176 | destination = REJECTED), |
---|
| 177 | |
---|
| 178 | Transition( |
---|
| 179 | transition_id = 'reset1', |
---|
| 180 | title = _('Reset to initial state'), |
---|
| 181 | msg = _('Reset to initial state'), |
---|
| 182 | source = REJECTED, |
---|
| 183 | destination = CREATED), |
---|
| 184 | |
---|
| 185 | Transition( |
---|
| 186 | transition_id = 'reset2', |
---|
| 187 | title = _('Reset to initial state'), |
---|
| 188 | msg = _('Reset to initial state'), |
---|
| 189 | source = APPROVED, |
---|
| 190 | destination = CREATED), |
---|
| 191 | |
---|
| 192 | Transition( |
---|
| 193 | transition_id = 'reset3', |
---|
| 194 | title = _('Reset to initial state'), |
---|
| 195 | msg = _('Reset to initial state'), |
---|
| 196 | source = SUBMITTED, |
---|
| 197 | destination = CREATED), |
---|
| 198 | |
---|
| 199 | Transition( |
---|
| 200 | transition_id = 'expire', |
---|
| 201 | title = _('Set to expired'), |
---|
| 202 | msg = _('Set to expired'), |
---|
| 203 | source = APPROVED, |
---|
| 204 | destination = EXPIRED), |
---|
| 205 | |
---|
| 206 | Transition( |
---|
| 207 | transition_id = 'reset4', |
---|
| 208 | title = _('Reset to initial state'), |
---|
| 209 | msg = _('Reset to initial state'), |
---|
| 210 | source = EXPIRED, |
---|
| 211 | destination = CREATED), |
---|
| 212 | ) |
---|
| 213 | |
---|
| 214 | |
---|
[12097] | 215 | IMPORTABLE_CONTRACT_TRANSITIONS = [ |
---|
[12089] | 216 | i.transition_id for i in REGISTRATION_TRANSITIONS] |
---|
| 217 | |
---|
[12097] | 218 | contract_workflow = IkobaWorkflow(CONTRACT_TRANSITIONS) |
---|
[12089] | 219 | |
---|
[12097] | 220 | class ContractWorkflowState(WorkflowState, grok.Adapter): |
---|
| 221 | """An adapter to adapt Contract objects to workflow states. |
---|
[12089] | 222 | """ |
---|
[12097] | 223 | grok.context(IContract) |
---|
[12089] | 224 | grok.provides(IWorkflowState) |
---|
| 225 | |
---|
[12097] | 226 | state_key = 'wf.contract.state' |
---|
| 227 | state_id = 'wf.contract.id' |
---|
[12089] | 228 | |
---|
[12097] | 229 | class ContractWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
| 230 | """Adapter to adapt Contract objects to workflow info objects. |
---|
[12089] | 231 | """ |
---|
[12097] | 232 | grok.context(IContract) |
---|
[12089] | 233 | grok.provides(IIkobaWorkflowInfo) |
---|
| 234 | |
---|
| 235 | def __init__(self, context): |
---|
| 236 | self.context = context |
---|
[12097] | 237 | self.wf = contract_workflow |
---|
[12089] | 238 | |
---|
[12097] | 239 | @grok.subscribe(IContract, IWorkflowTransitionEvent) |
---|
[12089] | 240 | def handle_document_transition_event(obj, event): |
---|
[12097] | 241 | """Append message to contract history and log file and update |
---|
[12089] | 242 | last_transition_date when transition happened. |
---|
| 243 | """ |
---|
| 244 | msg = event.transition.user_data['msg'] |
---|
| 245 | history = IObjectHistory(obj) |
---|
| 246 | history.addMessage(msg) |
---|
| 247 | obj.last_transition_date = datetime.utcnow() |
---|
| 248 | try: |
---|
| 249 | customers_container = grok.getSite()['customers'] |
---|
| 250 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
| 251 | except (TypeError, AttributeError): |
---|
| 252 | pass |
---|
| 253 | return |
---|