hooks/integrity.py
changeset 3630 275feb5370c9
parent 3590 1b0dbcf4b214
child 3646 3bba270202ef
equal deleted inserted replaced
3629:559cad62c786 3630:275feb5370c9
   140 
   140 
   141 class CheckConstraintHook(IntegrityHook):
   141 class CheckConstraintHook(IntegrityHook):
   142     """check the relation satisfy its constraints
   142     """check the relation satisfy its constraints
   143 
   143 
   144     this is delayed to a precommit time operation since other relation which
   144     this is delayed to a precommit time operation since other relation which
   145     will make constraint satisfied may be added later.
   145     will make constraint satisfied (or unsatisfied) may be added later.
   146     """
   146     """
   147     __regid__ = 'checkconstraint'
   147     __regid__ = 'checkconstraint'
   148     events = ('after_add_relation',)
   148     events = ('after_add_relation',)
   149 
   149 
   150     def __call__(self):
   150     def __call__(self):
   151         constraints = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
   151         constraints = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
   152                                 'constraints')
   152                                                 'constraints')
   153         if constraints:
   153         if constraints:
   154             _CheckConstraintsOp(self._cw, constraints=constraints,
   154             _CheckConstraintsOp(self._cw, constraints=constraints,
   155                                rdef=(self.eidfrom, self.rtype, self.eidto))
   155                                rdef=(self.eidfrom, self.rtype, self.eidto))
       
   156 
       
   157 class CheckAttributeConstraintHook(IntegrityHook):
       
   158     """check the attribute relation satisfy its constraints
       
   159 
       
   160     this is delayed to a precommit time operation since other relation which
       
   161     will make constraint satisfied (or unsatisfied) may be added later.
       
   162     """
       
   163     __regid__ = 'checkattrconstraint'
       
   164     events = ('after_add_entity', 'after_update_entity')
       
   165 
       
   166     def __call__(self):
       
   167         schema = self._cw.vreg.schema
       
   168         for attr in self.entity.edited_attributes:
       
   169             if schema.rschema(attr).is_final():
       
   170                 constraints = [c for c in entity.e_schema.constraints(attr)
       
   171                                if isinstance(c, RQLVocabularyConstraint)]
       
   172                 if constraints:
       
   173                     _CheckConstraintsOp(self._cw, constraints=constraints,
       
   174                                         rdef=(entity.eid, attr, None))
   156 
   175 
   157 
   176 
   158 class CheckUniqueHook(IntegrityHook):
   177 class CheckUniqueHook(IntegrityHook):
   159     __regid__ = 'checkunique'
   178     __regid__ = 'checkunique'
   160     events = ('before_add_entity', 'before_update_entity')
   179     events = ('before_add_entity', 'before_update_entity')