## ## workflow.py ## Login : ## Started on Sat Jun 11 00:35:16 2011 Uli Fouquet ## $Id$ ## ## Copyright (C) 2011 Uli Fouquet ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Workflows for access codes/pins. """ import grok from datetime import datetime from hurry.workflow.workflow import Transition, WorkflowState, NullCondition from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent from waeup.sirp.accesscodes.interfaces import IAccessCode from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo INITIALIZED = 'initialized' USED = 'used' DISABLED = 'disabled' def invalidate_action(wf, context): batch = getattr(context, '__parent__', None) if batch is None: return pin = context.representation batch.invalidate(pin) return def disable_action(wf, context): batch = getattr(context, '__parent__', None) if batch is None: return pin = context.representation batch.disable(pin) return def reenable_action(wf, context): batch = getattr(context, '__parent__', None) if batch is None: return pin = context.representation batch.enable(pin) return ACCESSCODE_TRANSITIONS = ( Transition( transition_id = 'init', title = 'Initialize PIN', source = None, condition = NullCondition, destination = INITIALIZED), Transition( transition_id = 'use', title = 'Use PIN', source = INITIALIZED, destination = USED, action = invalidate_action), Transition( transition_id = 'disable_unused', title = 'Disable unused PIN', source = INITIALIZED, destination = DISABLED, action = disable_action), Transition( transition_id = 'disable_used', title = 'Disable used PIN', source = USED, destination = DISABLED, action = disable_action), Transition( transition_id = 'reenable', title = 'Reenable disabled PIN', source = DISABLED, destination = INITIALIZED, action = reenable_action), ) accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS) class AccessCodeWorkflowState(WorkflowState, grok.Adapter): grok.context(IAccessCode) grok.provides(IWorkflowState) state_key = 'wf.accesscode.state' state_id = 'wf.accesscode.id' class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): grok.context(IAccessCode) grok.provides(IWAeUPWorkflowInfo) def __init__(self, context): self.context = context self.wf = accesscode_workflow @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) def handle_accesscode_transition_event(obj, event): # append message to history timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") msg = '%s - %s (new state: %s)' % ( timestamp, event.destination.title, event.destination) history = IObjectHistory(obj) history.addMessage(msg) return