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