hooks/integrity.py
branchstable
changeset 5007 bc0a67a95b69
parent 4835 13b0b96d7982
child 5030 5238d9a8dfee
equal deleted inserted replaced
5006:871269e5e020 5007:bc0a67a95b69
   226                 if rset and rset[0][0] != entity.eid:
   226                 if rset and rset[0][0] != entity.eid:
   227                     msg = self._cw._('the value "%s" is already used, use another one')
   227                     msg = self._cw._('the value "%s" is already used, use another one')
   228                     raise ValidationError(entity.eid, {attr: msg % val})
   228                     raise ValidationError(entity.eid, {attr: msg % val})
   229 
   229 
   230 
   230 
   231 class _DelayedDeleteOp(hook.Operation):
       
   232     """delete the object of composite relation except if the relation
       
   233     has actually been redirected to another composite
       
   234     """
       
   235 
       
   236     def precommit_event(self):
       
   237         session = self.session
       
   238         # don't do anything if the entity is being created or deleted
       
   239         if not (session.deleted_in_transaction(self.eid) or
       
   240                 session.added_in_transaction(self.eid)):
       
   241             etype = session.describe(self.eid)[0]
       
   242             session.execute('DELETE %s X WHERE X eid %%(x)s, NOT %s'
       
   243                             % (etype, self.relation),
       
   244                             {'x': self.eid}, 'x')
       
   245 
       
   246 
       
   247 class DeleteCompositeOrphanHook(IntegrityHook):
       
   248     """delete the composed of a composite relation when this relation is deleted
       
   249     """
       
   250     __regid__ = 'deletecomposite'
       
   251     events = ('before_delete_relation',)
       
   252 
       
   253     def __call__(self):
       
   254         # if the relation is being delete, don't delete composite's components
       
   255         # automatically
       
   256         pendingrdefs = self._cw.transaction_data.get('pendingrdefs', ())
       
   257         if (self._cw.describe(self.eidfrom)[0], self.rtype,
       
   258             self._cw.describe(self.eidto)[0]) in pendingrdefs:
       
   259             return
       
   260         composite = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
       
   261                                                  'composite')
       
   262         if composite == 'subject':
       
   263             _DelayedDeleteOp(self._cw, eid=self.eidto,
       
   264                              relation='Y %s X' % self.rtype)
       
   265         elif composite == 'object':
       
   266             _DelayedDeleteOp(self._cw, eid=self.eidfrom,
       
   267                              relation='X %s Y' % self.rtype)
       
   268 
       
   269 
       
   270 class DontRemoveOwnersGroupHook(IntegrityHook):
   231 class DontRemoveOwnersGroupHook(IntegrityHook):
   271     """delete the composed of a composite relation when this relation is deleted
   232     """delete the composed of a composite relation when this relation is deleted
   272     """
   233     """
   273     __regid__ = 'checkownersgroup'
   234     __regid__ = 'checkownersgroup'
   274     __select__ = IntegrityHook.__select__ & implements('CWGroup')
   235     __select__ = IntegrityHook.__select__ & implements('CWGroup')
   312 
   273 
   313     def __call__(self):
   274     def __call__(self):
   314         user = self.entity
   275         user = self.entity
   315         if 'login' in user.edited_attributes and user.login:
   276         if 'login' in user.edited_attributes and user.login:
   316             user.login = user.login.strip()
   277             user.login = user.login.strip()
       
   278 
       
   279 
       
   280 # 'active' integrity hooks: you usually don't want to deactivate them, they are
       
   281 # not really integrity check, they maintain consistency on changes
       
   282 
       
   283 class _DelayedDeleteOp(hook.Operation):
       
   284     """delete the object of composite relation except if the relation
       
   285     has actually been redirected to another composite
       
   286     """
       
   287 
       
   288     def precommit_event(self):
       
   289         session = self.session
       
   290         # don't do anything if the entity is being created or deleted
       
   291         if not (session.deleted_in_transaction(self.eid) or
       
   292                 session.added_in_transaction(self.eid)):
       
   293             etype = session.describe(self.eid)[0]
       
   294             session.execute('DELETE %s X WHERE X eid %%(x)s, NOT %s'
       
   295                             % (etype, self.relation),
       
   296                             {'x': self.eid}, 'x')
       
   297 
       
   298 
       
   299 class DeleteCompositeOrphanHook(hook.Hook):
       
   300     """delete the composed of a composite relation when this relation is deleted
       
   301     """
       
   302     __regid__ = 'deletecomposite'
       
   303     events = ('before_delete_relation',)
       
   304     category = 'activeintegrity'
       
   305 
       
   306     def __call__(self):
       
   307         # if the relation is being delete, don't delete composite's components
       
   308         # automatically
       
   309         pendingrdefs = self._cw.transaction_data.get('pendingrdefs', ())
       
   310         if (self._cw.describe(self.eidfrom)[0], self.rtype,
       
   311             self._cw.describe(self.eidto)[0]) in pendingrdefs:
       
   312             return
       
   313         composite = self._cw.schema_rproperty(self.rtype, self.eidfrom, self.eidto,
       
   314                                               'composite')
       
   315         if composite == 'subject':
       
   316             _DelayedDeleteOp(self._cw, eid=self.eidto,
       
   317                              relation='Y %s X' % self.rtype)
       
   318         elif composite == 'object':
       
   319             _DelayedDeleteOp(self._cw, eid=self.eidfrom,
       
   320                              relation='X %s Y' % self.rtype)