source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/workflow.py @ 7270

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

More copyright adjustments.

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