author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 12 Oct 2009 13:13:11 +0200 | |
branch | stable |
changeset 3642 | 8bc800f8b173 |
parent 3628 | 440931181322 |
child 3662 | 9eeadad82d93 |
permissions | -rw-r--r-- |
0 | 1 |
"""Core hooks: check schema validity, unsure we are not deleting necessary |
2 |
entities... |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
11 |
from datetime import datetime |
0 | 12 |
|
13 |
from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
14 |
from cubicweb.schema import RQLVocabularyConstraint |
0 | 15 |
from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
16 |
from cubicweb.server.hookhelper import (check_internal_entity, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
17 |
get_user_sessions, rproperty) |
0 | 18 |
from cubicweb.server.repository import FTIndexEntityOp |
19 |
||
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
20 |
# special relations that don't have to be checked for integrity, usually |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
21 |
# because they are handled internally by hooks (so we trust ourselves) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
22 |
DONT_CHECK_RTYPES_ON_ADD = set(('owned_by', 'created_by', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
23 |
'is', 'is_instance_of', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
24 |
'wf_info_for', 'from_state', 'to_state')) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
25 |
DONT_CHECK_RTYPES_ON_DEL = set(('is', 'is_instance_of', |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
26 |
'wf_info_for', 'from_state', 'to_state')) |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
27 |
|
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
28 |
|
0 | 29 |
def relation_deleted(session, eidfrom, rtype, eidto): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
30 |
session.transaction_data.setdefault('pendingrelations', []).append( |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
31 |
(eidfrom, rtype, eidto)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
32 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
33 |
def eschema_type_eid(session, etype): |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
34 |
"""get eid of the CWEType entity for the given yams type""" |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
35 |
eschema = session.repo.schema.eschema(etype) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
36 |
# eschema.eid is None if schema has been readen from the filesystem, not |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
37 |
# from the database (eg during tests) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
38 |
if eschema.eid is None: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
39 |
eschema.eid = session.unsafe_execute( |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
40 |
'Any X WHERE X is CWEType, X name %(name)s', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
41 |
{'name': str(etype)})[0][0] |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
42 |
return eschema.eid |
0 | 43 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
44 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
45 |
# base meta-data handling ###################################################### |
0 | 46 |
|
47 |
def setctime_before_add_entity(session, entity): |
|
48 |
"""before create a new entity -> set creation and modification date |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
49 |
|
0 | 50 |
this is a conveniency hook, you shouldn't have to disable it |
51 |
""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
52 |
timestamp = datetime.now() |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
53 |
entity.setdefault('creation_date', timestamp) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
54 |
entity.setdefault('modification_date', timestamp) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
55 |
if not session.get_shared_data('do-not-insert-cwuri'): |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
56 |
entity.setdefault('cwuri', u'%seid/%s' % (session.base_url(), entity.eid)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
57 |
|
0 | 58 |
|
59 |
def setmtime_before_update_entity(session, entity): |
|
60 |
"""update an entity -> set modification date""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
61 |
entity.setdefault('modification_date', datetime.now()) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
62 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
63 |
|
0 | 64 |
class SetCreatorOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
65 |
|
0 | 66 |
def precommit_event(self): |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
67 |
session = self.session |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
68 |
if self.entity.eid in session.transaction_data.get('pendingeids', ()): |
0 | 69 |
# entity have been created and deleted in the same transaction |
70 |
return |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
71 |
if not self.entity.created_by: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
72 |
session.add_relation(self.entity.eid, 'created_by', session.user.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
73 |
|
0 | 74 |
|
75 |
def setowner_after_add_entity(session, entity): |
|
76 |
"""create a new entity -> set owner and creator metadata""" |
|
77 |
asession = session.actual_session() |
|
78 |
if not asession.is_internal_session: |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
79 |
session.add_relation(entity.eid, 'owned_by', asession.user.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
80 |
SetCreatorOp(asession, entity=entity) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
81 |
|
0 | 82 |
|
83 |
def setis_after_add_entity(session, entity): |
|
84 |
"""create a new entity -> set is relation""" |
|
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
85 |
if hasattr(entity, '_cw_recreating'): |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
86 |
return |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
87 |
try: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
88 |
session.add_relation(entity.eid, 'is', |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
89 |
eschema_type_eid(session, entity.id)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
90 |
except IndexError: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
91 |
# during schema serialization, skip |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
92 |
return |
0 | 93 |
# XXX < 2.50 bw compat |
94 |
if not session.get_shared_data('do-not-insert-is_instance_of'): |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
95 |
for etype in entity.e_schema.ancestors() + [entity.e_schema]: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
96 |
session.add_relation(entity.eid, 'is_instance_of', |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
97 |
eschema_type_eid(session, etype)) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
98 |
|
0 | 99 |
|
100 |
def setowner_after_add_user(session, entity): |
|
101 |
"""when a user has been created, add owned_by relation on itself""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
102 |
session.add_relation(entity.eid, 'owned_by', entity.eid) |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
103 |
|
0 | 104 |
|
105 |
def fti_update_after_add_relation(session, eidfrom, rtype, eidto): |
|
106 |
"""sync fulltext index when relevant relation is added. Reindexing the |
|
107 |
contained entity is enough since it will implicitly reindex the container |
|
108 |
entity. |
|
109 |
""" |
|
110 |
ftcontainer = session.repo.schema.rschema(rtype).fulltext_container |
|
111 |
if ftcontainer == 'subject': |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
112 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidto)) |
0 | 113 |
elif ftcontainer == 'object': |
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
114 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom)) |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
115 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
116 |
|
0 | 117 |
def fti_update_after_delete_relation(session, eidfrom, rtype, eidto): |
118 |
"""sync fulltext index when relevant relation is deleted. Reindexing both |
|
119 |
entities is necessary. |
|
120 |
""" |
|
121 |
if session.repo.schema.rschema(rtype).fulltext_container: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
122 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidto)) |
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
123 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
124 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
125 |
|
0 | 126 |
class SyncOwnersOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
127 |
|
0 | 128 |
def precommit_event(self): |
129 |
self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,' |
|
130 |
'NOT EXISTS(X owned_by U, X eid %(x)s)', |
|
131 |
{'c': self.compositeeid, 'x': self.composedeid}, |
|
132 |
('c', 'x')) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
133 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
134 |
|
0 | 135 |
def sync_owner_after_add_composite_relation(session, eidfrom, rtype, eidto): |
136 |
"""when adding composite relation, the composed should have the same owners |
|
137 |
has the composite |
|
138 |
""" |
|
139 |
if rtype == 'wf_info_for': |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
140 |
# skip this special composite relation # XXX (syt) why? |
0 | 141 |
return |
142 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
143 |
if composite == 'subject': |
|
144 |
SyncOwnersOp(session, compositeeid=eidfrom, composedeid=eidto) |
|
145 |
elif composite == 'object': |
|
146 |
SyncOwnersOp(session, compositeeid=eidto, composedeid=eidfrom) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
147 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
148 |
|
0 | 149 |
def _register_metadata_hooks(hm): |
150 |
"""register meta-data related hooks on the hooks manager""" |
|
151 |
hm.register_hook(setctime_before_add_entity, 'before_add_entity', '') |
|
152 |
hm.register_hook(setmtime_before_update_entity, 'before_update_entity', '') |
|
153 |
hm.register_hook(setowner_after_add_entity, 'after_add_entity', '') |
|
154 |
hm.register_hook(sync_owner_after_add_composite_relation, 'after_add_relation', '') |
|
155 |
hm.register_hook(fti_update_after_add_relation, 'after_add_relation', '') |
|
156 |
hm.register_hook(fti_update_after_delete_relation, 'after_delete_relation', '') |
|
157 |
if 'is' in hm.schema: |
|
158 |
hm.register_hook(setis_after_add_entity, 'after_add_entity', '') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
159 |
if 'CWUser' in hm.schema: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
160 |
hm.register_hook(setowner_after_add_user, 'after_add_entity', 'CWUser') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
161 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
162 |
|
0 | 163 |
# core hooks ################################################################## |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
164 |
|
0 | 165 |
class DelayedDeleteOp(PreCommitOperation): |
166 |
"""delete the object of composite relation except if the relation |
|
167 |
has actually been redirected to another composite |
|
168 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
169 |
|
0 | 170 |
def precommit_event(self): |
171 |
session = self.session |
|
2607
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
172 |
# don't do anything if the entity is being created or deleted |
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
173 |
if not (self.eid in session.transaction_data.get('pendingeids', ()) or |
5d889b4928bb
[F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2606
diff
changeset
|
174 |
self.eid in session.transaction_data.get('neweids', ())): |
0 | 175 |
etype = session.describe(self.eid)[0] |
3568
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
176 |
if self.role == 'subject': |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
177 |
rql = 'DELETE %s X WHERE X eid %%(x)s, NOT X %s Y' |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
178 |
else: # self.role == 'object': |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
179 |
rql = 'DELETE %s X WHERE X eid %%(x)s, NOT Y %s X' |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
180 |
session.unsafe_execute(rql % (etype, self.rtype), {'x': self.eid}, 'x') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
181 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
182 |
|
0 | 183 |
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto): |
184 |
"""delete the object of composite relation""" |
|
3568
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
185 |
# if the relation is being delete, don't delete composite's components |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
186 |
# automatically |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
187 |
pendingrdefs = session.transaction_data.get('pendingrdefs', ()) |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
188 |
if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs: |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
189 |
return |
0 | 190 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
191 |
if composite == 'subject': |
|
3568
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
192 |
DelayedDeleteOp(session, eid=eidto, rtype=rtype, role='object') |
0 | 193 |
elif composite == 'object': |
3568
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
194 |
DelayedDeleteOp(session, eid=eidfrom, rtype=rtype, role='subject') |
0 | 195 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
196 |
|
0 | 197 |
def before_del_group(session, eid): |
198 |
"""check that we don't remove the owners group""" |
|
199 |
check_internal_entity(session, eid, ('owners',)) |
|
200 |
||
201 |
||
202 |
# schema validation hooks ##################################################### |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
203 |
|
0 | 204 |
class CheckConstraintsOperation(LateOperation): |
205 |
"""check a new relation satisfy its constraints |
|
206 |
""" |
|
207 |
def precommit_event(self): |
|
208 |
eidfrom, rtype, eidto = self.rdef |
|
209 |
# first check related entities have not been deleted in the same |
|
210 |
# transaction |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
211 |
pending = self.session.transaction_data.get('pendingeids', ()) |
0 | 212 |
if eidfrom in pending: |
213 |
return |
|
214 |
if eidto in pending: |
|
215 |
return |
|
216 |
for constraint in self.constraints: |
|
217 |
try: |
|
218 |
constraint.repo_check(self.session, eidfrom, rtype, eidto) |
|
219 |
except NotImplementedError: |
|
220 |
self.critical('can\'t check constraint %s, not supported', |
|
221 |
constraint) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
222 |
|
0 | 223 |
def commit_event(self): |
224 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
225 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
226 |
|
0 | 227 |
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto): |
228 |
"""check the relation satisfy its constraints |
|
229 |
||
230 |
this is delayed to a precommit time operation since other relation which |
|
231 |
will make constraint satisfied may be added later. |
|
232 |
""" |
|
233 |
constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
|
234 |
if constraints: |
|
235 |
CheckConstraintsOperation(session, constraints=constraints, |
|
236 |
rdef=(eidfrom, rtype, eidto)) |
|
237 |
||
238 |
def uniquecstrcheck_before_modification(session, entity): |
|
239 |
eschema = entity.e_schema |
|
240 |
for attr, val in entity.items(): |
|
241 |
if val is None: |
|
242 |
continue |
|
243 |
if eschema.subject_relation(attr).is_final() and \ |
|
244 |
eschema.has_unique_values(attr): |
|
245 |
rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr) |
|
246 |
rset = session.unsafe_execute(rql, {'val': val}) |
|
247 |
if rset and rset[0][0] != entity.eid: |
|
248 |
msg = session._('the value "%s" is already used, use another one') |
|
249 |
raise ValidationError(entity.eid, {attr: msg % val}) |
|
250 |
||
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
251 |
def cstrcheck_after_update_attributes(session, entity): |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
252 |
schema = session.vreg.schema |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
253 |
for attr in entity.edited_attributes: |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
254 |
if schema.rschema(attr).is_final(): |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
255 |
constraints = [c for c in entity.e_schema.constraints(attr) |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
256 |
if isinstance(c, RQLVocabularyConstraint)] |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
257 |
if constraints: |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
258 |
CheckConstraintsOperation(session, rdef=(entity.eid, attr, None), |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
259 |
constraints=constraints) |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
260 |
|
0 | 261 |
|
262 |
class CheckRequiredRelationOperation(LateOperation): |
|
263 |
"""checking relation cardinality has to be done after commit in |
|
264 |
case the relation is being replaced |
|
265 |
""" |
|
266 |
eid, rtype = None, None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
267 |
|
0 | 268 |
def precommit_event(self): |
269 |
# recheck pending eids |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
270 |
if self.eid in self.session.transaction_data.get('pendingeids', ()): |
0 | 271 |
return |
272 |
if self.session.unsafe_execute(*self._rql()).rowcount < 1: |
|
273 |
etype = self.session.describe(self.eid)[0] |
|
2241
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
274 |
_ = self.session._ |
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
275 |
msg = _('at least one relation %(rtype)s is required on %(etype)s (%(eid)s)') |
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
276 |
msg %= {'rtype': _(self.rtype), 'etype': _(etype), 'eid': self.eid} |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
277 |
raise ValidationError(self.eid, {self.rtype: msg}) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
278 |
|
0 | 279 |
def commit_event(self): |
280 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
281 |
|
0 | 282 |
def _rql(self): |
283 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
284 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
285 |
|
0 | 286 |
class CheckSRelationOp(CheckRequiredRelationOperation): |
287 |
"""check required subject relation""" |
|
288 |
def _rql(self): |
|
289 |
return 'Any O WHERE S eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
290 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
291 |
|
0 | 292 |
class CheckORelationOp(CheckRequiredRelationOperation): |
293 |
"""check required object relation""" |
|
294 |
def _rql(self): |
|
295 |
return 'Any S WHERE O eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
296 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
297 |
|
0 | 298 |
def checkrel_if_necessary(session, opcls, rtype, eid): |
299 |
"""check an equivalent operation has not already been added""" |
|
300 |
for op in session.pending_operations: |
|
301 |
if isinstance(op, opcls) and op.rtype == rtype and op.eid == eid: |
|
302 |
break |
|
303 |
else: |
|
304 |
opcls(session, rtype=rtype, eid=eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
305 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
306 |
|
0 | 307 |
def cardinalitycheck_after_add_entity(session, entity): |
308 |
"""check cardinalities are satisfied""" |
|
309 |
eid = entity.eid |
|
310 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
311 |
# skip automatically handled relations |
|
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
312 |
if rschema.type in DONT_CHECK_RTYPES_ON_ADD: |
0 | 313 |
continue |
314 |
if x == 'subject': |
|
315 |
subjtype = entity.e_schema |
|
316 |
objtype = targetschemas[0].type |
|
317 |
cardindex = 0 |
|
318 |
opcls = CheckSRelationOp |
|
319 |
else: |
|
320 |
subjtype = targetschemas[0].type |
|
321 |
objtype = entity.e_schema |
|
322 |
cardindex = 1 |
|
323 |
opcls = CheckORelationOp |
|
324 |
card = rschema.rproperty(subjtype, objtype, 'cardinality') |
|
325 |
if card[cardindex] in '1+': |
|
326 |
checkrel_if_necessary(session, opcls, rschema.type, eid) |
|
327 |
||
328 |
def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto): |
|
329 |
"""check cardinalities are satisfied""" |
|
2605
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
330 |
if rtype in DONT_CHECK_RTYPES_ON_DEL: |
c4f6a53884ec
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
331 |
return |
0 | 332 |
card = rproperty(session, rtype, eidfrom, eidto, 'cardinality') |
2745
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
333 |
pendingrdefs = session.transaction_data.get('pendingrdefs', ()) |
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
334 |
if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs: |
0dafa29ace1f
[schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2695
diff
changeset
|
335 |
return |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
336 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
0 | 337 |
if card[0] in '1+' and not eidfrom in pendingeids: |
338 |
checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom) |
|
339 |
if card[1] in '1+' and not eidto in pendingeids: |
|
340 |
checkrel_if_necessary(session, CheckORelationOp, rtype, eidto) |
|
341 |
||
342 |
||
343 |
def _register_core_hooks(hm): |
|
344 |
hm.register_hook(handle_composite_before_del_relation, 'before_delete_relation', '') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
345 |
hm.register_hook(before_del_group, 'before_delete_entity', 'CWGroup') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
346 |
|
0 | 347 |
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '') |
348 |
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
349 |
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
350 |
hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
351 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
352 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
353 |
hm.register_hook(cstrcheck_after_update_attributes, 'after_add_entity', '') |
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3528
diff
changeset
|
354 |
hm.register_hook(cstrcheck_after_update_attributes, 'after_update_entity', '') |
0 | 355 |
|
356 |
# user/groups synchronisation ################################################# |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
357 |
|
0 | 358 |
class GroupOperation(Operation): |
359 |
"""base class for group operation""" |
|
360 |
geid = None |
|
361 |
def __init__(self, session, *args, **kwargs): |
|
362 |
"""override to get the group name before actual groups manipulation: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
363 |
|
0 | 364 |
we may temporarily loose right access during a commit event, so |
365 |
no query should be emitted while comitting |
|
366 |
""" |
|
367 |
rql = 'Any N WHERE G eid %(x)s, G name N' |
|
368 |
result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False) |
|
369 |
Operation.__init__(self, session, *args, **kwargs) |
|
370 |
self.group = result[0][0] |
|
371 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
372 |
|
0 | 373 |
class DeleteGroupOp(GroupOperation): |
374 |
"""synchronize user when a in_group relation has been deleted""" |
|
375 |
def commit_event(self): |
|
376 |
"""the observed connections pool has been commited""" |
|
377 |
groups = self.cnxuser.groups |
|
378 |
try: |
|
379 |
groups.remove(self.group) |
|
380 |
except KeyError: |
|
381 |
self.error('user %s not in group %s', self.cnxuser, self.group) |
|
382 |
return |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
383 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
384 |
|
0 | 385 |
def after_del_in_group(session, fromeid, rtype, toeid): |
386 |
"""modify user permission, need to update users""" |
|
387 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
388 |
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
389 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
390 |
|
0 | 391 |
class AddGroupOp(GroupOperation): |
392 |
"""synchronize user when a in_group relation has been added""" |
|
393 |
def commit_event(self): |
|
394 |
"""the observed connections pool has been commited""" |
|
395 |
groups = self.cnxuser.groups |
|
396 |
if self.group in groups: |
|
397 |
self.warning('user %s already in group %s', self.cnxuser, |
|
398 |
self.group) |
|
399 |
return |
|
400 |
groups.add(self.group) |
|
401 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
402 |
|
0 | 403 |
def after_add_in_group(session, fromeid, rtype, toeid): |
404 |
"""modify user permission, need to update users""" |
|
405 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
406 |
AddGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
407 |
||
408 |
||
409 |
class DelUserOp(Operation): |
|
410 |
"""synchronize user when a in_group relation has been added""" |
|
411 |
def __init__(self, session, cnxid): |
|
412 |
self.cnxid = cnxid |
|
413 |
Operation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
414 |
|
0 | 415 |
def commit_event(self): |
416 |
"""the observed connections pool has been commited""" |
|
417 |
try: |
|
418 |
self.repo.close(self.cnxid) |
|
419 |
except BadConnectionId: |
|
420 |
pass # already closed |
|
421 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
422 |
|
0 | 423 |
def after_del_user(session, eid): |
424 |
"""modify user permission, need to update users""" |
|
425 |
for session_ in get_user_sessions(session.repo, eid): |
|
426 |
DelUserOp(session, session_.id) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
427 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
428 |
|
0 | 429 |
def _register_usergroup_hooks(hm): |
430 |
"""register user/group related hooks on the hooks manager""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
431 |
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser') |
0 | 432 |
hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group') |
433 |
hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group') |
|
434 |
||
435 |
||
436 |
# workflow handling ########################################################### |
|
437 |
||
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
438 |
from cubicweb.entities.wfobjs import WorkflowTransition, WorkflowException |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
439 |
|
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
440 |
def _change_state(session, x, oldstate, newstate): |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
441 |
nocheck = session.transaction_data.setdefault('skip-security', set()) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
442 |
nocheck.add((x, 'in_state', oldstate)) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
443 |
nocheck.add((x, 'in_state', newstate)) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
444 |
# delete previous state first in case we're using a super session |
3039
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
445 |
fromsource = session.describe(x)[1] |
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
446 |
# don't try to remove previous state if in_state isn't stored in the system |
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
447 |
# source |
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
448 |
if fromsource == 'system' or \ |
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
449 |
not session.repo.sources_by_uri[fromsource].support_relation('in_state'): |
7d5a4d27d052
[multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2992
diff
changeset
|
450 |
session.delete_relation(x, 'in_state', oldstate) |
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
451 |
session.add_relation(x, 'in_state', newstate) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
452 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
453 |
|
3528
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
454 |
class FireAutotransitionOp(PreCommitOperation): |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
455 |
"""try to fire auto transition after state changes""" |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
456 |
|
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
457 |
def precommit_event(self): |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
458 |
session = self.session |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
459 |
entity = self.entity |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
460 |
autotrs = list(entity.possible_transitions('auto')) |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
461 |
if autotrs: |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
462 |
assert len(autotrs) == 1 |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
463 |
entity.fire_transition(autotrs[0]) |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
464 |
|
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
465 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
466 |
def before_add_trinfo(session, entity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
467 |
"""check the transition is allowed, add missing information. Expect that: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
468 |
* wf_info_for inlined relation is set |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
469 |
* by_transition or to_state (managers only) inlined relation is set |
0 | 470 |
""" |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
471 |
# first retreive entity to which the state change apply |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
472 |
try: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
473 |
foreid = entity['wf_info_for'] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
474 |
except KeyError: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
475 |
msg = session._('mandatory relation') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
476 |
raise ValidationError(entity.eid, {'wf_info_for': msg}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
477 |
forentity = session.entity_from_eid(foreid) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
478 |
# then check it has a workflow set, unless we're in the process of changing |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
479 |
# entity's workflow |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
480 |
if session.transaction_data.get((forentity.eid, 'customwf')): |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
481 |
wfeid = session.transaction_data[(forentity.eid, 'customwf')] |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
482 |
wf = session.entity_from_eid(wfeid) |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
483 |
else: |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
484 |
wf = forentity.current_workflow |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
485 |
if wf is None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
486 |
msg = session._('related entity has no workflow set') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
487 |
raise ValidationError(entity.eid, {None: msg}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
488 |
# then check it has a state set |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
489 |
fromstate = forentity.current_state |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
490 |
if fromstate is None: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
491 |
msg = session._('related entity has no state') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
492 |
raise ValidationError(entity.eid, {None: msg}) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
493 |
# True if we are coming back from subworkflow |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
494 |
swtr = session.transaction_data.pop((forentity.eid, 'subwfentrytr'), None) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
495 |
cowpowers = session.is_super_session or 'managers' in session.user.groups |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
496 |
# no investigate the requested state change... |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
497 |
try: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
498 |
treid = entity['by_transition'] |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
499 |
except KeyError: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
500 |
# no transition set, check user is a manager and destination state is |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
501 |
# specified (and valid) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
502 |
if not cowpowers: |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
503 |
msg = session._('mandatory relation') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
504 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
505 |
deststateeid = entity.get('to_state') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
506 |
if not deststateeid: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
507 |
msg = session._('mandatory relation') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
508 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
509 |
deststate = wf.state_by_eid(deststateeid) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
510 |
if not cowpowers and deststate is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
511 |
msg = entity.req._("state doesn't belong to entity's workflow") |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
512 |
raise ValidationError(entity.eid, {'to_state': msg}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
513 |
else: |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
514 |
# check transition is valid and allowed, unless we're coming back from |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
515 |
# subworkflow |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
516 |
tr = session.entity_from_eid(treid) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
517 |
if swtr is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
518 |
if tr is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
519 |
msg = session._("transition doesn't belong to entity's workflow") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
520 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
521 |
if not tr.has_input_state(fromstate): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
522 |
msg = session._("transition isn't allowed") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
523 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
524 |
if not tr.may_be_fired(foreid): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
525 |
msg = session._("transition may not be fired") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
526 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
527 |
if entity.get('to_state'): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
528 |
deststateeid = entity['to_state'] |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
529 |
if not cowpowers and deststateeid != tr.destination().eid: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
530 |
msg = session._("transition isn't allowed") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
531 |
raise ValidationError(entity.eid, {'by_transition': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
532 |
if swtr is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
533 |
deststate = session.entity_from_eid(deststateeid) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
534 |
if not cowpowers and deststate is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
535 |
msg = entity.req._("state doesn't belong to entity's workflow") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
536 |
raise ValidationError(entity.eid, {'to_state': msg}) |
0 | 537 |
else: |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
538 |
deststateeid = tr.destination().eid |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
539 |
# everything is ok, add missing information on the trinfo entity |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
540 |
entity['from_state'] = fromstate.eid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
541 |
entity['to_state'] = deststateeid |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
542 |
nocheck = session.transaction_data.setdefault('skip-security', set()) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
543 |
nocheck.add((entity.eid, 'from_state', fromstate.eid)) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
544 |
nocheck.add((entity.eid, 'to_state', deststateeid)) |
3528
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
545 |
FireAutotransitionOp(session, entity=forentity) |
77a69de16709
support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3508
diff
changeset
|
546 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
547 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
548 |
def after_add_trinfo(session, entity): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
549 |
"""change related entity state""" |
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
550 |
_change_state(session, entity['wf_info_for'], |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
551 |
entity['from_state'], entity['to_state']) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
552 |
forentity = session.entity_from_eid(entity['wf_info_for']) |
3568
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
553 |
assert forentity.current_state.eid == entity['to_state'], ( |
87a7ca9d8ce6
[hooks] don't delete composed of a composite where relation is being removed from the schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
554 |
forentity.eid, forentity.current_state.name) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
555 |
if forentity.main_workflow.eid != forentity.current_workflow.eid: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
556 |
# we're in a subworkflow, check if we've reached an exit point |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
557 |
wftr = forentity.subworkflow_input_transition() |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
558 |
if wftr is None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
559 |
# inconsistency detected |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
560 |
msg = entity.req._("state doesn't belong to entity's current workflow") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
561 |
raise ValidationError(entity.eid, {'to_state': msg}) |
3628
440931181322
[wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3618
diff
changeset
|
562 |
tostate = wftr.get_exit_point(forentity, entity['to_state']) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
563 |
if tostate is not None: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
564 |
# reached an exit point |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
565 |
msg = session._('exiting from subworkflow %s') |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
566 |
msg %= session._(forentity.current_workflow.name) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
567 |
session.transaction_data[(forentity.eid, 'subwfentrytr')] = True |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
568 |
# XXX iirk |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
569 |
req = forentity.req |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
570 |
forentity.req = session.super_session |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
571 |
try: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
572 |
trinfo = forentity.change_state(tostate, msg, u'text/plain', |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
573 |
tr=wftr) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
574 |
finally: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
575 |
forentity.req = req |
0 | 576 |
|
577 |
||
578 |
class SetInitialStateOp(PreCommitOperation): |
|
579 |
"""make initial state be a default state""" |
|
580 |
||
581 |
def precommit_event(self): |
|
582 |
session = self.session |
|
583 |
entity = self.entity |
|
584 |
# if there is an initial state and the entity's state is not set, |
|
585 |
# use the initial state as a default state |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
586 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
587 |
if not entity.eid in pendingeids and not entity.in_state and \ |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
588 |
entity.main_workflow: |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
589 |
state = entity.main_workflow.initial |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
590 |
if state: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
591 |
# use super session to by-pass security checks |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
592 |
session.super_session.add_relation(entity.eid, 'in_state', |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
593 |
state.eid) |
0 | 594 |
|
595 |
||
596 |
def set_initial_state_after_add(session, entity): |
|
597 |
SetInitialStateOp(session, entity=entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
598 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
599 |
|
2983
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
600 |
def before_add_in_state(session, eidfrom, rtype, eidto): |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
601 |
"""check state apply, in case of direct in_state change using unsafe_execute |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
602 |
""" |
3508
d9915224f9a5
[B] server: fix initial data structure in session.transaction_data
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3071
diff
changeset
|
603 |
nocheck = session.transaction_data.setdefault('skip-security', set()) |
2983
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
604 |
if (eidfrom, 'in_state', eidto) in nocheck: |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
605 |
# state changed through TrInfo insertion, so we already know it's ok |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
606 |
return |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
607 |
entity = session.entity_from_eid(eidfrom) |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
608 |
mainwf = entity.main_workflow |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
609 |
if mainwf is None: |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
610 |
msg = session._('entity has no workflow set') |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
611 |
raise ValidationError(entity.eid, {None: msg}) |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
612 |
for wf in mainwf.iter_workflows(): |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
613 |
if wf.state_by_eid(eidto): |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
614 |
break |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
615 |
else: |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
616 |
msg = session._("state doesn't belong to entity's workflow. You may " |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
617 |
"want to set a custom workflow for this entity first.") |
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
618 |
raise ValidationError(eidfrom, {'in_state': msg}) |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
619 |
if entity.current_workflow and wf.eid != entity.current_workflow.eid: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
620 |
msg = session._("state doesn't belong to entity's current workflow") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
621 |
raise ValidationError(eidfrom, {'in_state': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
622 |
|
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
623 |
|
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
624 |
class CheckTrExitPoint(PreCommitOperation): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
625 |
|
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
626 |
def precommit_event(self): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
627 |
tr = self.session.entity_from_eid(self.treid) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
628 |
outputs = set() |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
629 |
for ep in tr.subworkflow_exit: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
630 |
if ep.subwf_state.eid in outputs: |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
631 |
msg = self.session._("can't have multiple exits on the same state") |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
632 |
raise ValidationError(self.treid, {'subworkflow_exit': msg}) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
633 |
outputs.add(ep.subwf_state.eid) |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
634 |
|
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
635 |
|
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
636 |
def after_add_subworkflow_exit(session, eidfrom, rtype, eidto): |
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
637 |
CheckTrExitPoint(session, treid=eidfrom) |
2983
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
638 |
|
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
639 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
640 |
class WorkflowChangedOp(PreCommitOperation): |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
641 |
"""fix entity current state when changing its workflow""" |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
642 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
643 |
def precommit_event(self): |
2985 | 644 |
# notice that enforcement that new workflow apply to the entity's type is |
645 |
# done by schema rule, no need to check it here |
|
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
646 |
session = self.session |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
647 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
648 |
if self.eid in pendingeids: |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
649 |
return |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
650 |
entity = session.entity_from_eid(self.eid) |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
651 |
# check custom workflow has not been rechanged to another one in the same |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
652 |
# transaction |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
653 |
mainwf = entity.main_workflow |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
654 |
if mainwf.eid == self.wfeid: |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
655 |
deststate = mainwf.initial |
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
656 |
if not deststate: |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
657 |
msg = session._('workflow has no initial state') |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
658 |
raise ValidationError(entity.eid, {'custom_workflow': msg}) |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
659 |
if mainwf.state_by_eid(entity.current_state.eid): |
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
660 |
# nothing to do |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
661 |
return |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
662 |
# if there are no history, simply go to new workflow's initial state |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
663 |
if not entity.workflow_history: |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
664 |
if entity.current_state.eid != deststate.eid: |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
665 |
_change_state(session, entity.eid, |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
666 |
entity.current_state.eid, deststate.eid) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
667 |
return |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
668 |
msg = session._('workflow changed to "%s"') |
2978
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
669 |
msg %= session._(mainwf.name) |
d8c5ad14ab8e
[wf]Â distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2949
diff
changeset
|
670 |
session.transaction_data[(entity.eid, 'customwf')] = self.wfeid |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
671 |
entity.change_state(deststate, msg, u'text/plain') |
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
672 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
673 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
674 |
def set_custom_workflow(session, eidfrom, rtype, eidto): |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
675 |
WorkflowChangedOp(session, eid=eidfrom, wfeid=eidto) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
676 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
677 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
678 |
def del_custom_workflow(session, eidfrom, rtype, eidto): |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
679 |
entity = session.entity_from_eid(eidfrom) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
680 |
typewf = entity.cwetype_workflow() |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
681 |
if typewf is not None: |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
682 |
WorkflowChangedOp(session, eid=eidfrom, wfeid=typewf.eid) |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
683 |
|
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
684 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
685 |
def after_del_workflow(session, eid): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
686 |
# workflow cleanup |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
687 |
session.execute('DELETE State X WHERE NOT X state_of Y') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
688 |
session.execute('DELETE Transition X WHERE NOT X transition_of Y') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
689 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
690 |
|
0 | 691 |
def _register_wf_hooks(hm): |
692 |
"""register workflow related hooks on the hooks manager""" |
|
693 |
if 'in_state' in hm.schema: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
694 |
hm.register_hook(before_add_trinfo, 'before_add_entity', 'TrInfo') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
695 |
hm.register_hook(after_add_trinfo, 'after_add_entity', 'TrInfo') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
696 |
#hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state') |
0 | 697 |
for eschema in hm.schema.entities(): |
698 |
if 'in_state' in eschema.subject_relations(): |
|
699 |
hm.register_hook(set_initial_state_after_add, 'after_add_entity', |
|
700 |
str(eschema)) |
|
2949
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
701 |
hm.register_hook(set_custom_workflow, 'after_add_relation', 'custom_workflow') |
a2aa2c51f3be
test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
702 |
hm.register_hook(del_custom_workflow, 'after_delete_relation', 'custom_workflow') |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2909
diff
changeset
|
703 |
hm.register_hook(after_del_workflow, 'after_delete_entity', 'Workflow') |
2983
b458631fe347
[wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2978
diff
changeset
|
704 |
hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state') |
2992
a5b8bf107a1a
[wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2985
diff
changeset
|
705 |
hm.register_hook(after_add_subworkflow_exit, 'after_add_relation', 'subworkflow_exit') |
0 | 706 |
|
707 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
708 |
# CWProperty hooks ############################################################# |
0 | 709 |
|
710 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
711 |
class DelCWPropertyOp(Operation): |
0 | 712 |
"""a user's custom properties has been deleted""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
713 |
|
0 | 714 |
def commit_event(self): |
715 |
"""the observed connections pool has been commited""" |
|
716 |
try: |
|
717 |
del self.epropdict[self.key] |
|
718 |
except KeyError: |
|
719 |
self.error('%s has no associated value', self.key) |
|
720 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
721 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
722 |
class ChangeCWPropertyOp(Operation): |
0 | 723 |
"""a user's custom properties has been added/changed""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
724 |
|
0 | 725 |
def commit_event(self): |
726 |
"""the observed connections pool has been commited""" |
|
727 |
self.epropdict[self.key] = self.value |
|
728 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
729 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
730 |
class AddCWPropertyOp(Operation): |
0 | 731 |
"""a user's custom properties has been added/changed""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
732 |
|
0 | 733 |
def commit_event(self): |
734 |
"""the observed connections pool has been commited""" |
|
735 |
eprop = self.eprop |
|
736 |
if not eprop.for_user: |
|
737 |
self.repo.vreg.eprop_values[eprop.pkey] = eprop.value |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
738 |
# if for_user is set, update is handled by a ChangeCWPropertyOp operation |
0 | 739 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
740 |
|
0 | 741 |
def after_add_eproperty(session, entity): |
742 |
key, value = entity.pkey, entity.value |
|
743 |
try: |
|
744 |
value = session.vreg.typed_value(key, value) |
|
745 |
except UnknownProperty: |
|
746 |
raise ValidationError(entity.eid, {'pkey': session._('unknown property key')}) |
|
747 |
except ValueError, ex: |
|
748 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
749 |
if not session.user.matching_groups('managers'): |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
750 |
session.add_relation(entity.eid, 'for_user', session.user.eid) |
0 | 751 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
752 |
AddCWPropertyOp(session, eprop=entity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
753 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
754 |
|
0 | 755 |
def after_update_eproperty(session, entity): |
2695
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
756 |
if not ('pkey' in entity.edited_attributes or |
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
757 |
'value' in entity.edited_attributes): |
56439c45781c
[hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2607
diff
changeset
|
758 |
return |
0 | 759 |
key, value = entity.pkey, entity.value |
760 |
try: |
|
761 |
value = session.vreg.typed_value(key, value) |
|
762 |
except UnknownProperty: |
|
763 |
return |
|
764 |
except ValueError, ex: |
|
765 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
766 |
if entity.for_user: |
|
767 |
for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
768 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 769 |
key=key, value=value) |
770 |
else: |
|
771 |
# site wide properties |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
772 |
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
0 | 773 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
774 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
775 |
|
0 | 776 |
def before_del_eproperty(session, eid): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
777 |
for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
0 | 778 |
if rtype == 'for_user' and eidfrom == eid: |
779 |
# if for_user was set, delete has already been handled |
|
780 |
break |
|
781 |
else: |
|
782 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
783 |
{'x': eid}, 'x')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
784 |
DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key) |
0 | 785 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
786 |
|
0 | 787 |
def after_add_for_user(session, fromeid, rtype, toeid): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
788 |
if not session.describe(fromeid)[0] == 'CWProperty': |
0 | 789 |
return |
790 |
key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
|
791 |
{'x': fromeid}, 'x')[0] |
|
792 |
if session.vreg.property_info(key)['sitewide']: |
|
793 |
raise ValidationError(fromeid, |
|
794 |
{'for_user': session._("site-wide property can't be set for user")}) |
|
795 |
for session_ in get_user_sessions(session.repo, toeid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
796 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 797 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
798 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
799 |
|
0 | 800 |
def before_del_for_user(session, fromeid, rtype, toeid): |
801 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
802 |
{'x': fromeid}, 'x')[0][0] |
|
803 |
relation_deleted(session, fromeid, rtype, toeid) |
|
804 |
for session_ in get_user_sessions(session.repo, toeid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
805 |
DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
0 | 806 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
807 |
|
0 | 808 |
def _register_eproperty_hooks(hm): |
809 |
"""register workflow related hooks on the hooks manager""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
810 |
hm.register_hook(after_add_eproperty, 'after_add_entity', 'CWProperty') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
811 |
hm.register_hook(after_update_eproperty, 'after_update_entity', 'CWProperty') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
812 |
hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty') |
0 | 813 |
hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user') |
814 |
hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user') |