source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/workflow.py @ 12192

Last change on this file since 12192 was 12168, checked in by Henrik Bettermann, 10 years ago

Documents can't be verified without file(s) attached.

Let is_approvable and is_verifiable be more verbose.

  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1## $Id: workflow.py 12168 2014-12-08 06:17:30Z 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"""
20import grok
21from datetime import datetime
22from zope.component import getUtility
23from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
24from hurry.workflow.interfaces import (
25    IWorkflowState, IWorkflowTransitionEvent, InvalidTransitionError)
26from waeup.ikoba.interfaces import (
27    IObjectHistory, IIkobaWorkflowInfo,
28    SimpleIkobaVocabulary,
29    CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED)
30from waeup.ikoba.interfaces import MessageFactory as _
31from waeup.ikoba.workflow import IkobaWorkflow, IkobaWorkflowInfo
32from waeup.ikoba.utils.helpers import get_current_principal
33from waeup.ikoba.documents.interfaces import IDocument
34
35IMPORTABLE_STATES = (CREATED, SUBMITTED, VERIFIED, REJECTED, EXPIRED)
36
37VERIFICATION_TRANSITIONS = (
38    Transition(
39        transition_id = 'create',
40        title = _('Create document'),
41        source = None,
42        condition = NullCondition,
43        msg = _('Document created'),
44        destination = CREATED),
45
46    Transition(
47        transition_id = 'submit',
48        title = _('Submit for verification'),
49        msg = _('Submitted for verification'),
50        source = CREATED,
51        destination = SUBMITTED),
52
53    Transition(
54        transition_id = 'verify',
55        title = _('Verify'),
56        msg = _('Verified'),
57        source = SUBMITTED,
58        destination = VERIFIED),
59
60    Transition(
61        transition_id = 'reject',
62        title = _('Reject'),
63        msg = _('REJECTED'),
64        source = SUBMITTED,
65        destination = REJECTED),
66
67    Transition(
68        transition_id = 'reset1',
69        title = _('Reset to initial state'),
70        msg = _('Reset to initial state'),
71        source = REJECTED,
72        destination = CREATED),
73
74    Transition(
75        transition_id = 'reset2',
76        title = _('Reset to initial state'),
77        msg = _('Reset to initial state'),
78        source = VERIFIED,
79        destination = CREATED),
80
81    Transition(
82        transition_id = 'reset3',
83        title = _('Reset to initial state'),
84        msg = _('Reset to initial state'),
85        source = SUBMITTED,
86        destination = CREATED),
87
88    Transition(
89        transition_id = 'expire',
90        title = _('Set to expired'),
91        msg = _('Set to expired'),
92        source = VERIFIED,
93        destination = EXPIRED),
94
95    Transition(
96        transition_id = 'reset4',
97        title = _('Reset to initial state'),
98        msg = _('Reset to initial state'),
99        source = EXPIRED,
100        destination = CREATED),
101    )
102
103
104IMPORTABLE_TRANSITIONS = [i.transition_id for i in VERIFICATION_TRANSITIONS]
105
106verification_workflow = IkobaWorkflow(VERIFICATION_TRANSITIONS)
107
108class VerificationWorkflowState(WorkflowState, grok.Adapter):
109    """An adapter to adapt Document objects to workflow states.
110    """
111    grok.context(IDocument)
112    grok.provides(IWorkflowState)
113
114    state_key = 'wf.verification.state'
115    state_id = 'wf.verification.id'
116
117class VerificationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter):
118    """Adapter to adapt Document objects to workflow info objects.
119    """
120    grok.context(IDocument)
121    grok.provides(IIkobaWorkflowInfo)
122
123    def __init__(self, context):
124        self.context = context
125        self.wf = verification_workflow
126
127@grok.subscribe(IDocument, IWorkflowTransitionEvent)
128def handle_document_transition_event(obj, event):
129    """Append message to document history and log file and update
130    last_transition_date when transition happened.
131
132    Undo the verification of document and raise an exception if document
133    does not meet the requirements for verification.
134    """
135    if event.transition.destination == VERIFIED:
136        verifiable, error = obj.is_verifiable
137        if not verifiable:
138            # Undo transition and raise an exception.
139            IWorkflowState(obj).setState(event.transition.source)
140            raise InvalidTransitionError(error)
141    if event.transition.transition_id == 'verify':
142        obj.setMD5()
143    msg = event.transition.user_data['msg']
144    history = IObjectHistory(obj)
145    history.addMessage(msg)
146    obj.last_transition_date = datetime.utcnow()
147    try:
148        customers_container = grok.getSite()['customers']
149        customers_container.logger.info('%s - %s' % (obj.customer_id,msg))
150    except (TypeError, AttributeError):
151        pass
152    return
Note: See TracBrowser for help on using the repository browser.