[12015] | 1 | ## $Id: workflow.py 12089 2014-11-28 21:35:09Z henrik $ |
---|
[11982] | 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 documents. |
---|
| 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 ( |
---|
[12032] | 26 | IObjectHistory, IIkobaWorkflowInfo, |
---|
[12089] | 27 | SimpleIkobaVocabulary, |
---|
| 28 | CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED) |
---|
[11982] | 29 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 30 | from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo |
---|
| 31 | from waeup.ikoba.utils.helpers import get_current_principal |
---|
| 32 | from waeup.ikoba.documents.interfaces import IDocument |
---|
| 33 | |
---|
[12089] | 34 | IMPORTABLE_STATES = (CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED) |
---|
[11982] | 35 | |
---|
[12089] | 36 | VERIFICATION_TRANSITIONS = ( |
---|
[11982] | 37 | Transition( |
---|
| 38 | transition_id = 'create', |
---|
| 39 | title = _('Create document'), |
---|
| 40 | source = None, |
---|
| 41 | condition = NullCondition, |
---|
| 42 | msg = _('Document created'), |
---|
| 43 | destination = CREATED), |
---|
| 44 | |
---|
| 45 | Transition( |
---|
| 46 | transition_id = 'submit', |
---|
| 47 | title = _('Submit for verification'), |
---|
| 48 | msg = _('Submitted for verification'), |
---|
| 49 | source = CREATED, |
---|
| 50 | destination = SUBMITTED), |
---|
| 51 | |
---|
| 52 | Transition( |
---|
| 53 | transition_id = 'verify', |
---|
| 54 | title = _('Verify'), |
---|
| 55 | msg = _('Verified'), |
---|
| 56 | source = SUBMITTED, |
---|
| 57 | destination = VERIFIED), |
---|
| 58 | |
---|
| 59 | Transition( |
---|
| 60 | transition_id = 'reject', |
---|
| 61 | title = _('Reject'), |
---|
| 62 | msg = _('REJECTED'), |
---|
| 63 | source = SUBMITTED, |
---|
| 64 | destination = REJECTED), |
---|
| 65 | |
---|
| 66 | Transition( |
---|
| 67 | transition_id = 'reset1', |
---|
[12089] | 68 | title = _('Reset to initial state'), |
---|
[11982] | 69 | msg = _('Reset to initial state'), |
---|
| 70 | source = REJECTED, |
---|
| 71 | destination = CREATED), |
---|
| 72 | |
---|
| 73 | Transition( |
---|
| 74 | transition_id = 'reset2', |
---|
[12089] | 75 | title = _('Reset to initial state'), |
---|
[11982] | 76 | msg = _('Reset to initial state'), |
---|
| 77 | source = VERIFIED, |
---|
| 78 | destination = CREATED), |
---|
| 79 | |
---|
| 80 | Transition( |
---|
| 81 | transition_id = 'reset3', |
---|
[12089] | 82 | title = _('Reset to initial state'), |
---|
[11982] | 83 | msg = _('Reset to initial state'), |
---|
| 84 | source = SUBMITTED, |
---|
| 85 | destination = CREATED), |
---|
[12015] | 86 | |
---|
| 87 | Transition( |
---|
[12089] | 88 | transition_id = 'expire', |
---|
| 89 | title = _('Set to expired'), |
---|
| 90 | msg = _('Set to expired'), |
---|
[12015] | 91 | source = VERIFIED, |
---|
[12089] | 92 | destination = EXPIRED), |
---|
| 93 | |
---|
| 94 | Transition( |
---|
| 95 | transition_id = 'reset4', |
---|
| 96 | title = _('Reset to initial state'), |
---|
| 97 | msg = _('Reset to initial state'), |
---|
| 98 | source = EXPIRED, |
---|
| 99 | destination = CREATED), |
---|
[11982] | 100 | ) |
---|
| 101 | |
---|
| 102 | |
---|
[12089] | 103 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in VERIFICATION_TRANSITIONS] |
---|
[11982] | 104 | |
---|
[12089] | 105 | verification_workflow = IkobaWorkflow(VERIFICATION_TRANSITIONS) |
---|
[11982] | 106 | |
---|
| 107 | class VerificationWorkflowState(WorkflowState, grok.Adapter): |
---|
| 108 | """An adapter to adapt Document objects to workflow states. |
---|
| 109 | """ |
---|
| 110 | grok.context(IDocument) |
---|
| 111 | grok.provides(IWorkflowState) |
---|
| 112 | |
---|
| 113 | state_key = 'wf.verification.state' |
---|
| 114 | state_id = 'wf.verification.id' |
---|
| 115 | |
---|
| 116 | class VerificationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
| 117 | """Adapter to adapt Document objects to workflow info objects. |
---|
| 118 | """ |
---|
| 119 | grok.context(IDocument) |
---|
| 120 | grok.provides(IIkobaWorkflowInfo) |
---|
| 121 | |
---|
| 122 | def __init__(self, context): |
---|
| 123 | self.context = context |
---|
| 124 | self.wf = verification_workflow |
---|
| 125 | |
---|
| 126 | @grok.subscribe(IDocument, IWorkflowTransitionEvent) |
---|
| 127 | def handle_document_transition_event(obj, event): |
---|
[12053] | 128 | """Append message to document history and log file and update |
---|
| 129 | last_transition_date when transition happened. |
---|
[11982] | 130 | """ |
---|
| 131 | msg = event.transition.user_data['msg'] |
---|
| 132 | history = IObjectHistory(obj) |
---|
| 133 | history.addMessage(msg) |
---|
[12053] | 134 | obj.last_transition_date = datetime.utcnow() |
---|
[11982] | 135 | try: |
---|
| 136 | customers_container = grok.getSite()['customers'] |
---|
| 137 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
| 138 | except (TypeError, AttributeError): |
---|
| 139 | pass |
---|
| 140 | return |
---|