cubicweb/hooks/integrity.py
changeset 11366 80dec361a5d0
parent 11057 0b59724cb3f2
child 11765 9cb215e833b0
equal deleted inserted replaced
11365:eedf0684533c 11366:80dec361a5d0
   242                 if constraints:
   242                 if constraints:
   243                     _CheckConstraintsOp.get_instance(self._cw).add_data(
   243                     _CheckConstraintsOp.get_instance(self._cw).add_data(
   244                         (self.entity.eid, attr, None, constraints))
   244                         (self.entity.eid, attr, None, constraints))
   245 
   245 
   246 
   246 
   247 class CheckUniqueHook(IntegrityHook):
       
   248     __regid__ = 'checkunique'
       
   249     events = ('before_add_entity', 'before_update_entity')
       
   250 
       
   251     def __call__(self):
       
   252         entity = self.entity
       
   253         eschema = entity.e_schema
       
   254         for attr, val in entity.cw_edited.items():
       
   255             if eschema.subjrels[attr].final and eschema.has_unique_values(attr):
       
   256                 if val is None:
       
   257                     continue
       
   258                 rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr)
       
   259                 rset = self._cw.execute(rql, {'val': val})
       
   260                 if rset and rset[0][0] != entity.eid:
       
   261                     msg = _('the value "%s" is already used, use another one')
       
   262                     raise validation_error(entity, {(attr, 'subject'): msg},
       
   263                                            (val,))
       
   264 
       
   265 
       
   266 class DontRemoveOwnersGroupHook(IntegrityHook):
   247 class DontRemoveOwnersGroupHook(IntegrityHook):
   267     """delete the composed of a composite relation when this relation is deleted
   248     """delete the composed of a composite relation when this relation is deleted
   268     """
   249     """
   269     __regid__ = 'checkownersgroup'
   250     __regid__ = 'checkownersgroup'
   270     __select__ = IntegrityHook.__select__ & is_instance('CWGroup')
   251     __select__ = IntegrityHook.__select__ & is_instance('CWGroup')