hooks/integrity.py
branchstable
changeset 5538 752bc67064f2
parent 5450 269dcd14b92c
child 5877 0c7b7b76a84f
equal deleted inserted replaced
5537:2889091bd1bf 5538:752bc67064f2
   134                 continue
   134                 continue
   135             rdef = rschema.role_rdef(eschema, targetschemas[0], role)
   135             rdef = rschema.role_rdef(eschema, targetschemas[0], role)
   136             if rdef.role_cardinality(role) in '1+':
   136             if rdef.role_cardinality(role) in '1+':
   137                 if role == 'subject':
   137                 if role == 'subject':
   138                     set_operation(self._cw, '_cwisrel', (eid, rschema.type),
   138                     set_operation(self._cw, '_cwisrel', (eid, rschema.type),
   139                                   _CheckSRelationOp)
   139                                   _CheckSRelationOp, list)
   140                 else:
   140                 else:
   141                     set_operation(self._cw, '_cwiorel', (eid, rschema.type),
   141                     set_operation(self._cw, '_cwiorel', (eid, rschema.type),
   142                                   _CheckORelationOp)
   142                                   _CheckORelationOp, list)
   143 
   143 
   144     def before_delete_relation(self):
   144     def before_delete_relation(self):
   145         rtype = self.rtype
   145         rtype = self.rtype
   146         if rtype in DONT_CHECK_RTYPES_ON_DEL:
   146         if rtype in DONT_CHECK_RTYPES_ON_DEL:
   147             return
   147             return
   151         if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs:
   151         if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs:
   152             return
   152             return
   153         card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality')
   153         card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality')
   154         if card[0] in '1+' and not session.deleted_in_transaction(eidfrom):
   154         if card[0] in '1+' and not session.deleted_in_transaction(eidfrom):
   155             set_operation(self._cw, '_cwisrel', (eidfrom, rtype),
   155             set_operation(self._cw, '_cwisrel', (eidfrom, rtype),
   156                           _CheckSRelationOp)
   156                           _CheckSRelationOp, list)
   157         if card[1] in '1+' and not session.deleted_in_transaction(eidto):
   157         if card[1] in '1+' and not session.deleted_in_transaction(eidto):
   158             set_operation(self._cw, '_cwiorel', (eidto, rtype),
   158             set_operation(self._cw, '_cwiorel', (eidto, rtype),
   159                           _CheckORelationOp)
   159                           _CheckORelationOp, list)
   160 
   160 
   161 
   161 
   162 class _CheckConstraintsOp(hook.LateOperation):
   162 class _CheckConstraintsOp(hook.LateOperation):
   163     """ check a new relation satisfy its constraints """
   163     """ check a new relation satisfy its constraints """
   164 
   164 
   203         constraints = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
   203         constraints = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
   204                                                 'constraints')
   204                                                 'constraints')
   205         if constraints:
   205         if constraints:
   206             hook.set_operation(self._cw, 'check_constraints_op',
   206             hook.set_operation(self._cw, 'check_constraints_op',
   207                                (self.eidfrom, self.rtype, self.eidto, tuple(constraints)),
   207                                (self.eidfrom, self.rtype, self.eidto, tuple(constraints)),
   208                                _CheckConstraintsOp)
   208                                _CheckConstraintsOp, list)
   209 
   209 
   210 
   210 
   211 class CheckAttributeConstraintHook(IntegrityHook):
   211 class CheckAttributeConstraintHook(IntegrityHook):
   212     """check the attribute relation satisfy its constraints
   212     """check the attribute relation satisfy its constraints
   213 
   213 
   224                 constraints = [c for c in eschema.rdef(attr).constraints
   224                 constraints = [c for c in eschema.rdef(attr).constraints
   225                                if isinstance(c, (RQLUniqueConstraint, RQLConstraint))]
   225                                if isinstance(c, (RQLUniqueConstraint, RQLConstraint))]
   226                 if constraints:
   226                 if constraints:
   227                     hook.set_operation(self._cw, 'check_constraints_op',
   227                     hook.set_operation(self._cw, 'check_constraints_op',
   228                                        (self.entity.eid, attr, None, tuple(constraints)),
   228                                        (self.entity.eid, attr, None, tuple(constraints)),
   229                                        _CheckConstraintsOp)
   229                                        _CheckConstraintsOp, list)
   230 
   230 
   231 
   231 
   232 class CheckUniqueHook(IntegrityHook):
   232 class CheckUniqueHook(IntegrityHook):
   233     __regid__ = 'checkunique'
   233     __regid__ = 'checkunique'
   234     events = ('before_add_entity', 'before_update_entity')
   234     events = ('before_add_entity', 'before_update_entity')