source: main/waeup.kofa/branches/uli-fix-zc-async/src/waeup/kofa/accesscodes/workflow.py @ 17931

Last change on this file since 17931 was 7819, checked in by Henrik Bettermann, 13 years ago

KOFA -> Kofa

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1## $Id: workflow.py 7819 2012-03-08 22:28:46Z henrik $
2##
3## Copyright (C) 2011 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"""
19Workflows for access codes/pins.
20"""
21import grok
22from zope.i18n import translate
23from zope.component import getUtility
24from datetime import datetime
25from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
26from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
27from waeup.kofa.accesscodes.interfaces import IAccessCode
28from waeup.kofa.interfaces import IObjectHistory, IKofaWorkflowInfo
29from waeup.kofa.interfaces import MessageFactory as _
30from waeup.kofa.workflow import KofaWorkflow, KofaWorkflowInfo
31
32INITIALIZED = 'initialized'
33USED = 'used'
34DISABLED = 'disabled'
35
36ac_states_dict = {
37    INITIALIZED: _('initialized'),
38    USED: _('used'),
39    DISABLED: _('disabled'),
40    }
41
42def invalidate_action(wf, context):
43    """Side actions taken when an access code is invalidated.
44    """
45    batch = getattr(context, '__parent__', None)
46    if batch is None:
47        return
48    batch.used_num += 1
49    return
50
51def disable_used_action(wf, context):
52    """Side actions taken when a used access code is disabled.
53    """
54    batch = getattr(context, '__parent__', None)
55    if batch is None:
56        return
57    batch.used_num -= 1
58    batch.disabled_num += 1
59
60def disable_unused_action(wf, context):
61    """Side actions taken when an unused access code is invalidated.
62    """
63    batch = getattr(context, '__parent__', None)
64    if batch is None:
65        return
66    batch.disabled_num += 1
67    return
68
69def reenable_action(wf, context):
70    """Side actions taken when an access code is reenabled.
71    """
72    batch = getattr(context, '__parent__', None)
73    if batch is None:
74        return
75    batch.disabled_num -= 1
76    return
77
78ACCESSCODE_TRANSITIONS = (
79    Transition(
80        transition_id = 'init',
81        title = _('Initialize PIN'),
82        source = None,
83        condition = NullCondition,
84        destination = INITIALIZED),
85
86    Transition(
87        transition_id = 'use',
88        title = _('Use PIN'),
89        source = INITIALIZED,
90        destination = USED,
91        action = invalidate_action),
92
93    Transition(
94        transition_id = 'disable_unused',
95        title = _('Disable unused PIN'),
96        source = INITIALIZED,
97        destination = DISABLED,
98        action = disable_unused_action),
99
100    Transition(
101        transition_id = 'disable_used',
102        title = _('Disable used PIN'),
103        source = USED,
104        destination = DISABLED,
105        action = disable_used_action),
106
107    Transition(
108        transition_id = 'reenable',
109        title = _('Reenable disabled PIN'),
110        source = DISABLED,
111        destination = INITIALIZED,
112        action = reenable_action),
113    )
114
115accesscode_workflow = KofaWorkflow(ACCESSCODE_TRANSITIONS)
116
117class AccessCodeWorkflowState(WorkflowState, grok.Adapter):
118    grok.context(IAccessCode)
119    grok.provides(IWorkflowState)
120
121    state_key = 'wf.accesscode.state'
122    state_id = 'wf.accesscode.id'
123
124class AccessCodeWorkflowInfo(KofaWorkflowInfo, grok.Adapter):
125    grok.context(IAccessCode)
126    grok.provides(IKofaWorkflowInfo)
127
128    def __init__(self, context):
129        self.context = context
130        self.wf = accesscode_workflow
131
132@grok.subscribe(IAccessCode, IWorkflowTransitionEvent)
133def handle_accesscode_transition_event(obj, event):
134    # append message to history
135    if event.comment is not None:
136        msg = event.comment
137    else:
138        msg = ac_states_dict[event.destination]
139    history = IObjectHistory(obj)
140    history.addMessage(msg)
141    return
Note: See TracBrowser for help on using the repository browser.