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