author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 25 Aug 2009 12:38:37 +0200 | |
branch | 3.5 |
changeset 2978 | d8c5ad14ab8e |
parent 2976 | d01417c14f35 |
child 2980 | 292387ed37f3 |
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 |
|
2948
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
12 |
from logilab.common.decorators import cached, clear_cache |
2920
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""" |
2944
3bd49b70f7f9
[wf] return None, not empty list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
26 |
return self.initial_state and self.initial_state[0] or None |
2920
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 |
""" |
2943
77622caef9bd
[schema] default_workflow_of more naturally expressed as default_workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
32 |
return any(et for et in self.reverse_default_workflow |
77622caef9bd
[schema] default_workflow_of more naturally expressed as default_workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
33 |
if et.name == etype) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
34 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
35 |
def after_deletion_path(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
36 |
"""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
|
37 |
information when this entity is being deleted |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
38 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
39 |
if self.workflow_of: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
40 |
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
|
41 |
return super(Workflow, self).after_deletion_path() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
42 |
|
2976
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
43 |
def iter_workflows(self, _done=None): |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
44 |
"""return an iterator on actual workflows, eg this workflow and its |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
45 |
subworkflows |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
46 |
""" |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
47 |
# infinite loop safety belt |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
48 |
if _done is None: |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
49 |
_done = set() |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
50 |
yield self |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
51 |
_done.add(self.eid) |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
52 |
for tr in self.req.execute('Any T WHERE T is WorkflowTransition, ' |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
53 |
'T transition_of WF, WF eid %(wf)s', |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
54 |
{'wf': self.eid}).entities(): |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
55 |
if tr.subwf.eid in _done: |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
56 |
continue |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
57 |
for subwf in tr.subwf.iter_workflows(_done): |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
58 |
yield subwf |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
59 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
60 |
# state / transitions accessors ############################################ |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
61 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
62 |
def state_by_name(self, statename): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
63 |
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
|
64 |
'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
|
65 |
{'n': statename, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
66 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
67 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
68 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
69 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
70 |
def state_by_eid(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
71 |
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
|
72 |
'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
|
73 |
{'s': eid, 'wf': self.eid}, ('wf', 's')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
74 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
75 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
76 |
return None |
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 transition_by_name(self, trname): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
79 |
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
|
80 |
'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
|
81 |
{'n': trname, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
82 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
83 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
84 |
return None |
0 | 85 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
86 |
def transition_by_eid(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
87 |
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
|
88 |
'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
|
89 |
{'t': eid, 'wf': self.eid}, ('wf', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
90 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
91 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
92 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
93 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
94 |
# wf construction methods ################################################## |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
95 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
96 |
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
|
97 |
"""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
|
98 |
entity type(s) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
99 |
""" |
2945
f0ab83510477
[wf] allow ascii state/transition names
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2944
diff
changeset
|
100 |
state = self.req.create_entity('State', name=unicode(name), **kwargs) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
101 |
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
|
102 |
{'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
|
103 |
if initial: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
104 |
assert not self.initial |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
105 |
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
|
106 |
'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
|
107 |
{'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
|
108 |
return state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
109 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
110 |
def add_transition(self, name, fromstates, tostate, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
111 |
requiredgroups=(), conditions=(), **kwargs): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
112 |
"""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
|
113 |
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
|
114 |
""" |
2945
f0ab83510477
[wf] allow ascii state/transition names
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2944
diff
changeset
|
115 |
tr = self.req.create_entity('Transition', name=unicode(name), **kwargs) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
116 |
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
|
117 |
'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
|
118 |
{'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
|
119 |
for state in fromstates: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
120 |
if hasattr(state, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
121 |
state = state.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
122 |
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
|
123 |
'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
|
124 |
{'s': state, 't': tr.eid}, ('s', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
125 |
if hasattr(tostate, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
126 |
tostate = tostate.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
127 |
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
|
128 |
'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
|
129 |
{'t': tr.eid, 's': tostate}, ('s', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
130 |
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
|
131 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
132 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
133 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
134 |
class BaseTransition(AnyEntity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
135 |
"""customized class for abstract transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
136 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
137 |
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
|
138 |
fired by the logged user |
0 | 139 |
""" |
2951
d0b77dd27740
[wf] BaseTransition is a virtual type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2948
diff
changeset
|
140 |
id = 'BaseTransition' |
0 | 141 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1572 | 142 |
|
2951
d0b77dd27740
[wf] BaseTransition is a virtual type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2948
diff
changeset
|
143 |
def __init__(self, *args, **kwargs): |
2954
48507919b6e3
[wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2951
diff
changeset
|
144 |
if self.id == 'BaseTransition': |
48507919b6e3
[wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2951
diff
changeset
|
145 |
raise Exception('should not be instantiated') |
48507919b6e3
[wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2951
diff
changeset
|
146 |
super(BaseTransition, self).__init__(*args, **kwargs) |
2951
d0b77dd27740
[wf] BaseTransition is a virtual type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2948
diff
changeset
|
147 |
|
2975
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
148 |
@property |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
149 |
def workflow(self): |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
150 |
return self.transition_of[0] |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
151 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
152 |
def may_be_fired(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
153 |
"""return true if the logged user may fire this transition |
0 | 154 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
155 |
`eid` is the eid of the object on which we may fire the transition |
0 | 156 |
""" |
157 |
user = self.req.user |
|
158 |
# check user is at least in one of the required groups if any |
|
159 |
groups = frozenset(g.name for g in self.require_group) |
|
160 |
if groups: |
|
161 |
matches = user.matching_groups(groups) |
|
162 |
if matches: |
|
163 |
return matches |
|
164 |
if 'owners' in groups and user.owns(eid): |
|
165 |
return True |
|
166 |
# check one of the rql expression conditions matches if any |
|
167 |
if self.condition: |
|
168 |
for rqlexpr in self.condition: |
|
169 |
if rqlexpr.check_expression(self.req, eid): |
|
170 |
return True |
|
171 |
if self.condition or groups: |
|
172 |
return False |
|
173 |
return True |
|
174 |
||
175 |
def after_deletion_path(self): |
|
176 |
"""return (path, parameters) which should be used as redirect |
|
177 |
information when this entity is being deleted |
|
178 |
""" |
|
179 |
if self.transition_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
180 |
return self.transition_of[0].rest_path(), {} |
0 | 181 |
return super(Transition, self).after_deletion_path() |
182 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
183 |
def set_transition_permissions(self, requiredgroups=(), conditions=(), |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
184 |
reset=True): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
185 |
"""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
|
186 |
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 |
if reset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
189 |
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
|
190 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
191 |
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
|
192 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
193 |
for gname in requiredgroups: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
194 |
### XXX ensure gname validity |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
195 |
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
|
196 |
'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
|
197 |
{'x': self.eid, 'gn': gname}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
198 |
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
|
199 |
if isinstance(conditions, basestring): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
200 |
conditions = (conditions,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
201 |
for expr in conditions: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
202 |
if isinstance(expr, str): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
203 |
expr = unicode(expr) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
204 |
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
|
205 |
'X expression %(expr)s, T condition X ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
206 |
'WHERE T eid %(x)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
207 |
{'x': self.eid, 'expr': expr}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
208 |
# XXX clear caches? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
209 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
210 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
211 |
class Transition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
212 |
"""customized class for Transition entities""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
213 |
id = 'Transition' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
214 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
215 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
216 |
return self.destination_state[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
217 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
218 |
def has_input_state(self, state): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
219 |
if hasattr(state, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
220 |
state = state.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
221 |
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
|
222 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
223 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
224 |
class WorkflowTransition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
225 |
"""customized class for WorkflowTransition entities""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
226 |
id = 'WorkflowTransition' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
227 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
228 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
229 |
def subwf(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
230 |
return self.subworkflow[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
231 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
232 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
233 |
return self.subwf.initial |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
234 |
|
1572 | 235 |
|
0 | 236 |
class State(AnyEntity): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
237 |
"""customized class for State entities""" |
0 | 238 |
id = 'State' |
239 |
fetch_attrs, fetch_order = fetch_config(['name']) |
|
240 |
rest_attr = 'eid' |
|
1572 | 241 |
|
2975
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
242 |
@property |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
243 |
def workflow(self): |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
244 |
return self.state_of[0] |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
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.state_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
251 |
return self.state_of[0].rest_path(), {} |
0 | 252 |
return super(State, self).after_deletion_path() |
253 |
||
1572 | 254 |
|
0 | 255 |
class TrInfo(AnyEntity): |
256 |
"""customized class for Transition information entities |
|
257 |
""" |
|
258 |
id = 'TrInfo' |
|
259 |
fetch_attrs, fetch_order = fetch_config(['creation_date', 'comment'], |
|
260 |
pclass=None) # don't want modification_date |
|
261 |
@property |
|
262 |
def for_entity(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
263 |
return self.wf_info_for[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
264 |
|
0 | 265 |
@property |
266 |
def previous_state(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
267 |
return self.from_state[0] |
1572 | 268 |
|
0 | 269 |
@property |
270 |
def new_state(self): |
|
271 |
return self.to_state[0] |
|
272 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
273 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
274 |
def transition(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
275 |
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
|
276 |
|
0 | 277 |
def after_deletion_path(self): |
278 |
"""return (path, parameters) which should be used as redirect |
|
279 |
information when this entity is being deleted |
|
280 |
""" |
|
281 |
if self.for_entity: |
|
282 |
return self.for_entity.rest_path(), {} |
|
283 |
return 'view', {} |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
284 |
|
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 |
class WorkflowableMixIn(object): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
287 |
"""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
|
288 |
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
|
289 |
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
|
290 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
291 |
__implements__ = (IWorkflowable,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
292 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
293 |
@property |
2978
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
294 |
def main_workflow(self): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
295 |
"""return current workflow applied to this entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
296 |
if self.custom_workflow: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
297 |
return self.custom_workflow[0] |
2946
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
298 |
return self.cwetype_workflow() |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
299 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
300 |
@property |
2978
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
301 |
def current_workflow(self): |
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
302 |
"""return current workflow applied to this entity""" |
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
303 |
return self.current_state.workflow |
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
304 |
|
d8c5ad14ab8e
[wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
305 |
@property |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
306 |
def current_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
307 |
"""return current state entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
308 |
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
|
309 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
310 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
311 |
def state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
312 |
"""return current state name""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
313 |
try: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
314 |
return self.in_state[0].name |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
315 |
except IndexError: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
316 |
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
|
317 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
318 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
319 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
320 |
def printable_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
321 |
"""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
|
322 |
state = self.current_state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
323 |
if state: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
324 |
return self.req._(state.name) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
325 |
return u'' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
326 |
|
2947
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
327 |
@property |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
328 |
def workflow_history(self): |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
329 |
"""return the workflow history for this entity (eg ordered list of |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
330 |
TrInfo entities) |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
331 |
""" |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
332 |
return self.reverse_wf_info_for |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
333 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
334 |
def latest_trinfo(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
335 |
"""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
|
336 |
return self.reverse_wf_info_for[-1] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
337 |
|
2946
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
338 |
@cached |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
339 |
def cwetype_workflow(self): |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
340 |
"""return the default workflow for entities of this type""" |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
341 |
# XXX CWEType method |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
342 |
wfrset = self.req.execute('Any WF WHERE X is ET, X eid %(x)s, ' |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
343 |
'WF workflow_of ET', {'x': self.eid}, 'x') |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
344 |
if len(wfrset) == 1: |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
345 |
return wfrset.get_entity(0, 0) |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
346 |
if len(wfrset) > 1: |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
347 |
for wf in wfrset.entities(): |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
348 |
if wf.is_default_workflow_of(self.id): |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
349 |
return wf |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
350 |
self.warning("can't find default workflow for %s", self.id) |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
351 |
else: |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
352 |
self.warning("can't find any workflow for %s", self.id) |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
353 |
return None |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
354 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
355 |
def possible_transitions(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
356 |
"""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
|
357 |
expected to be in this state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
358 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
359 |
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
|
360 |
return |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
361 |
rset = self.req.execute( |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
362 |
'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
|
363 |
'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
|
364 |
{'x': self.current_state.eid, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
365 |
'wfeid': self.current_workflow.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
366 |
for tr in rset.entities(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
367 |
if tr.may_be_fired(self.eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
368 |
yield tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
369 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
370 |
def _get_tr_kwargs(self, comment, commentformat): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
371 |
kwargs = {} |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
372 |
if comment is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
373 |
kwargs['comment'] = comment |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
374 |
if commentformat is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
375 |
kwargs['comment_format'] = commentformat |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
376 |
return kwargs |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
377 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
378 |
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
|
379 |
"""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
|
380 |
entity's workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
381 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
382 |
assert self.current_workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
383 |
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
|
384 |
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
|
385 |
# XXX try to find matching transition? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
386 |
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
|
387 |
('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
|
388 |
**self._get_tr_kwargs(comment, commentformat)) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
389 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
390 |
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
|
391 |
"""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
|
392 |
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
|
393 |
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
|
394 |
should be used. |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
395 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
396 |
assert self.current_workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
397 |
if not isinstance(statename, basestring): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
398 |
warn('give a state name') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
399 |
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
|
400 |
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
|
401 |
else: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
402 |
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
|
403 |
# XXX try to find matching transition? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
404 |
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
|
405 |
('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
|
406 |
**self._get_tr_kwargs(comment, commentformat)) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
407 |
|
2948
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
408 |
|
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
409 |
def clear_all_caches(self): |
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
410 |
super(WorkflowableMixIn, self).clear_all_caches() |
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
411 |
clear_cache(self, 'cwetype_workflow') |
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
412 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
413 |
@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
|
414 |
def can_pass_transition(self, trname): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
415 |
"""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
|
416 |
transition with the given name, else None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
417 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
421 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
422 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
423 |
@deprecated('use printable_state') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
424 |
def displayable_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
425 |
return self.req._(self.state) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
426 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
427 |
MI_REL_TRIGGERS[('in_state', 'subject')] = WorkflowableMixIn |