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