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

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

Start internationalization of students package (work in progress).

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