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