author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 11:05:14 +0200 | |
changeset 3406 | e343f37f7013 |
parent 3405 | 9d31c9cb8103 |
child 3451 | 6b46d73823f5 |
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 |
|
2988
89d97d7c0508
[wf] use custom exception class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2987
diff
changeset
|
19 |
class WorkflowException(Exception): pass |
1572 | 20 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
21 |
class Workflow(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
22 |
__regid__ = 'Workflow' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
23 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
24 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
25 |
def initial(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
26 |
"""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
|
27 |
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
|
28 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
29 |
def is_default_workflow_of(self, etype): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
30 |
"""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
|
31 |
entity type |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
32 |
""" |
2943
77622caef9bd
[schema] default_workflow_of more naturally expressed as default_workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
33 |
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
|
34 |
if et.name == etype) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
35 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
36 |
def after_deletion_path(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
37 |
"""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
|
38 |
information when this entity is being deleted |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
39 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
40 |
if self.workflow_of: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
41 |
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
|
42 |
return super(Workflow, self).after_deletion_path() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
43 |
|
2976
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
44 |
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
|
45 |
"""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
|
46 |
subworkflows |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
47 |
""" |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
48 |
# infinite loop safety belt |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
49 |
if _done is None: |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
50 |
_done = set() |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
51 |
yield self |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
52 |
_done.add(self.eid) |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
53 |
for tr in self._cw.execute('Any T WHERE T is WorkflowTransition, ' |
2976
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
54 |
'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
|
55 |
{'wf': self.eid}).entities(): |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
56 |
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
|
57 |
continue |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
58 |
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
|
59 |
yield subwf |
d01417c14f35
[wf] new iter_worfklows method on workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2975
diff
changeset
|
60 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
61 |
# state / transitions accessors ############################################ |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
62 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
63 |
def state_by_name(self, statename): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
64 |
rset = self._cw.execute('Any S, SN WHERE S name SN, S name %(n)s, ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
65 |
'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
|
66 |
{'n': statename, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
67 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
68 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
69 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
70 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
71 |
def state_by_eid(self, eid): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
72 |
rset = self._cw.execute('Any S, SN WHERE S name SN, S eid %(s)s, ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
73 |
'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
|
74 |
{'s': eid, 'wf': self.eid}, ('wf', 's')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
75 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
76 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
77 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
78 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
79 |
def transition_by_name(self, trname): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
80 |
rset = self._cw.execute('Any T, TN WHERE T name TN, T name %(n)s, ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
81 |
'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
|
82 |
{'n': trname, 'wf': self.eid}, 'wf') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
83 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
84 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
85 |
return None |
0 | 86 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
87 |
def transition_by_eid(self, eid): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
88 |
rset = self._cw.execute('Any T, TN WHERE T name TN, T eid %(t)s, ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
89 |
'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
|
90 |
{'t': eid, 'wf': self.eid}, ('wf', 't')) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
91 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
92 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
93 |
return None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
94 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
95 |
# wf construction methods ################################################## |
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 |
def add_state(self, name, initial=False, **kwargs): |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
98 |
"""add a state to this workflow""" |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
99 |
state = self._cw.create_entity('State', name=unicode(name), **kwargs) |
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
100 |
self._cw.execute('SET S state_of WF WHERE S eid %(s)s, WF eid %(wf)s', |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
101 |
{'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
|
102 |
if initial: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
103 |
assert not self.initial |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
104 |
self._cw.execute('SET WF initial_state S ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
105 |
'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
|
106 |
{'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
|
107 |
return state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
108 |
|
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
109 |
def _add_transition(self, trtype, name, fromstates, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
110 |
requiredgroups=(), conditions=(), **kwargs): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
111 |
tr = self._cw.create_entity(trtype, name=unicode(name), **kwargs) |
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
112 |
self._cw.execute('SET T transition_of WF ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
113 |
'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
|
114 |
{'t': tr.eid, 'wf': self.eid}, ('t', 'wf')) |
2987
5082dec70ed8
[wf] ensure fromstates given, allow to give an entity (avoid spotless error, we can iter on entities...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2986
diff
changeset
|
115 |
assert fromstates, fromstates |
5082dec70ed8
[wf] ensure fromstates given, allow to give an entity (avoid spotless error, we can iter on entities...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2986
diff
changeset
|
116 |
if not isinstance(fromstates, (tuple, list)): |
5082dec70ed8
[wf] ensure fromstates given, allow to give an entity (avoid spotless error, we can iter on entities...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2986
diff
changeset
|
117 |
fromstates = (fromstates,) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
118 |
for state in fromstates: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
119 |
if hasattr(state, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
120 |
state = state.eid |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
121 |
self._cw.execute('SET S allowed_transition T ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
122 |
'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
|
123 |
{'s': state, 't': tr.eid}, ('s', 't')) |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
124 |
tr.set_transition_permissions(requiredgroups, conditions, reset=False) |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
125 |
return tr |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
126 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
127 |
def add_transition(self, name, fromstates, tostate, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
128 |
requiredgroups=(), conditions=(), **kwargs): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
129 |
"""add a transition to this workflow from some state(s) to another""" |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
130 |
tr = self._add_transition('Transition', name, fromstates, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
131 |
requiredgroups, conditions, **kwargs) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
132 |
if hasattr(tostate, 'eid'): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
133 |
tostate = tostate.eid |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
134 |
self._cw.execute('SET T destination_state S ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
135 |
'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
|
136 |
{'t': tr.eid, 's': tostate}, ('s', 't')) |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
137 |
return tr |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
138 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
139 |
def add_wftransition(self, name, subworkflow, fromstates, exitpoints, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
140 |
requiredgroups=(), conditions=(), **kwargs): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
141 |
"""add a workflow transition to this workflow""" |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
142 |
tr = self._add_transition('WorkflowTransition', name, fromstates, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
143 |
requiredgroups, conditions, **kwargs) |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
144 |
if hasattr(subworkflow, 'eid'): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
145 |
subworkflow = subworkflow.eid |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
146 |
self._cw.execute('SET T subworkflow WF WHERE WF eid %(wf)s,T eid %(t)s', |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
147 |
{'t': tr.eid, 'wf': subworkflow}, ('wf', 't')) |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
148 |
for fromstate, tostate in exitpoints: |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
149 |
tr.add_exit_point(fromstate, tostate) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
150 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
151 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
152 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
153 |
class BaseTransition(AnyEntity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
154 |
"""customized class for abstract transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
155 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
156 |
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
|
157 |
fired by the logged user |
0 | 158 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
159 |
__regid__ = 'BaseTransition' |
0 | 160 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1572 | 161 |
|
2951
d0b77dd27740
[wf] BaseTransition is a virtual type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2948
diff
changeset
|
162 |
def __init__(self, *args, **kwargs): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
163 |
if self.__regid__ == 'BaseTransition': |
2988
89d97d7c0508
[wf] use custom exception class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2987
diff
changeset
|
164 |
raise WorkflowException('should not be instantiated') |
2954
48507919b6e3
[wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2951
diff
changeset
|
165 |
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
|
166 |
|
2975
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
167 |
@property |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
168 |
def workflow(self): |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
169 |
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
|
170 |
|
2991 | 171 |
def has_input_state(self, state): |
172 |
if hasattr(state, 'eid'): |
|
173 |
state = state.eid |
|
174 |
return any(s for s in self.reverse_allowed_transition if s.eid == state) |
|
175 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
176 |
def may_be_fired(self, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
177 |
"""return true if the logged user may fire this transition |
0 | 178 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
179 |
`eid` is the eid of the object on which we may fire the transition |
0 | 180 |
""" |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
181 |
user = self._cw.user |
0 | 182 |
# check user is at least in one of the required groups if any |
183 |
groups = frozenset(g.name for g in self.require_group) |
|
184 |
if groups: |
|
185 |
matches = user.matching_groups(groups) |
|
186 |
if matches: |
|
187 |
return matches |
|
188 |
if 'owners' in groups and user.owns(eid): |
|
189 |
return True |
|
190 |
# check one of the rql expression conditions matches if any |
|
191 |
if self.condition: |
|
192 |
for rqlexpr in self.condition: |
|
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
193 |
if rqlexpr.check_expression(self._cw, eid): |
0 | 194 |
return True |
195 |
if self.condition or groups: |
|
196 |
return False |
|
197 |
return True |
|
198 |
||
199 |
def after_deletion_path(self): |
|
200 |
"""return (path, parameters) which should be used as redirect |
|
201 |
information when this entity is being deleted |
|
202 |
""" |
|
203 |
if self.transition_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
204 |
return self.transition_of[0].rest_path(), {} |
0 | 205 |
return super(Transition, self).after_deletion_path() |
206 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
207 |
def set_transition_permissions(self, requiredgroups=(), conditions=(), |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
208 |
reset=True): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
209 |
"""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
|
210 |
transition |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
211 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
212 |
if reset: |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
213 |
self._cw.execute('DELETE T require_group G WHERE T eid %(x)s', |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
214 |
{'x': self.eid}, 'x') |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
215 |
self._cw.execute('DELETE T condition R WHERE T eid %(x)s', |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
216 |
{'x': self.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
217 |
for gname in requiredgroups: |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
218 |
rset = self._cw.execute('SET T require_group G ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
219 |
'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
|
220 |
{'x': self.eid, 'gn': gname}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
221 |
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
|
222 |
if isinstance(conditions, basestring): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
223 |
conditions = (conditions,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
224 |
for expr in conditions: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
225 |
if isinstance(expr, str): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
226 |
expr = unicode(expr) |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
227 |
self._cw.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
228 |
'X expression %(expr)s, T condition X ' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
229 |
'WHERE T eid %(x)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
230 |
{'x': self.eid, 'expr': expr}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
231 |
# XXX clear caches? |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
232 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
233 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
234 |
class Transition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
235 |
"""customized class for Transition entities""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
236 |
__regid__ = 'Transition' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
237 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
238 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
239 |
return self.destination_state[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
240 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
241 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
242 |
class WorkflowTransition(BaseTransition): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
243 |
"""customized class for WorkflowTransition entities""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
244 |
__regid__ = 'WorkflowTransition' |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
245 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
246 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
247 |
def subwf(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
248 |
return self.subworkflow[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
249 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
250 |
def destination(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
251 |
return self.subwf.initial |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
252 |
|
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
253 |
def add_exit_point(self, fromstate, tostate): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
254 |
if hasattr(fromstate, 'eid'): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
255 |
fromstate = fromstate.eid |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
256 |
if hasattr(tostate, 'eid'): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
257 |
tostate = tostate.eid |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
258 |
self._cw.execute('INSERT SubWorkflowExitPoint X: T subworkflow_exit X, ' |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
259 |
'X subworkflow_state FS, X destination_state TS ' |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
260 |
'WHERE T eid %(t)s, FS eid %(fs)s, TS eid %(ts)s', |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
261 |
{'t': self.eid, 'fs': fromstate, 'ts': tostate}, |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
262 |
('t', 'fs', 'ts')) |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
263 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
264 |
def get_exit_point(self, state): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
265 |
"""if state is an exit point, return its associated destination state""" |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
266 |
if hasattr(state, 'eid'): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
267 |
state = state.eid |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
268 |
stateeid = self.exit_points().get(state) |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
269 |
if stateeid is not None: |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
270 |
return self._cw.entity_from_eid(stateeid) |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
271 |
return None |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
272 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
273 |
@cached |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
274 |
def exit_points(self): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
275 |
result = {} |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
276 |
for ep in self.subworkflow_exit: |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
277 |
result[ep.subwf_state.eid] = ep.destination.eid |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
278 |
return result |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
279 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
280 |
def clear_all_caches(self): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
281 |
super(WorkflowableMixIn, self).clear_all_caches() |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
282 |
clear_cache(self, 'exit_points') |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
283 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
284 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
285 |
class SubWorkflowExitPoint(AnyEntity): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
286 |
"""customized class for SubWorkflowExitPoint entities""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
287 |
__regid__ = 'SubWorkflowExitPoint' |
2986
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
288 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
289 |
@property |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
290 |
def subwf_state(self): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
291 |
return self.subworkflow_state[0] |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
292 |
|
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
293 |
@property |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
294 |
def destination(self): |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
295 |
return self.destination_state[0] |
d8f2e53702be
[wf] new methods to build wf w/ workflow transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2980
diff
changeset
|
296 |
|
1572 | 297 |
|
0 | 298 |
class State(AnyEntity): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
299 |
"""customized class for State entities""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
300 |
__regid__ = 'State' |
0 | 301 |
fetch_attrs, fetch_order = fetch_config(['name']) |
302 |
rest_attr = 'eid' |
|
1572 | 303 |
|
2975
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
304 |
@property |
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
305 |
def workflow(self): |
3038
1261c3df0e5a
[multi-sources] in ms config we don't have state_of (and probably others, but that's the one used until now...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2991
diff
changeset
|
306 |
# take care, may be missing in multi-sources configuration |
1261c3df0e5a
[multi-sources] in ms config we don't have state_of (and probably others, but that's the one used until now...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2991
diff
changeset
|
307 |
return self.state_of and self.state_of[0] |
2975
fed52d317ba7
[wf] new .workflow property on states and transitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2954
diff
changeset
|
308 |
|
0 | 309 |
def after_deletion_path(self): |
310 |
"""return (path, parameters) which should be used as redirect |
|
311 |
information when this entity is being deleted |
|
312 |
""" |
|
313 |
if self.state_of: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
314 |
return self.state_of[0].rest_path(), {} |
0 | 315 |
return super(State, self).after_deletion_path() |
316 |
||
1572 | 317 |
|
0 | 318 |
class TrInfo(AnyEntity): |
319 |
"""customized class for Transition information entities |
|
320 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3038
diff
changeset
|
321 |
__regid__ = 'TrInfo' |
0 | 322 |
fetch_attrs, fetch_order = fetch_config(['creation_date', 'comment'], |
323 |
pclass=None) # don't want modification_date |
|
324 |
@property |
|
325 |
def for_entity(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
326 |
return self.wf_info_for[0] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
327 |
|
0 | 328 |
@property |
329 |
def previous_state(self): |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
330 |
return self.from_state[0] |
1572 | 331 |
|
0 | 332 |
@property |
333 |
def new_state(self): |
|
334 |
return self.to_state[0] |
|
335 |
||
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
336 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
337 |
def transition(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
338 |
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
|
339 |
|
0 | 340 |
def after_deletion_path(self): |
341 |
"""return (path, parameters) which should be used as redirect |
|
342 |
information when this entity is being deleted |
|
343 |
""" |
|
344 |
if self.for_entity: |
|
345 |
return self.for_entity.rest_path(), {} |
|
346 |
return 'view', {} |
|
2920
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 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
349 |
class WorkflowableMixIn(object): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
350 |
"""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
|
351 |
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
|
352 |
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
|
353 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
354 |
__implements__ = (IWorkflowable,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
355 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
356 |
@property |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
357 |
def main_workflow(self): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
358 |
"""return current workflow applied to this entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
359 |
if self.custom_workflow: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
360 |
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
|
361 |
return self.cwetype_workflow() |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
362 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
363 |
@property |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
364 |
def current_workflow(self): |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
365 |
"""return current workflow applied to this entity""" |
2991 | 366 |
return self.current_state and self.current_state.workflow or self.main_workflow |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
367 |
|
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2976
diff
changeset
|
368 |
@property |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
369 |
def current_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
370 |
"""return current state entity""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
371 |
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
|
372 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
373 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
374 |
def state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
375 |
"""return current state name""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
376 |
try: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
377 |
return self.in_state[0].name |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
378 |
except IndexError: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
379 |
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
|
380 |
return None |
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 |
@property |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
383 |
def printable_state(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
384 |
"""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
|
385 |
state = self.current_state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
386 |
if state: |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
387 |
return self._cw._(state.name) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
388 |
return u'' |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
389 |
|
2947
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
390 |
@property |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
391 |
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
|
392 |
"""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
|
393 |
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
|
394 |
""" |
083593802120
[wf] new workflow_history property, nicer the reverse_wf_info_for...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2946
diff
changeset
|
395 |
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
|
396 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
397 |
def latest_trinfo(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
398 |
"""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
|
399 |
return self.reverse_wf_info_for[-1] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
400 |
|
2946
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
401 |
@cached |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
402 |
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
|
403 |
"""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
|
404 |
# XXX CWEType method |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
405 |
wfrset = self._cw.execute('Any WF WHERE X is ET, X eid %(x)s, ' |
2946
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
406 |
'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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
return wf |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
413 |
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
|
414 |
else: |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
415 |
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
|
416 |
return None |
957fd4b276e2
[wf] refactor current_workflow by extracting cwetype_workflow method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2945
diff
changeset
|
417 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
418 |
def possible_transitions(self): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
419 |
"""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
|
420 |
expected to be in this state |
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 |
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
|
423 |
return |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
424 |
rset = self._cw.execute( |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
425 |
'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
|
426 |
'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
|
427 |
{'x': self.current_state.eid, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
428 |
'wfeid': self.current_workflow.eid}, 'x') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
429 |
for tr in rset.entities(): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
430 |
if tr.may_be_fired(self.eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
431 |
yield tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
432 |
|
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
433 |
def _add_trinfo(self, comment, commentformat, treid=None, tseid=None): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
434 |
kwargs = {} |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
435 |
if comment is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
436 |
kwargs['comment'] = comment |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
437 |
if commentformat is not None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
438 |
kwargs['comment_format'] = commentformat |
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
439 |
args = [('wf_info_for', 'E')] |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
440 |
kwargs['E'] = self.eid |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
441 |
if treid is not None: |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
442 |
args.append( ('by_transition', 'T') ) |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
443 |
kwargs['T'] = treid |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
444 |
if tseid is not None: |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
445 |
args.append( ('to_state', 'S') ) |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
446 |
kwargs['S'] = tseid |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
447 |
return self._cw.create_entity('TrInfo', *args, **kwargs) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
448 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
449 |
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
|
450 |
"""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
|
451 |
entity's workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
452 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
453 |
assert self.current_workflow |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
454 |
tr = self.current_workflow.transition_by_name(trname) |
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
455 |
assert tr is not None, 'not a %s transition: %s' % (self.id, trname) |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
456 |
return self._add_trinfo(comment, commentformat, tr.eid) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
457 |
|
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
458 |
def change_state(self, statename, comment=None, commentformat=None, tr=None): |
2980
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
459 |
"""change the entity's state to the given state (name or entity) in |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
460 |
entity's workflow. This method should only by used by manager to fix an |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
461 |
entity's state when their is no matching transition, otherwise |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
462 |
fire_transition should be used. |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
463 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
464 |
assert self.current_workflow |
2980
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
465 |
if hasattr(statename, 'eid'): |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
466 |
stateeid = statename.eid |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
467 |
else: |
2980
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
468 |
if not isinstance(statename, basestring): |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
469 |
warn('give a state name') |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
470 |
state = self.current_workflow.state_by_eid(statename) |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
471 |
else: |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
472 |
state = self.current_workflow.state_by_name(statename) |
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
473 |
if state is None: |
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
474 |
raise WorkflowException('not a %s state: %s' % (self.id, |
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
475 |
statename)) |
2980
292387ed37f3
[wf] allow state entity as argument to change_state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
476 |
stateeid = state.eid |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
477 |
# XXX try to find matching transition? |
2989
dfb3506647e4
[wf] refactor change_state/fire_transition to share more code, change_state can also take an optional tr argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2988
diff
changeset
|
478 |
return self._add_trinfo(comment, commentformat, tr and tr.eid, stateeid) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
479 |
|
2990
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
480 |
def subworkflow_input_transition(self): |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
481 |
"""return the transition which has went through the current sub-workflow |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
482 |
""" |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
483 |
if self.main_workflow.eid == self.current_workflow.eid: |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
484 |
return # doesn't make sense |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
485 |
subwfentries = [] |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
486 |
for trinfo in reversed(self.workflow_history): |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
487 |
if (trinfo.transition and |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
488 |
trinfo.previous_state.workflow.eid != trinfo.new_state.workflow.eid): |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
489 |
# entering or leaving a subworkflow |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
490 |
if (subwfentries and |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
491 |
subwfentries[-1].new_state.workflow.eid == trinfo.previous_state.workflow.eid): |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
492 |
# leave |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
493 |
del subwfentries[-1] |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
494 |
else: |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
495 |
# enter |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
496 |
subwfentries.append(trinfo) |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
497 |
if not subwfentries: |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
498 |
return None |
f221ba8552ab
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2989
diff
changeset
|
499 |
return subwfentries[-1].transition |
2948
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
500 |
|
d3cd8bd20ee5
override clear_all_caches to clear cwetype_workflow cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2947
diff
changeset
|
501 |
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
|
502 |
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
|
503 |
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
|
504 |
|
3405
9d31c9cb8103
nicer deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
505 |
@deprecated('[3.5] get transition from current workflow and use its may_be_fired method') |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
506 |
def can_pass_transition(self, trname): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
507 |
"""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
|
508 |
transition with the given name, else None |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
509 |
""" |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
510 |
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
|
511 |
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
|
512 |
return tr |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
513 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
514 |
@property |
3405
9d31c9cb8103
nicer deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
515 |
@deprecated('[3.5] use printable_state') |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
516 |
def displayable_state(self): |
3406
e343f37f7013
use self._cw instead of self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3405
diff
changeset
|
517 |
return self._cw._(self.state) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
518 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2917
diff
changeset
|
519 |
MI_REL_TRIGGERS[('in_state', 'subject')] = WorkflowableMixIn |