source: main/waeup.sirp/branches/ulif-groktoolkit-1.4/src/waeup/sirp/accesscodes/workflow.py @ 9819

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

Translate accesscode workflow.

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