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 | pin = context.representation |
---|
42 | batch.invalidate(pin) |
---|
43 | return |
---|
44 | |
---|
45 | def disable_action(wf, context): |
---|
46 | batch = getattr(context, '__parent__', None) |
---|
47 | if batch is None: |
---|
48 | return |
---|
49 | pin = context.representation |
---|
50 | batch.disable(pin) |
---|
51 | return |
---|
52 | |
---|
53 | def reenable_action(wf, context): |
---|
54 | batch = getattr(context, '__parent__', None) |
---|
55 | if batch is None: |
---|
56 | return |
---|
57 | pin = context.representation |
---|
58 | batch.enable(pin) |
---|
59 | return |
---|
60 | |
---|
61 | ACCESSCODE_TRANSITIONS = ( |
---|
62 | Transition( |
---|
63 | transition_id = 'init', |
---|
64 | title = 'Initialize PIN', |
---|
65 | source = None, |
---|
66 | condition = NullCondition, |
---|
67 | destination = INITIALIZED), |
---|
68 | |
---|
69 | Transition( |
---|
70 | transition_id = 'use', |
---|
71 | title = 'Use PIN', |
---|
72 | source = INITIALIZED, |
---|
73 | destination = USED, |
---|
74 | action = invalidate_action), |
---|
75 | |
---|
76 | Transition( |
---|
77 | transition_id = 'disable_unused', |
---|
78 | title = 'Disable unused PIN', |
---|
79 | source = INITIALIZED, |
---|
80 | destination = DISABLED, |
---|
81 | action = disable_action), |
---|
82 | |
---|
83 | Transition( |
---|
84 | transition_id = 'disable_used', |
---|
85 | title = 'Disable used PIN', |
---|
86 | source = USED, |
---|
87 | destination = DISABLED, |
---|
88 | action = disable_action), |
---|
89 | |
---|
90 | Transition( |
---|
91 | transition_id = 'reenable', |
---|
92 | title = 'Reenable disabled PIN', |
---|
93 | source = DISABLED, |
---|
94 | destination = INITIALIZED, |
---|
95 | action = reenable_action), |
---|
96 | ) |
---|
97 | |
---|
98 | accesscode_workflow = WAeUPWorkflow(ACCESSCODE_TRANSITIONS) |
---|
99 | |
---|
100 | class AccessCodeWorkflowState(WorkflowState, grok.Adapter): |
---|
101 | grok.context(IAccessCode) |
---|
102 | grok.provides(IWorkflowState) |
---|
103 | |
---|
104 | state_key = 'wf.accesscode.state' |
---|
105 | state_id = 'wf.accesscode.id' |
---|
106 | |
---|
107 | class AccessCodeWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): |
---|
108 | grok.context(IAccessCode) |
---|
109 | grok.provides(IWAeUPWorkflowInfo) |
---|
110 | |
---|
111 | def __init__(self, context): |
---|
112 | self.context = context |
---|
113 | self.wf = accesscode_workflow |
---|
114 | |
---|
115 | @grok.subscribe(IAccessCode, IWorkflowTransitionEvent) |
---|
116 | def handle_accesscode_transition_event(obj, event): |
---|
117 | # append message to history |
---|
118 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
119 | msg = '%s - %s (new state: %s)' % ( |
---|
120 | timestamp, event.destination.title, event.destination) |
---|
121 | history = IObjectHistory(obj) |
---|
122 | history.addMessage(msg) |
---|
123 | return |
---|