1 | ## $Id: workflow.py 12162 2014-12-07 15:13:26Z 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, |
---|
27 | SimpleIkobaVocabulary, |
---|
28 | CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED) |
---|
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 | |
---|
34 | IMPORTABLE_STATES = (CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED) |
---|
35 | |
---|
36 | VERIFICATION_TRANSITIONS = ( |
---|
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', |
---|
68 | title = _('Reset to initial state'), |
---|
69 | msg = _('Reset to initial state'), |
---|
70 | source = REJECTED, |
---|
71 | destination = CREATED), |
---|
72 | |
---|
73 | Transition( |
---|
74 | transition_id = 'reset2', |
---|
75 | title = _('Reset to initial state'), |
---|
76 | msg = _('Reset to initial state'), |
---|
77 | source = VERIFIED, |
---|
78 | destination = CREATED), |
---|
79 | |
---|
80 | Transition( |
---|
81 | transition_id = 'reset3', |
---|
82 | title = _('Reset to initial state'), |
---|
83 | msg = _('Reset to initial state'), |
---|
84 | source = SUBMITTED, |
---|
85 | destination = CREATED), |
---|
86 | |
---|
87 | Transition( |
---|
88 | transition_id = 'expire', |
---|
89 | title = _('Set to expired'), |
---|
90 | msg = _('Set to expired'), |
---|
91 | source = VERIFIED, |
---|
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), |
---|
100 | ) |
---|
101 | |
---|
102 | |
---|
103 | IMPORTABLE_TRANSITIONS = [i.transition_id for i in VERIFICATION_TRANSITIONS] |
---|
104 | |
---|
105 | verification_workflow = IkobaWorkflow(VERIFICATION_TRANSITIONS) |
---|
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): |
---|
128 | """Append message to document history and log file and update |
---|
129 | last_transition_date when transition happened. |
---|
130 | """ |
---|
131 | if event.transition.transition_id == 'verify': |
---|
132 | obj.setMD5() |
---|
133 | msg = event.transition.user_data['msg'] |
---|
134 | history = IObjectHistory(obj) |
---|
135 | history.addMessage(msg) |
---|
136 | obj.last_transition_date = datetime.utcnow() |
---|
137 | try: |
---|
138 | customers_container = grok.getSite()['customers'] |
---|
139 | customers_container.logger.info('%s - %s' % (obj.customer_id,msg)) |
---|
140 | except (TypeError, AttributeError): |
---|
141 | pass |
---|
142 | return |
---|