hooks/syncsources.py
changeset 6724 24bf6f181d0e
parent 6427 c8a5ac2d1eaa
child 6944 0cf10429ad39
equal deleted inserted replaced
6723:a2ccbcbb08a6 6724:24bf6f181d0e
       
     1 from yams.schema import role_name
     1 from cubicweb import ValidationError
     2 from cubicweb import ValidationError
     2 from cubicweb.selectors import is_instance
     3 from cubicweb.selectors import is_instance
     3 from cubicweb.server import hook
     4 from cubicweb.server import SOURCE_TYPES, hook
     4 
     5 
     5 class SourceHook(hook.Hook):
     6 class SourceHook(hook.Hook):
     6     __abstract__ = True
     7     __abstract__ = True
     7     category = 'cw.sources'
     8     category = 'cw.sources'
     8 
     9 
     9 
    10 
    10 class SourceAddedOp(hook.Operation):
    11 class SourceAddedOp(hook.Operation):
    11     def precommit_event(self):
    12     def postcommit_event(self):
    12         self.session.repo.add_source(self.entity)
    13         self.session.repo.add_source(self.entity)
    13 
    14 
    14 class SourceAddedHook(SourceHook):
    15 class SourceAddedHook(SourceHook):
    15     __regid__ = 'cw.sources.added'
    16     __regid__ = 'cw.sources.added'
    16     __select__ = SourceHook.__select__ & is_instance('CWSource')
    17     __select__ = SourceHook.__select__ & is_instance('CWSource')
    17     events = ('after_add_entity',)
    18     events = ('after_add_entity',)
    18     def __call__(self):
    19     def __call__(self):
       
    20         if not self.entity.type in SOURCE_TYPES:
       
    21             msg = self._cw._('unknown source type')
       
    22             raise ValidationError(self.entity.eid,
       
    23                                   {role_name('type', 'subject'): msg})
    19         SourceAddedOp(self._cw, entity=self.entity)
    24         SourceAddedOp(self._cw, entity=self.entity)
    20 
    25 
    21 
    26 
    22 class SourceRemovedOp(hook.Operation):
    27 class SourceRemovedOp(hook.Operation):
    23     def precommit_event(self):
    28     def precommit_event(self):
    29     events = ('before_delete_entity',)
    34     events = ('before_delete_entity',)
    30     def __call__(self):
    35     def __call__(self):
    31         if self.entity.name == 'system':
    36         if self.entity.name == 'system':
    32             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    37             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    33         SourceRemovedOp(self._cw, uri=self.entity.name)
    38         SourceRemovedOp(self._cw, uri=self.entity.name)
       
    39 
       
    40 class SourceRemovedHook(SourceHook):
       
    41     __regid__ = 'cw.sources.removed'
       
    42     __select__ = SourceHook.__select__ & hook.match_rtype('cw_support', 'cw_may_cross')
       
    43     events = ('after_add_relation',)
       
    44     def __call__(self):
       
    45         entity = self._cw.entity_from_eid(self.eidto)
       
    46         if entity.__regid__ == 'CWRType' and entity.name in  ('is', 'is_instance_of', 'cw_source'):
       
    47             msg = self._cw._('the %s relation type can\'t be used here') % entity.name
       
    48             raise ValidationError(self.eidto, {role_name(self.rtype, 'subject'): msg})