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

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

Translate accesscode workflow.

  • Property svn:keywords set to Id
File size: 4.1 KB
RevLine 
[7195]1## $Id: workflow.py 7689 2012-02-23 12:43:11Z henrik $
[6359]2##
[6478]3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[6359]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.
[7195]8##
[6359]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.
[7195]13##
[6359]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
[7321]26from waeup.sirp.interfaces import IObjectHistory, ISIRPWorkflowInfo
[7689]27from waeup.sirp.interfaces import MessageFactory as _
[7321]28from waeup.sirp.workflow import SIRPWorkflow, SIRPWorkflowInfo
[6359]29
30INITIALIZED = 'initialized'
31USED = 'used'
32DISABLED = 'disabled'
33
[7689]34ac_states_dict = {
35    INITIALIZED: _('initialized'),
36    USED: _('used'),
37    DISABLED: _('disabled'),
38    }
39
[6359]40def invalidate_action(wf, context):
[6547]41    """Side actions taken when an access code is invalidated.
42    """
[6359]43    batch = getattr(context, '__parent__', None)
44    if batch is None:
45        return
[6425]46    batch.used_num += 1
[6359]47    return
48
[6425]49def disable_used_action(wf, context):
[6547]50    """Side actions taken when a used access code is disabled.
51    """
[6425]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):
[6547]59    """Side actions taken when an unused access code is invalidated.
60    """
[6425]61    batch = getattr(context, '__parent__', None)
62    if batch is None:
63        return
64    batch.disabled_num += 1
[6359]65    return
66
67def reenable_action(wf, context):
[6547]68    """Side actions taken when an access code is reenabled.
69    """
[6359]70    batch = getattr(context, '__parent__', None)
71    if batch is None:
72        return
[6425]73    batch.disabled_num -= 1
[6359]74    return
75
76ACCESSCODE_TRANSITIONS = (
77    Transition(
78        transition_id = 'init',
[7689]79        title = _('Initialize PIN'),
[6359]80        source = None,
81        condition = NullCondition,
82        destination = INITIALIZED),
83
84    Transition(
85        transition_id = 'use',
[7689]86        title = _('Use PIN'),
[6359]87        source = INITIALIZED,
88        destination = USED,
89        action = invalidate_action),
90
91    Transition(
92        transition_id = 'disable_unused',
[7689]93        title = _('Disable unused PIN'),
[6359]94        source = INITIALIZED,
95        destination = DISABLED,
[6425]96        action = disable_unused_action),
[6359]97
98    Transition(
99        transition_id = 'disable_used',
[7689]100        title = _('Disable used PIN'),
[6359]101        source = USED,
102        destination = DISABLED,
[6425]103        action = disable_used_action),
[6359]104
105    Transition(
106        transition_id = 'reenable',
[7689]107        title = _('Reenable disabled PIN'),
[6359]108        source = DISABLED,
109        destination = INITIALIZED,
110        action = reenable_action),
111    )
112
[7321]113accesscode_workflow = SIRPWorkflow(ACCESSCODE_TRANSITIONS)
[6359]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
[7321]122class AccessCodeWorkflowInfo(SIRPWorkflowInfo, grok.Adapter):
[6359]123    grok.context(IAccessCode)
[7321]124    grok.provides(ISIRPWorkflowInfo)
[6359]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
[6420]133    if event.comment is not None:
[6471]134        msg = '%s' % event.comment
[6459]135    else:
[6471]136        msg = 'AC %s' % event.destination
[6459]137
[6359]138    history = IObjectHistory(obj)
139    history.addMessage(msg)
140    return
Note: See TracBrowser for help on using the repository browser.