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