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