9 __docformat__ = "restructuredtext en" |
9 __docformat__ = "restructuredtext en" |
10 |
10 |
11 from datetime import datetime |
11 from datetime import datetime |
12 |
12 |
13 from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
13 from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
14 from cubicweb.schema import RQLVocabularyConstraint |
14 from cubicweb.schema import RQLConstraint, RQLUniqueConstraint |
15 from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation |
15 from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation |
16 from cubicweb.server.hookhelper import (check_internal_entity, |
16 from cubicweb.server.hookhelper import (check_internal_entity, |
17 get_user_sessions, rproperty) |
17 get_user_sessions, rproperty) |
18 from cubicweb.server.repository import FTIndexEntityOp |
18 from cubicweb.server.repository import FTIndexEntityOp |
19 |
19 |
234 """ |
234 """ |
235 if session.is_super_session: |
235 if session.is_super_session: |
236 return |
236 return |
237 constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
237 constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints') |
238 if constraints: |
238 if constraints: |
|
239 # XXX get only RQL[Unique]Constraints? |
239 CheckConstraintsOperation(session, constraints=constraints, |
240 CheckConstraintsOperation(session, constraints=constraints, |
240 rdef=(eidfrom, rtype, eidto)) |
241 rdef=(eidfrom, rtype, eidto)) |
241 |
242 |
242 def uniquecstrcheck_before_modification(session, entity): |
243 def uniquecstrcheck_before_modification(session, entity): |
243 if session.is_super_session: |
244 if session.is_super_session: |
252 rset = session.unsafe_execute(rql, {'val': val}) |
253 rset = session.unsafe_execute(rql, {'val': val}) |
253 if rset and rset[0][0] != entity.eid: |
254 if rset and rset[0][0] != entity.eid: |
254 msg = session._('the value "%s" is already used, use another one') |
255 msg = session._('the value "%s" is already used, use another one') |
255 raise ValidationError(entity.eid, {attr: msg % val}) |
256 raise ValidationError(entity.eid, {attr: msg % val}) |
256 |
257 |
|
258 |
257 def cstrcheck_after_update_attributes(session, entity): |
259 def cstrcheck_after_update_attributes(session, entity): |
258 if session.is_super_session: |
260 if session.is_super_session: |
259 return |
261 return |
260 schema = session.vreg.schema |
262 schema = session.vreg.schema |
261 for attr in entity.edited_attributes: |
263 for attr in entity.edited_attributes: |
262 if schema.rschema(attr).final: |
264 if schema.rschema(attr).final: |
263 constraints = [c for c in entity.e_schema.constraints(attr) |
265 constraints = [c for c in entity.e_schema.constraints(attr) |
264 if isinstance(c, RQLVocabularyConstraint)] |
266 if isinstance(c, (RQLConstraint, RQLUniqueConstraint))] |
265 if constraints: |
267 if constraints: |
266 CheckConstraintsOperation(session, rdef=(entity.eid, attr, None), |
268 CheckConstraintsOperation(session, rdef=(entity.eid, attr, None), |
267 constraints=constraints) |
269 constraints=constraints) |
268 |
270 |
269 |
271 |