hooks/integrity.py
changeset 9361 0542a85fe667
parent 8556 bbe0d6985e59
child 9469 032825bbacab
equal deleted inserted replaced
9360:eda5071e30a1 9361:0542a85fe667
   105 
   105 
   106 
   106 
   107 class IntegrityHook(hook.Hook):
   107 class IntegrityHook(hook.Hook):
   108     __abstract__ = True
   108     __abstract__ = True
   109     category = 'integrity'
   109     category = 'integrity'
       
   110 
       
   111 
       
   112 class EnsureSymmetricRelationsAdd(hook.Hook):
       
   113     """ ensure X r Y => Y r X iff r is symmetric """
       
   114     __regid__ = 'cw.add_ensure_symmetry'
       
   115     category = 'activeintegrity'
       
   116     events = ('after_add_relation',)
       
   117     # __select__ is set in the registration callback
       
   118 
       
   119     def __call__(self):
       
   120         self._cw.repo.system_source.add_relation(self._cw, self.eidto,
       
   121                                                  self.rtype, self.eidfrom)
       
   122 
       
   123 
       
   124 class EnsureSymmetricRelationsDelete(hook.Hook):
       
   125     """ ensure X r Y => Y r X iff r is symmetric """
       
   126     __regid__ = 'cw.delete_ensure_symmetry'
       
   127     category = 'activeintegrity'
       
   128     events = ('after_delete_relation',)
       
   129     # __select__ is set in the registration callback
       
   130 
       
   131     def __call__(self):
       
   132         self._cw.repo.system_source.delete_relation(self._cw, self.eidto,
       
   133                                                     self.rtype, self.eidfrom)
   110 
   134 
   111 
   135 
   112 class CheckCardinalityHookBeforeDeleteRelation(IntegrityHook):
   136 class CheckCardinalityHookBeforeDeleteRelation(IntegrityHook):
   113     """check cardinalities are satisfied"""
   137     """check cardinalities are satisfied"""
   114     __regid__ = 'checkcard_before_delete_relation'
   138     __regid__ = 'checkcard_before_delete_relation'
   346             _DelayedDeleteOEntityOp.get_instance(self._cw).add_data(
   370             _DelayedDeleteOEntityOp.get_instance(self._cw).add_data(
   347                 (self.eidto, rtype))
   371                 (self.eidto, rtype))
   348         elif composite == 'object':
   372         elif composite == 'object':
   349             _DelayedDeleteSEntityOp.get_instance(self._cw).add_data(
   373             _DelayedDeleteSEntityOp.get_instance(self._cw).add_data(
   350                 (self.eidfrom, rtype))
   374                 (self.eidfrom, rtype))
       
   375 
       
   376 def registration_callback(vreg):
       
   377     vreg.register_all(globals().values(), __name__)
       
   378     symmetric_rtypes = [rschema.type for rschema in vreg.schema.relations()
       
   379                         if rschema.symmetric]
       
   380     EnsureSymmetricRelationsAdd.__select__ = hook.Hook.__select__ & hook.match_rtype(*symmetric_rtypes)
       
   381     EnsureSymmetricRelationsDelete.__select__ = hook.Hook.__select__ & hook.match_rtype(*symmetric_rtypes)
       
   382