1 | ## $Id: workflow.py 7321 2011-12-10 06:15:17Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
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. |
---|
8 | ## |
---|
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. |
---|
13 | ## |
---|
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, ISIRPWorkflowInfo |
---|
27 | from waeup.sirp.workflow import SIRPWorkflow, SIRPWorkflowInfo |
---|
28 | |
---|
29 | INITIALIZED = 'initialized' |
---|
30 | USED = 'used' |
---|
31 | DISABLED = 'disabled' |
---|
32 | |
---|
33 | def invalidate_action(wf, context): |
---|
34 | """Side actions taken when an access code is invalidated. |
---|
35 | """ |
---|
36 | batch = getattr(context, '__parent__', None) |
---|
37 | if batch is None: |
---|
38 | return |
---|
39 | batch.used_num += 1 |
---|
40 | return |
---|
41 | |
---|
42 | def disable_used_action(wf, context): |
---|
43 | """Side actions taken when a used access code is disabled. |
---|
44 | """ |
---|
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): |
---|
52 | """Side actions taken when an unused access code is invalidated. |
---|
53 | """ |
---|
54 | batch = getattr(context, '__parent__', None) |
---|
55 | if batch is None: |
---|
56 | return |
---|
57 | batch.disabled_num += 1 |
---|
58 | return |
---|
59 | |
---|
60 | def reenable_action(wf, context): |
---|
61 | """Side actions taken when an access code is reenabled. |
---|
62 | """ |
---|
63 | batch = getattr(context, '__parent__', None) |
---|
64 | if batch is None: |
---|
65 | return |
---|
66 | batch.disabled_num -= 1 |
---|
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, |
---|
89 | action = disable_unused_action), |
---|
90 | |
---|
91 | Transition( |
---|
92 | transition_id = 'disable_used', |
---|
93 | title = 'Disable used PIN', |
---|
94 | source = USED, |
---|
95 | destination = DISABLED, |
---|
96 | action = disable_used_action), |
---|
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 = SIRPWorkflow(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(SIRPWorkflowInfo, grok.Adapter): |
---|
116 | grok.context(IAccessCode) |
---|
117 | grok.provides(ISIRPWorkflowInfo) |
---|
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 |
---|
126 | if event.comment is not None: |
---|
127 | msg = '%s' % event.comment |
---|
128 | else: |
---|
129 | msg = 'AC %s' % event.destination |
---|
130 | |
---|
131 | history = IObjectHistory(obj) |
---|
132 | history.addMessage(msg) |
---|
133 | return |
---|