1 | ## |
---|
2 | ## workflow.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sat Jun 11 00:35:16 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
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 | batch = getattr(context, '__parent__', None) |
---|
39 | if batch is None: |
---|
40 | return |
---|
41 | batch.used_num += 1 |
---|
42 | return |
---|
43 | |
---|
44 | def disable_used_action(wf, context): |
---|
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 | batch = getattr(context, '__parent__', None) |
---|
53 | if batch is None: |
---|
54 | return |
---|
55 | batch.disabled_num += 1 |
---|
56 | return |
---|
57 | |
---|
58 | def reenable_action(wf, context): |
---|
59 | batch = getattr(context, '__parent__', None) |
---|
60 | if batch is None: |
---|
61 | return |
---|
62 | batch.disabled_num -= 1 |
---|
63 | return |
---|
64 | |
---|
65 | ACCESSCODE_TRANSITIONS = ( |
---|
66 | Transition( |
---|
67 | transition_id = 'init', |
---|
68 | title = 'Initialize PIN', |
---|
69 | source = None, |
---|
70 | condition = NullCondition, |
---|
71 | destination = INITIALIZED), |
---|
72 | |
---|
73 | Transition( |
---|
74 | transition_id = 'use', |
---|
75 | title = 'Use PIN', |
---|
76 | source = INITIALIZED, |
---|
77 | destination = USED, |
---|
78 | action = invalidate_action), |
---|
79 | |
---|
80 | Transition( |
---|
81 | transition_id = 'disable_unused', |
---|
82 | title = 'Disable unused PIN', |
---|
83 | source = INITIALIZED, |
---|
84 | destination = DISABLED, |
---|
85 | action = disable_unused_action), |
---|
86 | |
---|
87 | Transition( |
---|
88 | transition_id = 'disable_used', |
---|
89 | title = 'Disable used PIN', |
---|
90 | source = USED, |
---|
91 | destination = DISABLED, |
---|
92 | action = disable_used_action), |
---|
93 | |
---|
94 | Transition( |
---|
95 | transition_id = 'reenable', |
---|
96 | title = 'Reenable disabled PIN', |
---|
97 | source = DISABLED, |
---|
98 | destination = INITIALIZED, |
---|
99 | action = reenable_action), |
---|
100 | ) |
---|
101 | |
---|
102 | accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS) |
---|
103 | |
---|
104 | class AccessCodeWorkflowState(WorkflowState, grok.Adapter): |
---|
105 | grok.context(IAccessCode) |
---|
106 | grok.provides(IWorkflowState) |
---|
107 | |
---|
108 | state_key = 'wf.accesscode.state' |
---|
109 | state_id = 'wf.accesscode.id' |
---|
110 | |
---|
111 | class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
112 | grok.context(IAccessCode) |
---|
113 | grok.provides(IWAeUPWorkflowInfo) |
---|
114 | |
---|
115 | def __init__(self, context): |
---|
116 | self.context = context |
---|
117 | self.wf = accesscode_workflow |
---|
118 | |
---|
119 | @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) |
---|
120 | def handle_accesscode_transition_event(obj, event): |
---|
121 | # append message to history |
---|
122 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
123 | msg = '%s -' % timestamp |
---|
124 | if event.comment is not None: |
---|
125 | msg = '%s %s' % (msg, event.comment) |
---|
126 | else: |
---|
127 | msg = '%s %s by system' % (msg,event.destination.title()) |
---|
128 | |
---|
129 | history = IObjectHistory(obj) |
---|
130 | history.addMessage(msg) |
---|
131 | return |
---|