[12015] | 1 | ## $Id: workflow.py 12222 2014-12-14 06:20:40Z 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 zope.component import getUtility |
---|
| 22 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
[12168] | 23 | from hurry.workflow.interfaces import ( |
---|
| 24 | IWorkflowState, IWorkflowTransitionEvent, InvalidTransitionError) |
---|
[11982] | 25 | from waeup.ikoba.interfaces import ( |
---|
[12032] | 26 | IObjectHistory, IIkobaWorkflowInfo, |
---|
[12089] | 27 | SimpleIkobaVocabulary, |
---|
[12213] | 28 | CREATED, PUBLISHED) |
---|
[11982] | 29 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 30 | from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo |
---|
[12213] | 31 | from waeup.ikoba.documents.interfaces import IPublicDocument |
---|
[11982] | 32 | |
---|
[12213] | 33 | |
---|
| 34 | PUBLISHING_TRANSITIONS = ( |
---|
[11982] | 35 | Transition( |
---|
| 36 | transition_id = 'create', |
---|
| 37 | title = _('Create document'), |
---|
| 38 | source = None, |
---|
| 39 | condition = NullCondition, |
---|
| 40 | msg = _('Document created'), |
---|
| 41 | destination = CREATED), |
---|
[12215] | 42 | |
---|
| 43 | Transition( |
---|
| 44 | transition_id = 'publish', |
---|
| 45 | title = _('Publish document'), |
---|
| 46 | source = CREATED, |
---|
| 47 | condition = NullCondition, |
---|
| 48 | msg = _('Document published'), |
---|
| 49 | destination = PUBLISHED), |
---|
[11982] | 50 | ) |
---|
| 51 | |
---|
[12213] | 52 | publishing_workflow = IkobaWorkflow(PUBLISHING_TRANSITIONS) |
---|
[11982] | 53 | |
---|
[12213] | 54 | class PublishingWorkflowState(WorkflowState, grok.Adapter): |
---|
[11982] | 55 | """An adapter to adapt Document objects to workflow states. |
---|
| 56 | """ |
---|
[12213] | 57 | grok.context(IPublicDocument) |
---|
[11982] | 58 | grok.provides(IWorkflowState) |
---|
| 59 | |
---|
[12213] | 60 | state_key = 'wf.publishing.state' |
---|
| 61 | state_id = 'wf.publishing.id' |
---|
[11982] | 62 | |
---|
[12213] | 63 | |
---|
| 64 | class PublishingWorkflowInfo(IkobaWorkflowInfo, grok.Adapter): |
---|
| 65 | """Adapter to adapt CustomerDocument objects to workflow info objects. |
---|
[11982] | 66 | """ |
---|
[12213] | 67 | grok.context(IPublicDocument) |
---|
[11982] | 68 | grok.provides(IIkobaWorkflowInfo) |
---|
| 69 | |
---|
| 70 | def __init__(self, context): |
---|
| 71 | self.context = context |
---|
[12213] | 72 | self.wf = publishing_workflow |
---|
[11982] | 73 | |
---|
[12213] | 74 | |
---|
| 75 | @grok.subscribe(IPublicDocument, IWorkflowTransitionEvent) |
---|
| 76 | def handle_public_document_transition_event(obj, event): |
---|
[12210] | 77 | """Append message to document history and log file. |
---|
[12168] | 78 | |
---|
[11982] | 79 | """ |
---|
| 80 | msg = event.transition.user_data['msg'] |
---|
| 81 | history = IObjectHistory(obj) |
---|
| 82 | history.addMessage(msg) |
---|
| 83 | try: |
---|
[12222] | 84 | grok.getSite().logger.info('%s - %s' % (obj.document_id, msg)) |
---|
[11982] | 85 | except (TypeError, AttributeError): |
---|
| 86 | pass |
---|
[12213] | 87 | return |
---|