author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 20 Aug 2009 17:44:27 +0200 | |
branch | 3.5 |
changeset 2920 | 64322aa83a1d |
parent 2917 | 9a243ba71260 |
child 2943 | 77622caef9bd |
permissions | -rw-r--r-- |
0 | 1 |
"""workflow definition and history related entities |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1572
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1572
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
10 |
from warnings import warn |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
11 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
12 |
from logilab.common.decorators import cached |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
13 |
from logilab.common.deprecation import deprecated |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
14 |
|
0 | 15 |
from cubicweb.entities import AnyEntity, fetch_config |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
16 |
from cubicweb.interfaces import IWorkflowable |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
17 |
from cubicweb.common.mixins import MI_REL_TRIGGERS |
0 | 18 |
|
1572 | 19 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
20 |
class Workflow(AnyEntity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
21 |
id = 'Workflow' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
22 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
23 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
24 |
def initial(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
25 |
"""return the initial state for this workflow""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
26 |
return self.initial_state and self.initial_state[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
27 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
28 |
def is_default_workflow_of(self, etype): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
29 |
"""return True if this workflow is the default workflow for the given |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
30 |
entity type |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
31 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
32 |
return any(et for et in self.default_workflow_of if et.name == etype) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
33 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
34 |
def after_deletion_path(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
35 |
"""return (path, parameters) which should be used as redirect |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
36 |
information when this entity is being deleted |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
37 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
38 |
if self.workflow_of: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
39 |
return self.workflow_of[0].rest_path(), {'vid': 'workflow'} |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
40 |
return super(Workflow, self).after_deletion_path() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
41 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
42 |
# state / transitions accessors ############################################ |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
43 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
44 |
def state_by_name(self, statename): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
45 |
rset = self.req.execute('Any S, SN WHERE S name SN, S name %(n)s, ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
46 |
'S state_of WF, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
47 |
{'n': statename, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
48 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
49 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
50 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
51 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
52 |
def state_by_eid(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
53 |
rset = self.req.execute('Any S, SN WHERE S name SN, S eid %(s)s, ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
54 |
'S state_of WF, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
55 |
{'s': eid, 'wf': self.eid}, ('wf', 's')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
56 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
57 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
58 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
59 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
60 |
def transition_by_name(self, trname): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
61 |
rset = self.req.execute('Any T, TN WHERE T name TN, T name %(n)s, ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
62 |
'T transition_of WF, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
63 |
{'n': trname, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
64 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
65 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
66 |
return None |
0 | 67 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
68 |
def transition_by_eid(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
69 |
rset = self.req.execute('Any T, TN WHERE T name TN, T eid %(t)s, ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
70 |
'T transition_of WF, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
71 |
{'t': eid, 'wf': self.eid}, ('wf', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
72 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
73 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
74 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
75 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
76 |
# wf construction methods ################################################## |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
77 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
78 |
def add_state(self, name, initial=False, **kwargs): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
79 |
"""method to ease workflow definition: add a state for one or more |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
80 |
entity type(s) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
81 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
82 |
state = self.req.create_entity('State', name=name, **kwargs) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
83 |
self.req.execute('SET S state_of WF WHERE S eid %(s)s, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
84 |
{'s': state.eid, 'wf': self.eid}, ('s', 'wf')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
85 |
if initial: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
86 |
assert not self.initial |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
87 |
self.req.execute('SET WF initial_state S ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
88 |
'WHERE S eid %(s)s, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
89 |
{'s': state.eid, 'wf': self.eid}, ('s', 'wf')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
90 |
return state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
91 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
92 |
def add_transition(self, name, fromstates, tostate, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
93 |
requiredgroups=(), conditions=(), **kwargs): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
94 |
"""method to ease workflow definition: add a transition for one or more |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
95 |
entity type(s), from one or more state and to a single state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
96 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
97 |
tr = self.req.create_entity('Transition', name=name, **kwargs) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
98 |
self.req.execute('SET T transition_of WF ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
99 |
'WHERE T eid %(t)s, WF eid %(wf)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
100 |
{'t': tr.eid, 'wf': self.eid}, ('t', 'wf')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
101 |
for state in fromstates: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
102 |
if hasattr(state, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
103 |
state = state.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
104 |
self.req.execute('SET S allowed_transition T ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
105 |
'WHERE S eid %(s)s, T eid %(t)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
106 |
{'s': state, 't': tr.eid}, ('s', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
107 |
if hasattr(tostate, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
108 |
tostate = tostate.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
109 |
self.req.execute('SET T destination_state S ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
110 |
'WHERE S eid %(s)s, T eid %(t)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
111 |
{'t': tr.eid, 's': tostate}, ('s', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
112 |
tr.set_transition_permissions(requiredgroups, conditions, reset=False) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
113 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
114 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
115 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
116 |
class BaseTransition(AnyEntity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
117 |
"""customized class for abstract transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
118 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
119 |
provides a specific may_be_fired method to check if the relation may be |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
120 |
fired by the logged user |
0 | 121 |
""" |
122 |
id = 'Transition' |
|
123 |
fetch_attrs, fetch_order = fetch_config(['name']) |
|
1572 | 124 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
125 |
def may_be_fired(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
126 |
"""return true if the logged user may fire this transition |
0 | 127 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
128 |
`eid` is the eid of the object on which we may fire the transition |
0 | 129 |
""" |
130 |
user = self.req.user |
|
131 |
# check user is at least in one of the required groups if any |
|
132 |
groups = frozenset(g.name for g in self.require_group) |
|
133 |
if groups: |
|
134 |
matches = user.matching_groups(groups) |
|
135 |
if matches: |
|
136 |
return matches |
|
137 |
if 'owners' in groups and user.owns(eid): |
|
138 |
return True |
|
139 |
# check one of the rql expression conditions matches if any |
|
140 |
if self.condition: |
|
141 |
for rqlexpr in self.condition: |
|
142 |
if rqlexpr.check_expression(self.req, eid): |
|
143 |
return True |
|
144 |
if self.condition or groups: |
|
145 |
return False |
|
146 |
return True |
|
147 |
||
148 |
def after_deletion_path(self): |
|
149 |
"""return (path, parameters) which should be used as redirect |
|
150 |
information when this entity is being deleted |
|
151 |
""" |
|
152 |
if self.transition_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
153 |
return self.transition_of[0].rest_path(), {} |
0 | 154 |
return super(Transition, self).after_deletion_path() |
155 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
156 |
def set_transition_permissions(self, requiredgroups=(), conditions=(), |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
157 |
reset=True): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
158 |
"""set or add (if `reset` is False) groups and conditions for this |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
159 |
transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
160 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
161 |
if reset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
162 |
self.req.execute('DELETE T require_group G WHERE T eid %(x)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
163 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
164 |
self.req.execute('DELETE T condition R WHERE T eid %(x)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
165 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
166 |
for gname in requiredgroups: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
167 |
### XXX ensure gname validity |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
168 |
rset = self.req.execute('SET T require_group G ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
169 |
'WHERE T eid %(x)s, G name %(gn)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
170 |
{'x': self.eid, 'gn': gname}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
171 |
assert rset, '%s is not a known group' % gname |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
172 |
if isinstance(conditions, basestring): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
173 |
conditions = (conditions,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
174 |
for expr in conditions: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
175 |
if isinstance(expr, str): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
176 |
expr = unicode(expr) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
177 |
self.req.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
178 |
'X expression %(expr)s, T condition X ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
179 |
'WHERE T eid %(x)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
180 |
{'x': self.eid, 'expr': expr}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
181 |
# XXX clear caches? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
182 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
183 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
184 |
class Transition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
185 |
"""customized class for Transition entities""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
186 |
id = 'Transition' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
187 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
188 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
189 |
return self.destination_state[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
190 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
191 |
def has_input_state(self, state): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
192 |
if hasattr(state, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
193 |
state = state.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
194 |
return any(s for s in self.reverse_allowed_transition if s.eid == state) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
195 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
196 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
197 |
class WorkflowTransition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
198 |
"""customized class for WorkflowTransition entities""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
199 |
id = 'WorkflowTransition' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
200 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
201 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
202 |
def subwf(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
203 |
return self.subworkflow[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
204 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
205 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
206 |
return self.subwf.initial |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
207 |
|
1572 | 208 |
|
0 | 209 |
class State(AnyEntity): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
210 |
"""customized class for State entities""" |
0 | 211 |
id = 'State' |
212 |
fetch_attrs, fetch_order = fetch_config(['name']) |
|
213 |
rest_attr = 'eid' |
|
1572 | 214 |
|
0 | 215 |
def after_deletion_path(self): |
216 |
"""return (path, parameters) which should be used as redirect |
|
217 |
information when this entity is being deleted |
|
218 |
""" |
|
219 |
if self.state_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
220 |
return self.state_of[0].rest_path(), {} |
0 | 221 |
return super(State, self).after_deletion_path() |
222 |
||
1572 | 223 |
|
0 | 224 |
class TrInfo(AnyEntity): |
225 |
"""customized class for Transition information entities |
|
226 |
""" |
|
227 |
id = 'TrInfo' |
|
228 |
fetch_attrs, fetch_order = fetch_config(['creation_date', 'comment'], |
|
229 |
pclass=None) # don't want modification_date |
|
230 |
@property |
|
231 |
def for_entity(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
232 |
return self.wf_info_for[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
233 |
|
0 | 234 |
@property |
235 |
def previous_state(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
236 |
return self.from_state[0] |
1572 | 237 |
|
0 | 238 |
@property |
239 |
def new_state(self): |
|
240 |
return self.to_state[0] |
|
241 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
242 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
243 |
def transition(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
244 |
return self.by_transition and self.by_transition[0] or None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
245 |
|
0 | 246 |
def after_deletion_path(self): |
247 |
"""return (path, parameters) which should be used as redirect |
|
248 |
information when this entity is being deleted |
|
249 |
""" |
|
250 |
if self.for_entity: |
|
251 |
return self.for_entity.rest_path(), {} |
|
252 |
return 'view', {} |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
253 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
254 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
255 |
class WorkflowableMixIn(object): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
256 |
"""base mixin providing workflow helper methods for workflowable entities. |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
257 |
This mixin will be automatically set on class supporting the 'in_state' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
258 |
relation (which implies supporting 'wf_info_for' as well) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
259 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
260 |
__implements__ = (IWorkflowable,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
261 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
262 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
263 |
@cached |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
264 |
def current_workflow(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
265 |
"""return current workflow applied to this entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
266 |
if self.custom_workflow: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
267 |
return self.custom_workflow[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
268 |
wfrset = self.req.execute('Any WF WHERE X is ET, X eid %(x)s, WF workflow_of ET', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
269 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
270 |
if len(wfrset) == 1: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
271 |
return wfrset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
272 |
if len(wfrset) > 1: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
273 |
for wf in wfrset.entities(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
274 |
if wf.is_default_workflow_of(self.id): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
275 |
return wf |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
276 |
self.warning("can't find default workflow for %s", self.id) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
277 |
else: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
278 |
self.warning("can't find any workflow for %s", self.id) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
279 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
280 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
281 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
282 |
def current_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
283 |
"""return current state entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
284 |
return self.in_state and self.in_state[0] or None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
285 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
286 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
287 |
def state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
288 |
"""return current state name""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
289 |
try: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
290 |
return self.in_state[0].name |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
291 |
except IndexError: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
292 |
self.warning('entity %s has no state', self) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
293 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
294 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
295 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
296 |
def printable_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
297 |
"""return current state name translated to context's language""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
298 |
state = self.current_state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
299 |
if state: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
300 |
return self.req._(state.name) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
301 |
return u'' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
302 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
303 |
def latest_trinfo(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
304 |
"""return the latest transition information for this entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
305 |
return self.reverse_wf_info_for[-1] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
306 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
307 |
def possible_transitions(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
308 |
"""generates transition that MAY be fired for the given entity, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
309 |
expected to be in this state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
310 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
311 |
if self.current_state is None or self.current_workflow is None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
312 |
return |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
313 |
rset = self.req.execute( |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
314 |
'Any T,N WHERE S allowed_transition T, S eid %(x)s, ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
315 |
'T name N, T transition_of WF, WF eid %(wfeid)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
316 |
{'x': self.current_state.eid, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
317 |
'wfeid': self.current_workflow.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
318 |
for tr in rset.entities(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
319 |
if tr.may_be_fired(self.eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
320 |
yield tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
321 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
322 |
def _get_tr_kwargs(self, comment, commentformat): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
323 |
kwargs = {} |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
324 |
if comment is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
325 |
kwargs['comment'] = comment |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
326 |
if commentformat is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
327 |
kwargs['comment_format'] = commentformat |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
328 |
return kwargs |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
329 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
330 |
def fire_transition(self, trname, comment=None, commentformat=None): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
331 |
"""change the entity's state by firing transition of the given name in |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
332 |
entity's workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
333 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
334 |
assert self.current_workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
335 |
tr = self.current_workflow.transition_by_name(trname) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
336 |
assert tr is not None, 'not a %s transition: %s' % (self.id, state) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
337 |
# XXX try to find matching transition? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
338 |
self.req.create_entity('TrInfo', ('by_transition', 'T'), |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
339 |
('wf_info_for', 'E'), T=tr.eid, E=self.eid, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
340 |
**self._get_tr_kwargs(comment, commentformat)) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
341 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
342 |
def change_state(self, statename, comment=None, commentformat=None): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
343 |
"""change the entity's state to the state of the given name in entity's |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
344 |
workflow. This method should only by used by manager to fix an entity's |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
345 |
state when their is no matching transition, otherwise fire_transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
346 |
should be used. |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
347 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
348 |
assert self.current_workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
349 |
if not isinstance(statename, basestring): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
350 |
warn('give a state name') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
351 |
state = self.current_workflow.state_by_eid(statename) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
352 |
assert state is not None, 'not a %s state: %s' % (self.id, state) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
353 |
else: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
354 |
state = self.current_workflow.state_by_name(statename) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
355 |
# XXX try to find matching transition? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
356 |
self.req.create_entity('TrInfo', ('to_state', 'S'), |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
357 |
('wf_info_for', 'E'), S=state.eid, E=self.eid, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
358 |
**self._get_tr_kwargs(comment, commentformat)) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
359 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
360 |
@deprecated('get transition from current workflow and use its may_be_fired method') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
361 |
def can_pass_transition(self, trname): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
362 |
"""return the Transition instance if the current user can fire the |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
363 |
transition with the given name, else None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
364 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
365 |
tr = self.current_workflow and self.current_workflow.transition_by_name(trname) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
366 |
if tr and tr.may_be_fired(self.eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
367 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
368 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
369 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
370 |
@deprecated('use printable_state') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
371 |
def displayable_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
372 |
return self.req._(self.state) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
373 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
374 |
MI_REL_TRIGGERS[('in_state', 'subject')] = WorkflowableMixIn |