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