author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 04 Aug 2009 17:39:02 +0200 | |
changeset 2682 | 0188a4a02403 |
parent 2607 | 5d889b4928bb |
child 2695 | 56439c45781c |
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 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
70 |
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
|
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') |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
316 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
0 | 317 |
if card[0] in '1+' and not eidfrom in pendingeids: |
318 |
checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom) |
|
319 |
if card[1] in '1+' and not eidto in pendingeids: |
|
320 |
checkrel_if_necessary(session, CheckORelationOp, rtype, eidto) |
|
321 |
||
322 |
||
323 |
def _register_core_hooks(hm): |
|
324 |
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
|
325 |
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
|
326 |
|
0 | 327 |
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '') |
328 |
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
329 |
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
330 |
hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
331 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
332 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
333 |
||
334 |
||
335 |
# user/groups synchronisation ################################################# |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
336 |
|
0 | 337 |
class GroupOperation(Operation): |
338 |
"""base class for group operation""" |
|
339 |
geid = None |
|
340 |
def __init__(self, session, *args, **kwargs): |
|
341 |
"""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
|
342 |
|
0 | 343 |
we may temporarily loose right access during a commit event, so |
344 |
no query should be emitted while comitting |
|
345 |
""" |
|
346 |
rql = 'Any N WHERE G eid %(x)s, G name N' |
|
347 |
result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False) |
|
348 |
Operation.__init__(self, session, *args, **kwargs) |
|
349 |
self.group = result[0][0] |
|
350 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
351 |
|
0 | 352 |
class DeleteGroupOp(GroupOperation): |
353 |
"""synchronize user when a in_group relation has been deleted""" |
|
354 |
def commit_event(self): |
|
355 |
"""the observed connections pool has been commited""" |
|
356 |
groups = self.cnxuser.groups |
|
357 |
try: |
|
358 |
groups.remove(self.group) |
|
359 |
except KeyError: |
|
360 |
self.error('user %s not in group %s', self.cnxuser, self.group) |
|
361 |
return |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
362 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
363 |
|
0 | 364 |
def after_del_in_group(session, fromeid, rtype, toeid): |
365 |
"""modify user permission, need to update users""" |
|
366 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
367 |
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
368 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
369 |
|
0 | 370 |
class AddGroupOp(GroupOperation): |
371 |
"""synchronize user when a in_group relation has been added""" |
|
372 |
def commit_event(self): |
|
373 |
"""the observed connections pool has been commited""" |
|
374 |
groups = self.cnxuser.groups |
|
375 |
if self.group in groups: |
|
376 |
self.warning('user %s already in group %s', self.cnxuser, |
|
377 |
self.group) |
|
378 |
return |
|
379 |
groups.add(self.group) |
|
380 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
381 |
|
0 | 382 |
def after_add_in_group(session, fromeid, rtype, toeid): |
383 |
"""modify user permission, need to update users""" |
|
384 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
385 |
AddGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
386 |
||
387 |
||
388 |
class DelUserOp(Operation): |
|
389 |
"""synchronize user when a in_group relation has been added""" |
|
390 |
def __init__(self, session, cnxid): |
|
391 |
self.cnxid = cnxid |
|
392 |
Operation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
393 |
|
0 | 394 |
def commit_event(self): |
395 |
"""the observed connections pool has been commited""" |
|
396 |
try: |
|
397 |
self.repo.close(self.cnxid) |
|
398 |
except BadConnectionId: |
|
399 |
pass # already closed |
|
400 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
401 |
|
0 | 402 |
def after_del_user(session, eid): |
403 |
"""modify user permission, need to update users""" |
|
404 |
for session_ in get_user_sessions(session.repo, eid): |
|
405 |
DelUserOp(session, session_.id) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
406 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
407 |
|
0 | 408 |
def _register_usergroup_hooks(hm): |
409 |
"""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
|
410 |
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser') |
0 | 411 |
hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group') |
412 |
hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group') |
|
413 |
||
414 |
||
415 |
# workflow handling ########################################################### |
|
416 |
||
417 |
def before_add_in_state(session, fromeid, rtype, toeid): |
|
418 |
"""check the transition is allowed and record transition information |
|
419 |
""" |
|
420 |
assert rtype == 'in_state' |
|
421 |
state = previous_state(session, fromeid) |
|
422 |
etype = session.describe(fromeid)[0] |
|
423 |
if not (session.is_super_session or 'managers' in session.user.groups): |
|
424 |
if not state is None: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
425 |
entity = session.entity_from_eid(fromeid) |
0 | 426 |
# we should find at least one transition going to this state |
427 |
try: |
|
428 |
iter(state.transitions(entity, toeid)).next() |
|
429 |
except StopIteration: |
|
430 |
msg = session._('transition is not allowed') |
|
431 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
432 |
else: |
|
433 |
# not a transition |
|
434 |
# check state is initial state if the workflow defines one |
|
435 |
isrset = session.unsafe_execute('Any S WHERE ET initial_state S, ET name %(etype)s', |
|
436 |
{'etype': etype}) |
|
437 |
if isrset and not toeid == isrset[0][0]: |
|
438 |
msg = session._('not the initial state for this entity') |
|
439 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
440 |
eschema = session.repo.schema[etype] |
|
441 |
if not 'wf_info_for' in eschema.object_relations(): |
|
442 |
# workflow history not activated for this entity type |
|
443 |
return |
|
444 |
rql = 'INSERT TrInfo T: T wf_info_for E, T to_state DS, T comment %(comment)s' |
|
445 |
args = {'comment': session.get_shared_data('trcomment', None, pop=True), |
|
446 |
'e': fromeid, 'ds': toeid} |
|
447 |
cformat = session.get_shared_data('trcommentformat', None, pop=True) |
|
448 |
if cformat is not None: |
|
449 |
args['comment_format'] = cformat |
|
450 |
rql += ', T comment_format %(comment_format)s' |
|
451 |
restriction = ['DS eid %(ds)s, E eid %(e)s'] |
|
452 |
if not state is None: # not a transition |
|
453 |
rql += ', T from_state FS' |
|
454 |
restriction.append('FS eid %(fs)s') |
|
455 |
args['fs'] = state.eid |
|
456 |
rql = '%s WHERE %s' % (rql, ', '.join(restriction)) |
|
457 |
session.unsafe_execute(rql, args, 'e') |
|
458 |
||
459 |
||
460 |
class SetInitialStateOp(PreCommitOperation): |
|
461 |
"""make initial state be a default state""" |
|
462 |
||
463 |
def precommit_event(self): |
|
464 |
session = self.session |
|
465 |
entity = self.entity |
|
466 |
# if there is an initial state and the entity's state is not set, |
|
467 |
# 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
|
468 |
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
|
469 |
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
|
470 |
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
|
471 |
{'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
|
472 |
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
|
473 |
session.add_relation(entity.eid, 'in_state', rset[0][0]) |
0 | 474 |
|
475 |
||
476 |
def set_initial_state_after_add(session, entity): |
|
477 |
SetInitialStateOp(session, entity=entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
478 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
479 |
|
0 | 480 |
def _register_wf_hooks(hm): |
481 |
"""register workflow related hooks on the hooks manager""" |
|
482 |
if 'in_state' in hm.schema: |
|
483 |
hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state') |
|
484 |
hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state') |
|
485 |
for eschema in hm.schema.entities(): |
|
486 |
if 'in_state' in eschema.subject_relations(): |
|
487 |
hm.register_hook(set_initial_state_after_add, 'after_add_entity', |
|
488 |
str(eschema)) |
|
489 |
||
490 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
491 |
# CWProperty hooks ############################################################# |
0 | 492 |
|
493 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
494 |
class DelCWPropertyOp(Operation): |
0 | 495 |
"""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
|
496 |
|
0 | 497 |
def commit_event(self): |
498 |
"""the observed connections pool has been commited""" |
|
499 |
try: |
|
500 |
del self.epropdict[self.key] |
|
501 |
except KeyError: |
|
502 |
self.error('%s has no associated value', self.key) |
|
503 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
504 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
505 |
class ChangeCWPropertyOp(Operation): |
0 | 506 |
"""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
|
507 |
|
0 | 508 |
def commit_event(self): |
509 |
"""the observed connections pool has been commited""" |
|
510 |
self.epropdict[self.key] = self.value |
|
511 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
512 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
513 |
class AddCWPropertyOp(Operation): |
0 | 514 |
"""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
|
515 |
|
0 | 516 |
def commit_event(self): |
517 |
"""the observed connections pool has been commited""" |
|
518 |
eprop = self.eprop |
|
519 |
if not eprop.for_user: |
|
520 |
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
|
521 |
# if for_user is set, update is handled by a ChangeCWPropertyOp operation |
0 | 522 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
523 |
|
0 | 524 |
def after_add_eproperty(session, entity): |
525 |
key, value = entity.pkey, entity.value |
|
526 |
try: |
|
527 |
value = session.vreg.typed_value(key, value) |
|
528 |
except UnknownProperty: |
|
529 |
raise ValidationError(entity.eid, {'pkey': session._('unknown property key')}) |
|
530 |
except ValueError, ex: |
|
531 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
532 |
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
|
533 |
session.add_relation(entity.eid, 'for_user', session.user.eid) |
0 | 534 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
535 |
AddCWPropertyOp(session, eprop=entity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
536 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
537 |
|
0 | 538 |
def after_update_eproperty(session, entity): |
539 |
key, value = entity.pkey, entity.value |
|
540 |
try: |
|
541 |
value = session.vreg.typed_value(key, value) |
|
542 |
except UnknownProperty: |
|
543 |
return |
|
544 |
except ValueError, ex: |
|
545 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
546 |
if entity.for_user: |
|
547 |
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
|
548 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 549 |
key=key, value=value) |
550 |
else: |
|
551 |
# site wide properties |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
552 |
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
0 | 553 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
554 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
555 |
|
0 | 556 |
def before_del_eproperty(session, eid): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
557 |
for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
0 | 558 |
if rtype == 'for_user' and eidfrom == eid: |
559 |
# if for_user was set, delete has already been handled |
|
560 |
break |
|
561 |
else: |
|
562 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
563 |
{'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
|
564 |
DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key) |
0 | 565 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
566 |
|
0 | 567 |
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
|
568 |
if not session.describe(fromeid)[0] == 'CWProperty': |
0 | 569 |
return |
570 |
key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
|
571 |
{'x': fromeid}, 'x')[0] |
|
572 |
if session.vreg.property_info(key)['sitewide']: |
|
573 |
raise ValidationError(fromeid, |
|
574 |
{'for_user': session._("site-wide property can't be set for user")}) |
|
575 |
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
|
576 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 577 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
578 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
579 |
|
0 | 580 |
def before_del_for_user(session, fromeid, rtype, toeid): |
581 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
582 |
{'x': fromeid}, 'x')[0][0] |
|
583 |
relation_deleted(session, fromeid, rtype, toeid) |
|
584 |
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
|
585 |
DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
0 | 586 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
587 |
|
0 | 588 |
def _register_eproperty_hooks(hm): |
589 |
"""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
|
590 |
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
|
591 |
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
|
592 |
hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty') |
0 | 593 |
hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user') |
594 |
hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user') |