hooks/integrity.py
brancholdstable
changeset 6665 90f2f20367bc
parent 6375 df4fd2a1b0e7
child 6376 f8662240ed4d
equal deleted inserted replaced
6018:f4d1d5d9ccbb 6665:90f2f20367bc
    24 from threading import Lock
    24 from threading import Lock
    25 
    25 
    26 from yams.schema import role_name
    26 from yams.schema import role_name
    27 
    27 
    28 from cubicweb import ValidationError
    28 from cubicweb import ValidationError
    29 from cubicweb.schema import RQLConstraint, RQLUniqueConstraint
    29 from cubicweb.schema import (META_RTYPES, WORKFLOW_RTYPES,
    30 from cubicweb.selectors import implements
    30                              RQLConstraint, RQLUniqueConstraint)
       
    31 from cubicweb.selectors import is_instance
    31 from cubicweb.uilib import soup2xhtml
    32 from cubicweb.uilib import soup2xhtml
    32 from cubicweb.server import hook
    33 from cubicweb.server import hook
    33 from cubicweb.server.hook import set_operation
    34 from cubicweb.server.hook import set_operation
    34 
    35 
    35 # special relations that don't have to be checked for integrity, usually
    36 # special relations that don't have to be checked for integrity, usually
    36 # because they are handled internally by hooks (so we trust ourselves)
    37 # because they are handled internally by hooks (so we trust ourselves)
    37 DONT_CHECK_RTYPES_ON_ADD = set(('owned_by', 'created_by',
    38 DONT_CHECK_RTYPES_ON_ADD = META_RTYPES | WORKFLOW_RTYPES
    38                                 'is', 'is_instance_of',
    39 DONT_CHECK_RTYPES_ON_DEL = META_RTYPES | WORKFLOW_RTYPES
    39                                 'wf_info_for', 'from_state', 'to_state'))
       
    40 DONT_CHECK_RTYPES_ON_DEL = set(('is', 'is_instance_of',
       
    41                                 'wf_info_for', 'from_state', 'to_state'))
       
    42 
    40 
    43 _UNIQUE_CONSTRAINTS_LOCK = Lock()
    41 _UNIQUE_CONSTRAINTS_LOCK = Lock()
    44 _UNIQUE_CONSTRAINTS_HOLDER = None
    42 _UNIQUE_CONSTRAINTS_HOLDER = None
    45 
    43 
    46 
    44 
   251 
   249 
   252 class DontRemoveOwnersGroupHook(IntegrityHook):
   250 class DontRemoveOwnersGroupHook(IntegrityHook):
   253     """delete the composed of a composite relation when this relation is deleted
   251     """delete the composed of a composite relation when this relation is deleted
   254     """
   252     """
   255     __regid__ = 'checkownersgroup'
   253     __regid__ = 'checkownersgroup'
   256     __select__ = IntegrityHook.__select__ & implements('CWGroup')
   254     __select__ = IntegrityHook.__select__ & is_instance('CWGroup')
   257     events = ('before_delete_entity', 'before_update_entity')
   255     events = ('before_delete_entity', 'before_update_entity')
   258 
   256 
   259     def __call__(self):
   257     def __call__(self):
   260         if self.event == 'before_delete_entity' and self.entity.name == 'owners':
   258         if self.event == 'before_delete_entity' and self.entity.name == 'owners':
   261             msg = self._cw._('can\'t be deleted')
   259             msg = self._cw._('can\'t be deleted')
   291 
   289 
   292 
   290 
   293 class StripCWUserLoginHook(IntegrityHook):
   291 class StripCWUserLoginHook(IntegrityHook):
   294     """ensure user logins are stripped"""
   292     """ensure user logins are stripped"""
   295     __regid__ = 'stripuserlogin'
   293     __regid__ = 'stripuserlogin'
   296     __select__ = IntegrityHook.__select__ & implements('CWUser')
   294     __select__ = IntegrityHook.__select__ & is_instance('CWUser')
   297     events = ('before_add_entity', 'before_update_entity',)
   295     events = ('before_add_entity', 'before_update_entity',)
   298 
   296 
   299     def __call__(self):
   297     def __call__(self):
   300         user = self.entity
   298         user = self.entity
   301         if 'login' in user.edited_attributes and user.login:
   299         if 'login' in user.edited_attributes and user.login: