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