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