author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 13 Aug 2009 10:20:57 +0200 | |
changeset 2799 | b703639614e7 |
parent 2784 | f395115070c1 |
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 |
|
14 |
||
15 |
from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation |
|
16 |
from cubicweb.server.hookhelper import (check_internal_entity, previous_state, |
|
17 |
get_user_sessions, rproperty) |
|
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( |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
40 |
'Any X WHERE X is CWEType, X name %(name)s', {'name': etype})[0][0] |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
41 |
return eschema.eid |
0 | 42 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
43 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
44 |
# base meta-data handling ###################################################### |
0 | 45 |
|
46 |
def setctime_before_add_entity(session, entity): |
|
47 |
"""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
|
48 |
|
0 | 49 |
this is a conveniency hook, you shouldn't have to disable it |
50 |
""" |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
|
0 | 57 |
|
58 |
def setmtime_before_update_entity(session, entity): |
|
59 |
"""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
|
60 |
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
|
61 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
62 |
|
0 | 63 |
class SetCreatorOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
64 |
|
0 | 65 |
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
|
66 |
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
|
67 |
if self.entity.eid in session.transaction_data.get('pendingeids', ()): |
0 | 68 |
# entity have been created and deleted in the same transaction |
69 |
return |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
70 |
if not self.entity.created_by: |
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 |
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
|
72 |
|
0 | 73 |
|
74 |
def setowner_after_add_entity(session, entity): |
|
75 |
"""create a new entity -> set owner and creator metadata""" |
|
76 |
asession = session.actual_session() |
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
|
0 | 81 |
|
82 |
def setis_after_add_entity(session, entity): |
|
83 |
"""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
|
84 |
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
|
85 |
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
|
86 |
try: |
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
# 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
|
91 |
return |
0 | 92 |
# XXX < 2.50 bw compat |
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
|
0 | 98 |
|
99 |
def setowner_after_add_user(session, entity): |
|
100 |
"""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
|
101 |
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
|
102 |
|
0 | 103 |
|
104 |
def fti_update_after_add_relation(session, eidfrom, rtype, eidto): |
|
105 |
"""sync fulltext index when relevant relation is added. Reindexing the |
|
106 |
contained entity is enough since it will implicitly reindex the container |
|
107 |
entity. |
|
108 |
""" |
|
109 |
ftcontainer = session.repo.schema.rschema(rtype).fulltext_container |
|
110 |
if ftcontainer == 'subject': |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
111 |
FTIndexEntityOp(session, entity=session.entity_from_eid(eidto)) |
0 | 112 |
elif ftcontainer == 'object': |
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
113 |
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
|
114 |
|
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
115 |
|
0 | 116 |
def fti_update_after_delete_relation(session, eidfrom, rtype, eidto): |
117 |
"""sync fulltext index when relevant relation is deleted. Reindexing both |
|
118 |
entities is necessary. |
|
119 |
""" |
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
124 |
|
0 | 125 |
class SyncOwnersOp(PreCommitOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
126 |
|
0 | 127 |
def precommit_event(self): |
128 |
self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,' |
|
129 |
'NOT EXISTS(X owned_by U, X eid %(x)s)', |
|
130 |
{'c': self.compositeeid, 'x': self.composedeid}, |
|
131 |
('c', 'x')) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
132 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
133 |
|
0 | 134 |
def sync_owner_after_add_composite_relation(session, eidfrom, rtype, eidto): |
135 |
"""when adding composite relation, the composed should have the same owners |
|
136 |
has the composite |
|
137 |
""" |
|
138 |
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
|
139 |
# skip this special composite relation # XXX (syt) why? |
0 | 140 |
return |
141 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
142 |
if composite == 'subject': |
|
143 |
SyncOwnersOp(session, compositeeid=eidfrom, composedeid=eidto) |
|
144 |
elif composite == 'object': |
|
145 |
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
|
146 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
147 |
|
0 | 148 |
def _register_metadata_hooks(hm): |
149 |
"""register meta-data related hooks on the hooks manager""" |
|
150 |
hm.register_hook(setctime_before_add_entity, 'before_add_entity', '') |
|
151 |
hm.register_hook(setmtime_before_update_entity, 'before_update_entity', '') |
|
152 |
hm.register_hook(setowner_after_add_entity, 'after_add_entity', '') |
|
153 |
hm.register_hook(sync_owner_after_add_composite_relation, 'after_add_relation', '') |
|
154 |
hm.register_hook(fti_update_after_add_relation, 'after_add_relation', '') |
|
155 |
hm.register_hook(fti_update_after_delete_relation, 'after_delete_relation', '') |
|
156 |
if 'is' in hm.schema: |
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
161 |
|
0 | 162 |
# core hooks ################################################################## |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
163 |
|
0 | 164 |
class DelayedDeleteOp(PreCommitOperation): |
165 |
"""delete the object of composite relation except if the relation |
|
166 |
has actually been redirected to another composite |
|
167 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
|
0 | 169 |
def precommit_event(self): |
170 |
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
|
171 |
# 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
|
172 |
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
|
173 |
self.eid in session.transaction_data.get('neweids', ())): |
0 | 174 |
etype = session.describe(self.eid)[0] |
175 |
session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s' |
|
176 |
% (etype, self.relation), |
|
177 |
{'x': self.eid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
178 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
179 |
|
0 | 180 |
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto): |
181 |
"""delete the object of composite relation""" |
|
182 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
183 |
if composite == 'subject': |
|
184 |
DelayedDeleteOp(session, eid=eidto, relation='Y %s X' % rtype) |
|
185 |
elif composite == 'object': |
|
186 |
DelayedDeleteOp(session, eid=eidfrom, relation='X %s Y' % rtype) |
|
187 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
188 |
|
0 | 189 |
def before_del_group(session, eid): |
190 |
"""check that we don't remove the owners group""" |
|
191 |
check_internal_entity(session, eid, ('owners',)) |
|
192 |
||
193 |
||
194 |
# schema validation hooks ##################################################### |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
195 |
|
0 | 196 |
class CheckConstraintsOperation(LateOperation): |
197 |
"""check a new relation satisfy its constraints |
|
198 |
""" |
|
199 |
def precommit_event(self): |
|
200 |
eidfrom, rtype, eidto = self.rdef |
|
201 |
# first check related entities have not been deleted in the same |
|
202 |
# transaction |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
203 |
pending = self.session.transaction_data.get('pendingeids', ()) |
0 | 204 |
if eidfrom in pending: |
205 |
return |
|
206 |
if eidto in pending: |
|
207 |
return |
|
208 |
for constraint in self.constraints: |
|
209 |
try: |
|
210 |
constraint.repo_check(self.session, eidfrom, rtype, eidto) |
|
211 |
except NotImplementedError: |
|
212 |
self.critical('can\'t check constraint %s, not supported', |
|
213 |
constraint) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
214 |
|
0 | 215 |
def commit_event(self): |
216 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
217 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
218 |
|
0 | 219 |
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto): |
220 |
"""check the relation satisfy its constraints |
|
221 |
||
222 |
this is delayed to a precommit time operation since other relation which |
|
223 |
will make constraint satisfied may be added later. |
|
224 |
""" |
|
225 |
constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
|
226 |
if constraints: |
|
227 |
CheckConstraintsOperation(session, constraints=constraints, |
|
228 |
rdef=(eidfrom, rtype, eidto)) |
|
229 |
||
230 |
def uniquecstrcheck_before_modification(session, entity): |
|
231 |
eschema = entity.e_schema |
|
232 |
for attr, val in entity.items(): |
|
233 |
if val is None: |
|
234 |
continue |
|
235 |
if eschema.subject_relation(attr).is_final() and \ |
|
236 |
eschema.has_unique_values(attr): |
|
237 |
rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr) |
|
238 |
rset = session.unsafe_execute(rql, {'val': val}) |
|
239 |
if rset and rset[0][0] != entity.eid: |
|
240 |
msg = session._('the value "%s" is already used, use another one') |
|
241 |
raise ValidationError(entity.eid, {attr: msg % val}) |
|
242 |
||
243 |
||
244 |
class CheckRequiredRelationOperation(LateOperation): |
|
245 |
"""checking relation cardinality has to be done after commit in |
|
246 |
case the relation is being replaced |
|
247 |
""" |
|
248 |
eid, rtype = None, None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
249 |
|
0 | 250 |
def precommit_event(self): |
251 |
# recheck pending eids |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
252 |
if self.eid in self.session.transaction_data.get('pendingeids', ()): |
0 | 253 |
return |
254 |
if self.session.unsafe_execute(*self._rql()).rowcount < 1: |
|
255 |
etype = self.session.describe(self.eid)[0] |
|
2241
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
256 |
_ = self.session._ |
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
|
0 | 261 |
def commit_event(self): |
262 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
263 |
|
0 | 264 |
def _rql(self): |
265 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
266 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
267 |
|
0 | 268 |
class CheckSRelationOp(CheckRequiredRelationOperation): |
269 |
"""check required subject relation""" |
|
270 |
def _rql(self): |
|
271 |
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
|
272 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
273 |
|
0 | 274 |
class CheckORelationOp(CheckRequiredRelationOperation): |
275 |
"""check required object relation""" |
|
276 |
def _rql(self): |
|
277 |
return 'Any S WHERE O eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
278 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
279 |
|
0 | 280 |
def checkrel_if_necessary(session, opcls, rtype, eid): |
281 |
"""check an equivalent operation has not already been added""" |
|
282 |
for op in session.pending_operations: |
|
283 |
if isinstance(op, opcls) and op.rtype == rtype and op.eid == eid: |
|
284 |
break |
|
285 |
else: |
|
286 |
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
|
287 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
288 |
|
0 | 289 |
def cardinalitycheck_after_add_entity(session, entity): |
290 |
"""check cardinalities are satisfied""" |
|
291 |
eid = entity.eid |
|
292 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
293 |
# 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
|
294 |
if rschema.type in DONT_CHECK_RTYPES_ON_ADD: |
0 | 295 |
continue |
296 |
if x == 'subject': |
|
297 |
subjtype = entity.e_schema |
|
298 |
objtype = targetschemas[0].type |
|
299 |
cardindex = 0 |
|
300 |
opcls = CheckSRelationOp |
|
301 |
else: |
|
302 |
subjtype = targetschemas[0].type |
|
303 |
objtype = entity.e_schema |
|
304 |
cardindex = 1 |
|
305 |
opcls = CheckORelationOp |
|
306 |
card = rschema.rproperty(subjtype, objtype, 'cardinality') |
|
307 |
if card[cardindex] in '1+': |
|
308 |
checkrel_if_necessary(session, opcls, rschema.type, eid) |
|
309 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
310 |
|
0 | 311 |
def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto): |
312 |
"""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
|
313 |
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
|
314 |
return |
0 | 315 |
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
|
316 |
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
|
317 |
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
|
318 |
return |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
319 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
0 | 320 |
if card[0] in '1+' and not eidfrom in pendingeids: |
321 |
checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom) |
|
322 |
if card[1] in '1+' and not eidto in pendingeids: |
|
323 |
checkrel_if_necessary(session, CheckORelationOp, rtype, eidto) |
|
324 |
||
325 |
||
326 |
def _register_core_hooks(hm): |
|
327 |
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
|
328 |
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
|
329 |
|
0 | 330 |
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '') |
331 |
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
332 |
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
333 |
hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
334 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
335 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
336 |
||
337 |
||
338 |
# user/groups synchronisation ################################################# |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
339 |
|
0 | 340 |
class GroupOperation(Operation): |
341 |
"""base class for group operation""" |
|
342 |
geid = None |
|
343 |
def __init__(self, session, *args, **kwargs): |
|
344 |
"""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
|
345 |
|
0 | 346 |
we may temporarily loose right access during a commit event, so |
347 |
no query should be emitted while comitting |
|
348 |
""" |
|
349 |
rql = 'Any N WHERE G eid %(x)s, G name N' |
|
350 |
result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False) |
|
351 |
Operation.__init__(self, session, *args, **kwargs) |
|
352 |
self.group = result[0][0] |
|
353 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
354 |
|
0 | 355 |
class DeleteGroupOp(GroupOperation): |
356 |
"""synchronize user when a in_group relation has been deleted""" |
|
357 |
def commit_event(self): |
|
358 |
"""the observed connections pool has been commited""" |
|
359 |
groups = self.cnxuser.groups |
|
360 |
try: |
|
361 |
groups.remove(self.group) |
|
362 |
except KeyError: |
|
363 |
self.error('user %s not in group %s', self.cnxuser, self.group) |
|
364 |
return |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
365 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
366 |
|
0 | 367 |
def after_del_in_group(session, fromeid, rtype, toeid): |
368 |
"""modify user permission, need to update users""" |
|
369 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
370 |
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
371 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
372 |
|
0 | 373 |
class AddGroupOp(GroupOperation): |
374 |
"""synchronize user when a in_group relation has been added""" |
|
375 |
def commit_event(self): |
|
376 |
"""the observed connections pool has been commited""" |
|
377 |
groups = self.cnxuser.groups |
|
378 |
if self.group in groups: |
|
379 |
self.warning('user %s already in group %s', self.cnxuser, |
|
380 |
self.group) |
|
381 |
return |
|
382 |
groups.add(self.group) |
|
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_add_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 |
AddGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
389 |
||
390 |
||
391 |
class DelUserOp(Operation): |
|
392 |
"""synchronize user when a in_group relation has been added""" |
|
393 |
def __init__(self, session, cnxid): |
|
394 |
self.cnxid = cnxid |
|
395 |
Operation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
396 |
|
0 | 397 |
def commit_event(self): |
398 |
"""the observed connections pool has been commited""" |
|
399 |
try: |
|
400 |
self.repo.close(self.cnxid) |
|
401 |
except BadConnectionId: |
|
402 |
pass # already closed |
|
403 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
404 |
|
0 | 405 |
def after_del_user(session, eid): |
406 |
"""modify user permission, need to update users""" |
|
407 |
for session_ in get_user_sessions(session.repo, eid): |
|
408 |
DelUserOp(session, session_.id) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
409 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
410 |
|
0 | 411 |
def _register_usergroup_hooks(hm): |
412 |
"""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
|
413 |
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser') |
0 | 414 |
hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group') |
415 |
hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group') |
|
416 |
||
417 |
||
418 |
# workflow handling ########################################################### |
|
419 |
||
420 |
def before_add_in_state(session, fromeid, rtype, toeid): |
|
421 |
"""check the transition is allowed and record transition information |
|
422 |
""" |
|
423 |
assert rtype == 'in_state' |
|
424 |
state = previous_state(session, fromeid) |
|
425 |
etype = session.describe(fromeid)[0] |
|
426 |
if not (session.is_super_session or 'managers' in session.user.groups): |
|
427 |
if not state is None: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
428 |
entity = session.entity_from_eid(fromeid) |
0 | 429 |
# we should find at least one transition going to this state |
430 |
try: |
|
431 |
iter(state.transitions(entity, toeid)).next() |
|
432 |
except StopIteration: |
|
433 |
msg = session._('transition is not allowed') |
|
434 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
435 |
else: |
|
436 |
# not a transition |
|
437 |
# check state is initial state if the workflow defines one |
|
438 |
isrset = session.unsafe_execute('Any S WHERE ET initial_state S, ET name %(etype)s', |
|
439 |
{'etype': etype}) |
|
440 |
if isrset and not toeid == isrset[0][0]: |
|
441 |
msg = session._('not the initial state for this entity') |
|
442 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
443 |
eschema = session.repo.schema[etype] |
|
444 |
if not 'wf_info_for' in eschema.object_relations(): |
|
445 |
# workflow history not activated for this entity type |
|
446 |
return |
|
447 |
rql = 'INSERT TrInfo T: T wf_info_for E, T to_state DS, T comment %(comment)s' |
|
448 |
args = {'comment': session.get_shared_data('trcomment', None, pop=True), |
|
449 |
'e': fromeid, 'ds': toeid} |
|
450 |
cformat = session.get_shared_data('trcommentformat', None, pop=True) |
|
451 |
if cformat is not None: |
|
452 |
args['comment_format'] = cformat |
|
453 |
rql += ', T comment_format %(comment_format)s' |
|
454 |
restriction = ['DS eid %(ds)s, E eid %(e)s'] |
|
455 |
if not state is None: # not a transition |
|
456 |
rql += ', T from_state FS' |
|
457 |
restriction.append('FS eid %(fs)s') |
|
458 |
args['fs'] = state.eid |
|
459 |
rql = '%s WHERE %s' % (rql, ', '.join(restriction)) |
|
460 |
session.unsafe_execute(rql, args, 'e') |
|
461 |
||
462 |
||
463 |
class SetInitialStateOp(PreCommitOperation): |
|
464 |
"""make initial state be a default state""" |
|
465 |
||
466 |
def precommit_event(self): |
|
467 |
session = self.session |
|
468 |
entity = self.entity |
|
469 |
# if there is an initial state and the entity's state is not set, |
|
470 |
# 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
|
471 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
2606
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
472 |
if not entity.eid in pendingeids and not entity.in_state: |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
473 |
rset = session.execute('Any S WHERE ET initial_state S, ET name %(name)s', |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
474 |
{'name': entity.id}) |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
475 |
if rset: |
598ac51cac04
[R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] don't compute rset if not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2605
diff
changeset
|
476 |
session.add_relation(entity.eid, 'in_state', rset[0][0]) |
0 | 477 |
|
478 |
||
479 |
def set_initial_state_after_add(session, entity): |
|
480 |
SetInitialStateOp(session, entity=entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
481 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
482 |
|
0 | 483 |
def _register_wf_hooks(hm): |
484 |
"""register workflow related hooks on the hooks manager""" |
|
485 |
if 'in_state' in hm.schema: |
|
486 |
hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state') |
|
487 |
hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state') |
|
488 |
for eschema in hm.schema.entities(): |
|
489 |
if 'in_state' in eschema.subject_relations(): |
|
490 |
hm.register_hook(set_initial_state_after_add, 'after_add_entity', |
|
491 |
str(eschema)) |
|
492 |
||
493 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
494 |
# CWProperty hooks ############################################################# |
0 | 495 |
|
496 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
497 |
class DelCWPropertyOp(Operation): |
0 | 498 |
"""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
|
499 |
|
0 | 500 |
def commit_event(self): |
501 |
"""the observed connections pool has been commited""" |
|
502 |
try: |
|
503 |
del self.epropdict[self.key] |
|
504 |
except KeyError: |
|
505 |
self.error('%s has no associated value', self.key) |
|
506 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
507 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
508 |
class ChangeCWPropertyOp(Operation): |
0 | 509 |
"""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
|
510 |
|
0 | 511 |
def commit_event(self): |
512 |
"""the observed connections pool has been commited""" |
|
513 |
self.epropdict[self.key] = self.value |
|
514 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
515 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
516 |
class AddCWPropertyOp(Operation): |
0 | 517 |
"""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
|
518 |
|
0 | 519 |
def commit_event(self): |
520 |
"""the observed connections pool has been commited""" |
|
521 |
eprop = self.eprop |
|
522 |
if not eprop.for_user: |
|
523 |
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
|
524 |
# if for_user is set, update is handled by a ChangeCWPropertyOp operation |
0 | 525 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
526 |
|
0 | 527 |
def after_add_eproperty(session, entity): |
528 |
key, value = entity.pkey, entity.value |
|
529 |
try: |
|
530 |
value = session.vreg.typed_value(key, value) |
|
531 |
except UnknownProperty: |
|
532 |
raise ValidationError(entity.eid, {'pkey': session._('unknown property key')}) |
|
533 |
except ValueError, ex: |
|
534 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
535 |
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
|
536 |
session.add_relation(entity.eid, 'for_user', session.user.eid) |
0 | 537 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
538 |
AddCWPropertyOp(session, eprop=entity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
539 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
540 |
|
0 | 541 |
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
|
542 |
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
|
543 |
'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
|
544 |
return |
0 | 545 |
key, value = entity.pkey, entity.value |
546 |
try: |
|
547 |
value = session.vreg.typed_value(key, value) |
|
548 |
except UnknownProperty: |
|
549 |
return |
|
550 |
except ValueError, ex: |
|
551 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
552 |
if entity.for_user: |
|
553 |
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
|
554 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 555 |
key=key, value=value) |
556 |
else: |
|
557 |
# site wide properties |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
558 |
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
0 | 559 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
560 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
561 |
|
0 | 562 |
def before_del_eproperty(session, eid): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
563 |
for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
0 | 564 |
if rtype == 'for_user' and eidfrom == eid: |
565 |
# if for_user was set, delete has already been handled |
|
566 |
break |
|
567 |
else: |
|
568 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
569 |
{'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
|
570 |
DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key) |
0 | 571 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
572 |
|
0 | 573 |
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
|
574 |
if not session.describe(fromeid)[0] == 'CWProperty': |
0 | 575 |
return |
576 |
key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
|
577 |
{'x': fromeid}, 'x')[0] |
|
578 |
if session.vreg.property_info(key)['sitewide']: |
|
579 |
raise ValidationError(fromeid, |
|
580 |
{'for_user': session._("site-wide property can't be set for user")}) |
|
581 |
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
|
582 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 583 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
584 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
585 |
|
0 | 586 |
def before_del_for_user(session, fromeid, rtype, toeid): |
587 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
588 |
{'x': fromeid}, 'x')[0][0] |
|
589 |
relation_deleted(session, fromeid, rtype, toeid) |
|
590 |
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
|
591 |
DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
0 | 592 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
593 |
|
0 | 594 |
def _register_eproperty_hooks(hm): |
595 |
"""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
|
596 |
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
|
597 |
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
|
598 |
hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty') |
0 | 599 |
hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user') |
600 |
hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user') |