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

Last change on this file since 7113 was 6547, checked in by uli, 13 years ago

Add docs.

File size: 4.0 KB
Line 
1##
2## workflow.py
3## Login : <uli@pu.smp.net>
4## Started on  Sat Jun 11 00:35:16 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""
23Workflows for access codes/pins.
24"""
25import grok
26from datetime import datetime
27from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
28from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
29from waeup.sirp.accesscodes.interfaces import IAccessCode
30from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo
31from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo
32
33INITIALIZED = 'initialized'
34USED = 'used'
35DISABLED = 'disabled'
36
37def invalidate_action(wf, context):
38    """Side actions taken when an access code is invalidated.
39    """
40    batch = getattr(context, '__parent__', None)
41    if batch is None:
42        return
43    batch.used_num += 1
44    return
45
46def disable_used_action(wf, context):
47    """Side actions taken when a used access code is disabled.
48    """
49    batch = getattr(context, '__parent__', None)
50    if batch is None:
51        return
52    batch.used_num -= 1
53    batch.disabled_num += 1
54
55def disable_unused_action(wf, context):
56    """Side actions taken when an unused access code is invalidated.
57    """
58    batch = getattr(context, '__parent__', None)
59    if batch is None:
60        return
61    batch.disabled_num += 1
62    return
63
64def reenable_action(wf, context):
65    """Side actions taken when an access code is reenabled.
66    """
67    batch = getattr(context, '__parent__', None)
68    if batch is None:
69        return
70    batch.disabled_num -= 1
71    return
72
73ACCESSCODE_TRANSITIONS = (
74    Transition(
75        transition_id = 'init',
76        title = 'Initialize PIN',
77        source = None,
78        condition = NullCondition,
79        destination = INITIALIZED),
80
81    Transition(
82        transition_id = 'use',
83        title = 'Use PIN',
84        source = INITIALIZED,
85        destination = USED,
86        action = invalidate_action),
87
88    Transition(
89        transition_id = 'disable_unused',
90        title = 'Disable unused PIN',
91        source = INITIALIZED,
92        destination = DISABLED,
93        action = disable_unused_action),
94
95    Transition(
96        transition_id = 'disable_used',
97        title = 'Disable used PIN',
98        source = USED,
99        destination = DISABLED,
100        action = disable_used_action),
101
102    Transition(
103        transition_id = 'reenable',
104        title = 'Reenable disabled PIN',
105        source = DISABLED,
106        destination = INITIALIZED,
107        action = reenable_action),
108    )
109
110accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS)
111
112class AccessCodeWorkflowState(WorkflowState, grok.Adapter):
113    grok.context(IAccessCode)
114    grok.provides(IWorkflowState)
115
116    state_key = 'wf.accesscode.state'
117    state_id = 'wf.accesscode.id'
118
119class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
120    grok.context(IAccessCode)
121    grok.provides(IWAeUPWorkflowInfo)
122
123    def __init__(self, context):
124        self.context = context
125        self.wf = accesscode_workflow
126
127@grok.subscribe(IAccessCode, IWorkflowTransitionEvent)
128def handle_accesscode_transition_event(obj, event):
129    # append message to history
130    if event.comment is not None:
131        msg = '%s' % event.comment
132    else:
133        msg = 'AC %s' % event.destination
134
135    history = IObjectHistory(obj)
136    history.addMessage(msg)
137    return
Note: See TracBrowser for help on using the repository browser.