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

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

Add first browser components with tests.

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