[7195] | 1 | ## $Id: workflow.py 7819 2012-03-08 22:28:46Z 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 |
---|
[7719] | 22 | from zope.i18n import translate |
---|
| 23 | from zope.component import getUtility |
---|
[6359] | 24 | from datetime import datetime |
---|
| 25 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
| 26 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
[7811] | 27 | from waeup.kofa.accesscodes.interfaces import IAccessCode |
---|
[7819] | 28 | from waeup.kofa.interfaces import IObjectHistory, IKofaWorkflowInfo |
---|
[7811] | 29 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7819] | 30 | from waeup.kofa.workflow import KofaWorkflow, KofaWorkflowInfo |
---|
[6359] | 31 | |
---|
| 32 | INITIALIZED = 'initialized' |
---|
| 33 | USED = 'used' |
---|
| 34 | DISABLED = 'disabled' |
---|
| 35 | |
---|
[7689] | 36 | ac_states_dict = { |
---|
| 37 | INITIALIZED: _('initialized'), |
---|
| 38 | USED: _('used'), |
---|
| 39 | DISABLED: _('disabled'), |
---|
| 40 | } |
---|
| 41 | |
---|
[6359] | 42 | def 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] | 51 | def 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 | |
---|
| 60 | def 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 | |
---|
| 69 | def 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 | |
---|
| 78 | ACCESSCODE_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 | |
---|
[7819] | 115 | accesscode_workflow = KofaWorkflow(ACCESSCODE_TRANSITIONS) |
---|
[6359] | 116 | |
---|
| 117 | class 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 | |
---|
[7819] | 124 | class AccessCodeWorkflowInfo(KofaWorkflowInfo, grok.Adapter): |
---|
[6359] | 125 | grok.context(IAccessCode) |
---|
[7819] | 126 | grok.provides(IKofaWorkflowInfo) |
---|
[6359] | 127 | |
---|
| 128 | def __init__(self, context): |
---|
| 129 | self.context = context |
---|
| 130 | self.wf = accesscode_workflow |
---|
| 131 | |
---|
| 132 | @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) |
---|
| 133 | def 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 |
---|