1 | ## |
---|
2 | ## workflow.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sat Jun 11 00:35:16 2011 Uli Fouquet |
---|
5 | ## $Id: workflow.py 7137 2011-11-19 08:37:08Z henrik $ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """ |
---|
23 | Workflows for access codes/pins. |
---|
24 | """ |
---|
25 | import grok |
---|
26 | from datetime import datetime |
---|
27 | from hurry.workflow.workflow import Transition, WorkflowState, NullCondition |
---|
28 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent |
---|
29 | from waeup.sirp.accesscodes.interfaces import IAccessCode |
---|
30 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo |
---|
31 | from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo |
---|
32 | |
---|
33 | INITIALIZED = 'initialized' |
---|
34 | USED = 'used' |
---|
35 | DISABLED = 'disabled' |
---|
36 | |
---|
37 | def invalidate_action(wf, context): |
---|
38 | """Side actions taken when an access code is invalidated. |
---|
39 | """ |
---|
40 | batch = getattr(context, '__parent__', None) |
---|
41 | if batch is None: |
---|
42 | return |
---|
43 | batch.used_num += 1 |
---|
44 | return |
---|
45 | |
---|
46 | def disable_used_action(wf, context): |
---|
47 | """Side actions taken when a used access code is disabled. |
---|
48 | """ |
---|
49 | batch = getattr(context, '__parent__', None) |
---|
50 | if batch is None: |
---|
51 | return |
---|
52 | batch.used_num -= 1 |
---|
53 | batch.disabled_num += 1 |
---|
54 | |
---|
55 | def disable_unused_action(wf, context): |
---|
56 | """Side actions taken when an unused access code is invalidated. |
---|
57 | """ |
---|
58 | batch = getattr(context, '__parent__', None) |
---|
59 | if batch is None: |
---|
60 | return |
---|
61 | batch.disabled_num += 1 |
---|
62 | return |
---|
63 | |
---|
64 | def reenable_action(wf, context): |
---|
65 | """Side actions taken when an access code is reenabled. |
---|
66 | """ |
---|
67 | batch = getattr(context, '__parent__', None) |
---|
68 | if batch is None: |
---|
69 | return |
---|
70 | batch.disabled_num -= 1 |
---|
71 | return |
---|
72 | |
---|
73 | ACCESSCODE_TRANSITIONS = ( |
---|
74 | Transition( |
---|
75 | transition_id = 'init', |
---|
76 | title = 'Initialize PIN', |
---|
77 | source = None, |
---|
78 | condition = NullCondition, |
---|
79 | destination = INITIALIZED), |
---|
80 | |
---|
81 | Transition( |
---|
82 | transition_id = 'use', |
---|
83 | title = 'Use PIN', |
---|
84 | source = INITIALIZED, |
---|
85 | destination = USED, |
---|
86 | action = invalidate_action), |
---|
87 | |
---|
88 | Transition( |
---|
89 | transition_id = 'disable_unused', |
---|
90 | title = 'Disable unused PIN', |
---|
91 | source = INITIALIZED, |
---|
92 | destination = DISABLED, |
---|
93 | action = disable_unused_action), |
---|
94 | |
---|
95 | Transition( |
---|
96 | transition_id = 'disable_used', |
---|
97 | title = 'Disable used PIN', |
---|
98 | source = USED, |
---|
99 | destination = DISABLED, |
---|
100 | action = disable_used_action), |
---|
101 | |
---|
102 | Transition( |
---|
103 | transition_id = 'reenable', |
---|
104 | title = 'Reenable disabled PIN', |
---|
105 | source = DISABLED, |
---|
106 | destination = INITIALIZED, |
---|
107 | action = reenable_action), |
---|
108 | ) |
---|
109 | |
---|
110 | accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS) |
---|
111 | |
---|
112 | class AccessCodeWorkflowState(WorkflowState, grok.Adapter): |
---|
113 | grok.context(IAccessCode) |
---|
114 | grok.provides(IWorkflowState) |
---|
115 | |
---|
116 | state_key = 'wf.accesscode.state' |
---|
117 | state_id = 'wf.accesscode.id' |
---|
118 | |
---|
119 | class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
120 | grok.context(IAccessCode) |
---|
121 | grok.provides(IWAeUPWorkflowInfo) |
---|
122 | |
---|
123 | def __init__(self, context): |
---|
124 | self.context = context |
---|
125 | self.wf = accesscode_workflow |
---|
126 | |
---|
127 | @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) |
---|
128 | def handle_accesscode_transition_event(obj, event): |
---|
129 | # append message to history |
---|
130 | if event.comment is not None: |
---|
131 | msg = '%s' % event.comment |
---|
132 | else: |
---|
133 | msg = 'AC %s' % event.destination |
---|
134 | |
---|
135 | history = IObjectHistory(obj) |
---|
136 | history.addMessage(msg) |
---|
137 | return |
---|