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

Last change on this file since 6541 was 6478, checked in by Henrik Bettermann, 14 years ago

Add HB to copyright line.

File size: 3.7 KB
RevLine 
[6359]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##
[6478]7## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[6359]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    batch = getattr(context, '__parent__', None)
39    if batch is None:
40        return
[6425]41    batch.used_num += 1
[6359]42    return
43
[6425]44def disable_used_action(wf, context):
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    batch = getattr(context, '__parent__', None)
53    if batch is None:
54        return
55    batch.disabled_num += 1
[6359]56    return
57
58def reenable_action(wf, context):
59    batch = getattr(context, '__parent__', None)
60    if batch is None:
61        return
[6425]62    batch.disabled_num -= 1
[6359]63    return
64
65ACCESSCODE_TRANSITIONS = (
66    Transition(
67        transition_id = 'init',
68        title = 'Initialize PIN',
69        source = None,
70        condition = NullCondition,
71        destination = INITIALIZED),
72
73    Transition(
74        transition_id = 'use',
75        title = 'Use PIN',
76        source = INITIALIZED,
77        destination = USED,
78        action = invalidate_action),
79
80    Transition(
81        transition_id = 'disable_unused',
82        title = 'Disable unused PIN',
83        source = INITIALIZED,
84        destination = DISABLED,
[6425]85        action = disable_unused_action),
[6359]86
87    Transition(
88        transition_id = 'disable_used',
89        title = 'Disable used PIN',
90        source = USED,
91        destination = DISABLED,
[6425]92        action = disable_used_action),
[6359]93
94    Transition(
95        transition_id = 'reenable',
96        title = 'Reenable disabled PIN',
97        source = DISABLED,
98        destination = INITIALIZED,
99        action = reenable_action),
100    )
101
102accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS)
103
104class AccessCodeWorkflowState(WorkflowState, grok.Adapter):
105    grok.context(IAccessCode)
106    grok.provides(IWorkflowState)
107
108    state_key = 'wf.accesscode.state'
109    state_id = 'wf.accesscode.id'
110
111class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
112    grok.context(IAccessCode)
113    grok.provides(IWAeUPWorkflowInfo)
114
115    def __init__(self, context):
116        self.context = context
117        self.wf = accesscode_workflow
118
119@grok.subscribe(IAccessCode, IWorkflowTransitionEvent)
120def handle_accesscode_transition_event(obj, event):
121    # append message to history
[6420]122    if event.comment is not None:
[6471]123        msg = '%s' % event.comment
[6459]124    else:
[6471]125        msg = 'AC %s' % event.destination
[6459]126
[6359]127    history = IObjectHistory(obj)
128    history.addMessage(msg)
129    return
Note: See TracBrowser for help on using the repository browser.