server/hooks.py
branchstable
changeset 3554 26e586f3c15c
parent 3528 77a69de16709
child 3568 87a7ca9d8ce6
equal deleted inserted replaced
3553:76b4a177a018 3554:26e586f3c15c
     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 
    14 from cubicweb.schema import RQLVocabularyConstraint
    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 
   238             rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr)
   238             rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr)
   239             rset = session.unsafe_execute(rql, {'val': val})
   239             rset = session.unsafe_execute(rql, {'val': val})
   240             if rset and rset[0][0] != entity.eid:
   240             if rset and rset[0][0] != entity.eid:
   241                 msg = session._('the value "%s" is already used, use another one')
   241                 msg = session._('the value "%s" is already used, use another one')
   242                 raise ValidationError(entity.eid, {attr: msg % val})
   242                 raise ValidationError(entity.eid, {attr: msg % val})
       
   243 
       
   244 def cstrcheck_after_update_attributes(session, entity):
       
   245     schema = session.vreg.schema
       
   246     for attr in entity.edited_attributes:
       
   247         if schema.rschema(attr).is_final():
       
   248             constraints = [c for c in entity.e_schema.constraints(attr)
       
   249                            if isinstance(c, RQLVocabularyConstraint)]
       
   250             if constraints:
       
   251                 CheckConstraintsOperation(session, rdef=(entity.eid, attr, None),
       
   252                                           constraints=constraints)
   243 
   253 
   244 
   254 
   245 class CheckRequiredRelationOperation(LateOperation):
   255 class CheckRequiredRelationOperation(LateOperation):
   246     """checking relation cardinality has to be done after commit in
   256     """checking relation cardinality has to be done after commit in
   247     case the relation is being replaced
   257     case the relation is being replaced
   332     hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
   342     hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
   333     hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
   343     hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
   334     hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '')
   344     hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '')
   335     hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
   345     hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
   336     hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
   346     hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
   337 
   347     hm.register_hook(cstrcheck_after_update_attributes, 'after_add_entity', '')
       
   348     hm.register_hook(cstrcheck_after_update_attributes, 'after_update_entity', '')
   338 
   349 
   339 # user/groups synchronisation #################################################
   350 # user/groups synchronisation #################################################
   340 
   351 
   341 class GroupOperation(Operation):
   352 class GroupOperation(Operation):
   342     """base class for group operation"""
   353     """base class for group operation"""