[7195] | 1 | ## $Id: workflow.py 7195 2011-11-25 07:34:07Z 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 |
---|
| 26 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
| 27 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
| 28 | |
---|
| 29 | INITIALIZED = 'initialized' |
---|
| 30 | USED = 'used' |
---|
| 31 | DISABLED = 'disabled' |
---|
| 32 | |
---|
| 33 | def invalidate_action(wf, context): |
---|
[6547] | 34 | """Side actions taken when an access code is invalidated. |
---|
| 35 | """ |
---|
[6359] | 36 | batch = getattr(context, '__parent__', None) |
---|
| 37 | if batch is None: |
---|
| 38 | return |
---|
[6425] | 39 | batch.used_num += 1 |
---|
[6359] | 40 | return |
---|
| 41 | |
---|
[6425] | 42 | def disable_used_action(wf, context): |
---|
[6547] | 43 | """Side actions taken when a used access code is disabled. |
---|
| 44 | """ |
---|
[6425] | 45 | batch = getattr(context, '__parent__', None) |
---|
| 46 | if batch is None: |
---|
| 47 | return |
---|
| 48 | batch.used_num -= 1 |
---|
| 49 | batch.disabled_num += 1 |
---|
| 50 | |
---|
| 51 | def disable_unused_action(wf, context): |
---|
[6547] | 52 | """Side actions taken when an unused access code is invalidated. |
---|
| 53 | """ |
---|
[6425] | 54 | batch = getattr(context, '__parent__', None) |
---|
| 55 | if batch is None: |
---|
| 56 | return |
---|
| 57 | batch.disabled_num += 1 |
---|
[6359] | 58 | return |
---|
| 59 | |
---|
| 60 | def reenable_action(wf, context): |
---|
[6547] | 61 | """Side actions taken when an access code is reenabled. |
---|
| 62 | """ |
---|
[6359] | 63 | batch = getattr(context, '__parent__', None) |
---|
| 64 | if batch is None: |
---|
| 65 | return |
---|
[6425] | 66 | batch.disabled_num -= 1 |
---|
[6359] | 67 | return |
---|
| 68 | |
---|
| 69 | ACCESSCODE_TRANSITIONS = ( |
---|
| 70 | Transition( |
---|
| 71 | transition_id = 'init', |
---|
| 72 | title = 'Initialize PIN', |
---|
| 73 | source = None, |
---|
| 74 | condition = NullCondition, |
---|
| 75 | destination = INITIALIZED), |
---|
| 76 | |
---|
| 77 | Transition( |
---|
| 78 | transition_id = 'use', |
---|
| 79 | title = 'Use PIN', |
---|
| 80 | source = INITIALIZED, |
---|
| 81 | destination = USED, |
---|
| 82 | action = invalidate_action), |
---|
| 83 | |
---|
| 84 | Transition( |
---|
| 85 | transition_id = 'disable_unused', |
---|
| 86 | title = 'Disable unused PIN', |
---|
| 87 | source = INITIALIZED, |
---|
| 88 | destination = DISABLED, |
---|
[6425] | 89 | action = disable_unused_action), |
---|
[6359] | 90 | |
---|
| 91 | Transition( |
---|
| 92 | transition_id = 'disable_used', |
---|
| 93 | title = 'Disable used PIN', |
---|
| 94 | source = USED, |
---|
| 95 | destination = DISABLED, |
---|
[6425] | 96 | action = disable_used_action), |
---|
[6359] | 97 | |
---|
| 98 | Transition( |
---|
| 99 | transition_id = 'reenable', |
---|
| 100 | title = 'Reenable disabled PIN', |
---|
| 101 | source = DISABLED, |
---|
| 102 | destination = INITIALIZED, |
---|
| 103 | action = reenable_action), |
---|
| 104 | ) |
---|
| 105 | |
---|
| 106 | accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS) |
---|
| 107 | |
---|
| 108 | class AccessCodeWorkflowState(WorkflowState, grok.Adapter): |
---|
| 109 | grok.context(IAccessCode) |
---|
| 110 | grok.provides(IWorkflowState) |
---|
| 111 | |
---|
| 112 | state_key = 'wf.accesscode.state' |
---|
| 113 | state_id = 'wf.accesscode.id' |
---|
| 114 | |
---|
| 115 | class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
| 116 | grok.context(IAccessCode) |
---|
| 117 | grok.provides(IWAeUPWorkflowInfo) |
---|
| 118 | |
---|
| 119 | def __init__(self, context): |
---|
| 120 | self.context = context |
---|
| 121 | self.wf = accesscode_workflow |
---|
| 122 | |
---|
| 123 | @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) |
---|
| 124 | def handle_accesscode_transition_event(obj, event): |
---|
| 125 | # append message to history |
---|
[6420] | 126 | if event.comment is not None: |
---|
[6471] | 127 | msg = '%s' % event.comment |
---|
[6459] | 128 | else: |
---|
[6471] | 129 | msg = 'AC %s' % event.destination |
---|
[6459] | 130 | |
---|
[6359] | 131 | history = IObjectHistory(obj) |
---|
| 132 | history.addMessage(msg) |
---|
| 133 | return |
---|