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

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

We need documents which can be accessed or downloaded from product pages. These documents can provide general information of registration processes or pdf forms to be filled offline and later uploaded by customers.

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1## $Id: workflow.py 12200 2014-12-12 15:34:50Z 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, PUBLISHED)
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    Transition(
103        transition_id = 'publish',
104        title = _('Publish'),
105        msg = _('Published'),
106        source = CREATED,
107        destination = PUBLISHED),
108    )
109
110
111IMPORTABLE_TRANSITIONS = [i.transition_id for i in VERIFICATION_TRANSITIONS]
112
113verification_workflow = IkobaWorkflow(VERIFICATION_TRANSITIONS)
114
115class VerificationWorkflowState(WorkflowState, grok.Adapter):
116    """An adapter to adapt Document objects to workflow states.
117    """
118    grok.context(IDocument)
119    grok.provides(IWorkflowState)
120
121    state_key = 'wf.verification.state'
122    state_id = 'wf.verification.id'
123
124class VerificationWorkflowInfo(IkobaWorkflowInfo, grok.Adapter):
125    """Adapter to adapt Document objects to workflow info objects.
126    """
127    grok.context(IDocument)
128    grok.provides(IIkobaWorkflowInfo)
129
130    def __init__(self, context):
131        self.context = context
132        self.wf = verification_workflow
133
134@grok.subscribe(IDocument, IWorkflowTransitionEvent)
135def handle_document_transition_event(obj, event):
136    """Append message to document history and log file and update
137    last_transition_date when transition happened.
138
139    Undo the verification of document and raise an exception if document
140    does not meet the requirements for verification.
141    """
142    if event.transition.destination == VERIFIED:
143        verifiable, error = obj.is_verifiable
144        if not verifiable:
145            # Undo transition and raise an exception.
146            IWorkflowState(obj).setState(event.transition.source)
147            raise InvalidTransitionError(error)
148    if event.transition.transition_id == 'verify':
149        obj.setMD5()
150    msg = event.transition.user_data['msg']
151    history = IObjectHistory(obj)
152    history.addMessage(msg)
153    obj.last_transition_date = datetime.utcnow()
154    try:
155        customers_container = grok.getSite()['customers']
156        customers_container.logger.info('%s - %s' % (obj.customer_id,msg))
157    except (TypeError, AttributeError):
158        pass
159    return
Note: See TracBrowser for help on using the repository browser.