author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 31 Jul 2009 23:47:20 +0200 | |
changeset 2606 | 598ac51cac04 |
parent 2605 | c4f6a53884ec |
child 2607 | 5d889b4928bb |
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 |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
171 |
if not self.eid in session.transaction_data.get('pendingeids', ()): |
0 | 172 |
etype = session.describe(self.eid)[0] |
173 |
session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s' |
|
174 |
% (etype, self.relation), |
|
175 |
{'x': self.eid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
176 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
177 |
|
0 | 178 |
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto): |
179 |
"""delete the object of composite relation""" |
|
180 |
composite = rproperty(session, rtype, eidfrom, eidto, 'composite') |
|
181 |
if composite == 'subject': |
|
182 |
DelayedDeleteOp(session, eid=eidto, relation='Y %s X' % rtype) |
|
183 |
elif composite == 'object': |
|
184 |
DelayedDeleteOp(session, eid=eidfrom, relation='X %s Y' % rtype) |
|
185 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
186 |
|
0 | 187 |
def before_del_group(session, eid): |
188 |
"""check that we don't remove the owners group""" |
|
189 |
check_internal_entity(session, eid, ('owners',)) |
|
190 |
||
191 |
||
192 |
# schema validation hooks ##################################################### |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
193 |
|
0 | 194 |
class CheckConstraintsOperation(LateOperation): |
195 |
"""check a new relation satisfy its constraints |
|
196 |
""" |
|
197 |
def precommit_event(self): |
|
198 |
eidfrom, rtype, eidto = self.rdef |
|
199 |
# first check related entities have not been deleted in the same |
|
200 |
# transaction |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
201 |
pending = self.session.transaction_data.get('pendingeids', ()) |
0 | 202 |
if eidfrom in pending: |
203 |
return |
|
204 |
if eidto in pending: |
|
205 |
return |
|
206 |
for constraint in self.constraints: |
|
207 |
try: |
|
208 |
constraint.repo_check(self.session, eidfrom, rtype, eidto) |
|
209 |
except NotImplementedError: |
|
210 |
self.critical('can\'t check constraint %s, not supported', |
|
211 |
constraint) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
212 |
|
0 | 213 |
def commit_event(self): |
214 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
215 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
216 |
|
0 | 217 |
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto): |
218 |
"""check the relation satisfy its constraints |
|
219 |
||
220 |
this is delayed to a precommit time operation since other relation which |
|
221 |
will make constraint satisfied may be added later. |
|
222 |
""" |
|
223 |
constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
|
224 |
if constraints: |
|
225 |
CheckConstraintsOperation(session, constraints=constraints, |
|
226 |
rdef=(eidfrom, rtype, eidto)) |
|
227 |
||
228 |
def uniquecstrcheck_before_modification(session, entity): |
|
229 |
eschema = entity.e_schema |
|
230 |
for attr, val in entity.items(): |
|
231 |
if val is None: |
|
232 |
continue |
|
233 |
if eschema.subject_relation(attr).is_final() and \ |
|
234 |
eschema.has_unique_values(attr): |
|
235 |
rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr) |
|
236 |
rset = session.unsafe_execute(rql, {'val': val}) |
|
237 |
if rset and rset[0][0] != entity.eid: |
|
238 |
msg = session._('the value "%s" is already used, use another one') |
|
239 |
raise ValidationError(entity.eid, {attr: msg % val}) |
|
240 |
||
241 |
||
242 |
class CheckRequiredRelationOperation(LateOperation): |
|
243 |
"""checking relation cardinality has to be done after commit in |
|
244 |
case the relation is being replaced |
|
245 |
""" |
|
246 |
eid, rtype = None, None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
247 |
|
0 | 248 |
def precommit_event(self): |
249 |
# recheck pending eids |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
250 |
if self.eid in self.session.transaction_data.get('pendingeids', ()): |
0 | 251 |
return |
252 |
if self.session.unsafe_execute(*self._rql()).rowcount < 1: |
|
253 |
etype = self.session.describe(self.eid)[0] |
|
2241
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
254 |
_ = self.session._ |
fcf08ac5f8c0
translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2195
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
|
0 | 259 |
def commit_event(self): |
260 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
261 |
|
0 | 262 |
def _rql(self): |
263 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
264 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
265 |
|
0 | 266 |
class CheckSRelationOp(CheckRequiredRelationOperation): |
267 |
"""check required subject relation""" |
|
268 |
def _rql(self): |
|
269 |
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
|
270 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
271 |
|
0 | 272 |
class CheckORelationOp(CheckRequiredRelationOperation): |
273 |
"""check required object relation""" |
|
274 |
def _rql(self): |
|
275 |
return 'Any S WHERE O eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x' |
|
276 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
277 |
|
0 | 278 |
def checkrel_if_necessary(session, opcls, rtype, eid): |
279 |
"""check an equivalent operation has not already been added""" |
|
280 |
for op in session.pending_operations: |
|
281 |
if isinstance(op, opcls) and op.rtype == rtype and op.eid == eid: |
|
282 |
break |
|
283 |
else: |
|
284 |
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
|
285 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
286 |
|
0 | 287 |
def cardinalitycheck_after_add_entity(session, entity): |
288 |
"""check cardinalities are satisfied""" |
|
289 |
eid = entity.eid |
|
290 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
291 |
# 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
|
292 |
if rschema.type in DONT_CHECK_RTYPES_ON_ADD: |
0 | 293 |
continue |
294 |
if x == 'subject': |
|
295 |
subjtype = entity.e_schema |
|
296 |
objtype = targetschemas[0].type |
|
297 |
cardindex = 0 |
|
298 |
opcls = CheckSRelationOp |
|
299 |
else: |
|
300 |
subjtype = targetschemas[0].type |
|
301 |
objtype = entity.e_schema |
|
302 |
cardindex = 1 |
|
303 |
opcls = CheckORelationOp |
|
304 |
card = rschema.rproperty(subjtype, objtype, 'cardinality') |
|
305 |
if card[cardindex] in '1+': |
|
306 |
checkrel_if_necessary(session, opcls, rschema.type, eid) |
|
307 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
308 |
|
0 | 309 |
def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto): |
310 |
"""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
|
311 |
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
|
312 |
return |
0 | 313 |
card = rproperty(session, rtype, eidfrom, eidto, 'cardinality') |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
314 |
pendingeids = session.transaction_data.get('pendingeids', ()) |
0 | 315 |
if card[0] in '1+' and not eidfrom in pendingeids: |
316 |
checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom) |
|
317 |
if card[1] in '1+' and not eidto in pendingeids: |
|
318 |
checkrel_if_necessary(session, CheckORelationOp, rtype, eidto) |
|
319 |
||
320 |
||
321 |
def _register_core_hooks(hm): |
|
322 |
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
|
323 |
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
|
324 |
|
0 | 325 |
#hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '') |
326 |
hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '') |
|
327 |
hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '') |
|
328 |
hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '') |
|
329 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '') |
|
330 |
hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '') |
|
331 |
||
332 |
||
333 |
# user/groups synchronisation ################################################# |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
334 |
|
0 | 335 |
class GroupOperation(Operation): |
336 |
"""base class for group operation""" |
|
337 |
geid = None |
|
338 |
def __init__(self, session, *args, **kwargs): |
|
339 |
"""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
|
340 |
|
0 | 341 |
we may temporarily loose right access during a commit event, so |
342 |
no query should be emitted while comitting |
|
343 |
""" |
|
344 |
rql = 'Any N WHERE G eid %(x)s, G name N' |
|
345 |
result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False) |
|
346 |
Operation.__init__(self, session, *args, **kwargs) |
|
347 |
self.group = result[0][0] |
|
348 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
349 |
|
0 | 350 |
class DeleteGroupOp(GroupOperation): |
351 |
"""synchronize user when a in_group relation has been deleted""" |
|
352 |
def commit_event(self): |
|
353 |
"""the observed connections pool has been commited""" |
|
354 |
groups = self.cnxuser.groups |
|
355 |
try: |
|
356 |
groups.remove(self.group) |
|
357 |
except KeyError: |
|
358 |
self.error('user %s not in group %s', self.cnxuser, self.group) |
|
359 |
return |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
360 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
361 |
|
0 | 362 |
def after_del_in_group(session, fromeid, rtype, toeid): |
363 |
"""modify user permission, need to update users""" |
|
364 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
365 |
DeleteGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
366 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
367 |
|
0 | 368 |
class AddGroupOp(GroupOperation): |
369 |
"""synchronize user when a in_group relation has been added""" |
|
370 |
def commit_event(self): |
|
371 |
"""the observed connections pool has been commited""" |
|
372 |
groups = self.cnxuser.groups |
|
373 |
if self.group in groups: |
|
374 |
self.warning('user %s already in group %s', self.cnxuser, |
|
375 |
self.group) |
|
376 |
return |
|
377 |
groups.add(self.group) |
|
378 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
379 |
|
0 | 380 |
def after_add_in_group(session, fromeid, rtype, toeid): |
381 |
"""modify user permission, need to update users""" |
|
382 |
for session_ in get_user_sessions(session.repo, fromeid): |
|
383 |
AddGroupOp(session, cnxuser=session_.user, geid=toeid) |
|
384 |
||
385 |
||
386 |
class DelUserOp(Operation): |
|
387 |
"""synchronize user when a in_group relation has been added""" |
|
388 |
def __init__(self, session, cnxid): |
|
389 |
self.cnxid = cnxid |
|
390 |
Operation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
391 |
|
0 | 392 |
def commit_event(self): |
393 |
"""the observed connections pool has been commited""" |
|
394 |
try: |
|
395 |
self.repo.close(self.cnxid) |
|
396 |
except BadConnectionId: |
|
397 |
pass # already closed |
|
398 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
399 |
|
0 | 400 |
def after_del_user(session, eid): |
401 |
"""modify user permission, need to update users""" |
|
402 |
for session_ in get_user_sessions(session.repo, eid): |
|
403 |
DelUserOp(session, session_.id) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
404 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
405 |
|
0 | 406 |
def _register_usergroup_hooks(hm): |
407 |
"""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
|
408 |
hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser') |
0 | 409 |
hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group') |
410 |
hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group') |
|
411 |
||
412 |
||
413 |
# workflow handling ########################################################### |
|
414 |
||
415 |
def before_add_in_state(session, fromeid, rtype, toeid): |
|
416 |
"""check the transition is allowed and record transition information |
|
417 |
""" |
|
418 |
assert rtype == 'in_state' |
|
419 |
state = previous_state(session, fromeid) |
|
420 |
etype = session.describe(fromeid)[0] |
|
421 |
if not (session.is_super_session or 'managers' in session.user.groups): |
|
422 |
if not state is None: |
|
2195
58bef4f707ce
update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
423 |
entity = session.entity_from_eid(fromeid) |
0 | 424 |
# we should find at least one transition going to this state |
425 |
try: |
|
426 |
iter(state.transitions(entity, toeid)).next() |
|
427 |
except StopIteration: |
|
428 |
msg = session._('transition is not allowed') |
|
429 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
430 |
else: |
|
431 |
# not a transition |
|
432 |
# check state is initial state if the workflow defines one |
|
433 |
isrset = session.unsafe_execute('Any S WHERE ET initial_state S, ET name %(etype)s', |
|
434 |
{'etype': etype}) |
|
435 |
if isrset and not toeid == isrset[0][0]: |
|
436 |
msg = session._('not the initial state for this entity') |
|
437 |
raise ValidationError(fromeid, {'in_state': msg}) |
|
438 |
eschema = session.repo.schema[etype] |
|
439 |
if not 'wf_info_for' in eschema.object_relations(): |
|
440 |
# workflow history not activated for this entity type |
|
441 |
return |
|
442 |
rql = 'INSERT TrInfo T: T wf_info_for E, T to_state DS, T comment %(comment)s' |
|
443 |
args = {'comment': session.get_shared_data('trcomment', None, pop=True), |
|
444 |
'e': fromeid, 'ds': toeid} |
|
445 |
cformat = session.get_shared_data('trcommentformat', None, pop=True) |
|
446 |
if cformat is not None: |
|
447 |
args['comment_format'] = cformat |
|
448 |
rql += ', T comment_format %(comment_format)s' |
|
449 |
restriction = ['DS eid %(ds)s, E eid %(e)s'] |
|
450 |
if not state is None: # not a transition |
|
451 |
rql += ', T from_state FS' |
|
452 |
restriction.append('FS eid %(fs)s') |
|
453 |
args['fs'] = state.eid |
|
454 |
rql = '%s WHERE %s' % (rql, ', '.join(restriction)) |
|
455 |
session.unsafe_execute(rql, args, 'e') |
|
456 |
||
457 |
||
458 |
class SetInitialStateOp(PreCommitOperation): |
|
459 |
"""make initial state be a default state""" |
|
460 |
||
461 |
def precommit_event(self): |
|
462 |
session = self.session |
|
463 |
entity = self.entity |
|
464 |
# if there is an initial state and the entity's state is not set, |
|
465 |
# 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
|
466 |
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
|
467 |
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
|
468 |
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
|
469 |
{'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
|
470 |
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
|
471 |
session.add_relation(entity.eid, 'in_state', rset[0][0]) |
0 | 472 |
|
473 |
||
474 |
def set_initial_state_after_add(session, entity): |
|
475 |
SetInitialStateOp(session, entity=entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
476 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
477 |
|
0 | 478 |
def _register_wf_hooks(hm): |
479 |
"""register workflow related hooks on the hooks manager""" |
|
480 |
if 'in_state' in hm.schema: |
|
481 |
hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state') |
|
482 |
hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state') |
|
483 |
for eschema in hm.schema.entities(): |
|
484 |
if 'in_state' in eschema.subject_relations(): |
|
485 |
hm.register_hook(set_initial_state_after_add, 'after_add_entity', |
|
486 |
str(eschema)) |
|
487 |
||
488 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
489 |
# CWProperty hooks ############################################################# |
0 | 490 |
|
491 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
492 |
class DelCWPropertyOp(Operation): |
0 | 493 |
"""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
|
494 |
|
0 | 495 |
def commit_event(self): |
496 |
"""the observed connections pool has been commited""" |
|
497 |
try: |
|
498 |
del self.epropdict[self.key] |
|
499 |
except KeyError: |
|
500 |
self.error('%s has no associated value', self.key) |
|
501 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
502 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
503 |
class ChangeCWPropertyOp(Operation): |
0 | 504 |
"""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
|
505 |
|
0 | 506 |
def commit_event(self): |
507 |
"""the observed connections pool has been commited""" |
|
508 |
self.epropdict[self.key] = self.value |
|
509 |
||
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
510 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
511 |
class AddCWPropertyOp(Operation): |
0 | 512 |
"""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
|
513 |
|
0 | 514 |
def commit_event(self): |
515 |
"""the observed connections pool has been commited""" |
|
516 |
eprop = self.eprop |
|
517 |
if not eprop.for_user: |
|
518 |
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
|
519 |
# if for_user is set, update is handled by a ChangeCWPropertyOp operation |
0 | 520 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
521 |
|
0 | 522 |
def after_add_eproperty(session, entity): |
523 |
key, value = entity.pkey, entity.value |
|
524 |
try: |
|
525 |
value = session.vreg.typed_value(key, value) |
|
526 |
except UnknownProperty: |
|
527 |
raise ValidationError(entity.eid, {'pkey': session._('unknown property key')}) |
|
528 |
except ValueError, ex: |
|
529 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
530 |
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
|
531 |
session.add_relation(entity.eid, 'for_user', session.user.eid) |
0 | 532 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
533 |
AddCWPropertyOp(session, eprop=entity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
534 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
535 |
|
0 | 536 |
def after_update_eproperty(session, entity): |
537 |
key, value = entity.pkey, entity.value |
|
538 |
try: |
|
539 |
value = session.vreg.typed_value(key, value) |
|
540 |
except UnknownProperty: |
|
541 |
return |
|
542 |
except ValueError, ex: |
|
543 |
raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
|
544 |
if entity.for_user: |
|
545 |
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
|
546 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 547 |
key=key, value=value) |
548 |
else: |
|
549 |
# site wide properties |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
550 |
ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
0 | 551 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
552 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
553 |
|
0 | 554 |
def before_del_eproperty(session, eid): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
555 |
for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
0 | 556 |
if rtype == 'for_user' and eidfrom == eid: |
557 |
# if for_user was set, delete has already been handled |
|
558 |
break |
|
559 |
else: |
|
560 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
561 |
{'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
|
562 |
DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key) |
0 | 563 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
564 |
|
0 | 565 |
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
|
566 |
if not session.describe(fromeid)[0] == 'CWProperty': |
0 | 567 |
return |
568 |
key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
|
569 |
{'x': fromeid}, 'x')[0] |
|
570 |
if session.vreg.property_info(key)['sitewide']: |
|
571 |
raise ValidationError(fromeid, |
|
572 |
{'for_user': session._("site-wide property can't be set for user")}) |
|
573 |
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
|
574 |
ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
0 | 575 |
key=key, value=value) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
576 |
|
2603
e47d63351891
[R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2463
diff
changeset
|
577 |
|
0 | 578 |
def before_del_for_user(session, fromeid, rtype, toeid): |
579 |
key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
|
580 |
{'x': fromeid}, 'x')[0][0] |
|
581 |
relation_deleted(session, fromeid, rtype, toeid) |
|
582 |
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
|
583 |
DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
0 | 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 _register_eproperty_hooks(hm): |
587 |
"""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
|
588 |
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
|
589 |
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
|
590 |
hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty') |
0 | 591 |
hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user') |
592 |
hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user') |