[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 | """ |
---|
| 19 | Workflows for access codes/pins. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
| 22 | from datetime import datetime |
---|
| 23 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
| 24 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
| 25 | from waeup.sirp.accesscodes.interfaces import IAccessCode |
---|
[7321] | 26 | from waeup.sirp.interfaces import IObjectHistory, ISIRPWorkflowInfo |
---|
[7689] | 27 | from waeup.sirp.interfaces import MessageFactory as _ |
---|
[7321] | 28 | from waeup.sirp.workflow import SIRPWorkflow, SIRPWorkflowInfo |
---|
[6359] | 29 | |
---|
| 30 | INITIALIZED = 'initialized' |
---|
| 31 | USED = 'used' |
---|
| 32 | DISABLED = 'disabled' |
---|
| 33 | |
---|
[7689] | 34 | ac_states_dict = { |
---|
| 35 | INITIALIZED: _('initialized'), |
---|
| 36 | USED: _('used'), |
---|
| 37 | DISABLED: _('disabled'), |
---|
| 38 | } |
---|
| 39 | |
---|
[6359] | 40 | def 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] | 49 | def 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 | |
---|
| 58 | def 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 | |
---|
| 67 | def 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 | |
---|
| 76 | ACCESSCODE_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] | 113 | accesscode_workflow = SIRPWorkflow(ACCESSCODE_TRANSITIONS) |
---|
[6359] | 114 | |
---|
| 115 | class 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] | 122 | class 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) |
---|
| 131 | def 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 |
---|