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