hooks/syncsources.py
changeset 8556 bbe0d6985e59
parent 8190 2a3c1b787688
child 8557 8756f932ca83
equal deleted inserted replaced
8555:c747242d22a6 8556:bbe0d6985e59
     1 # copyright 2010-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2010-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """hooks for repository sources synchronization"""
    18 """hooks for repository sources synchronization"""
    19 
    19 
       
    20 _ = unicode
       
    21 
    20 from socket import gethostname
    22 from socket import gethostname
    21 
    23 
    22 from logilab.common.decorators import clear_cache
    24 from logilab.common.decorators import clear_cache
    23 from yams.schema import role_name
    25 
    24 
    26 from cubicweb import validation_error
    25 from cubicweb import ValidationError
       
    26 from cubicweb.predicates import is_instance
    27 from cubicweb.predicates import is_instance
    27 from cubicweb.server import SOURCE_TYPES, hook
    28 from cubicweb.server import SOURCE_TYPES, hook
    28 
    29 
    29 class SourceHook(hook.Hook):
    30 class SourceHook(hook.Hook):
    30     __abstract__ = True
    31     __abstract__ = True
    44     events = ('after_add_entity',)
    45     events = ('after_add_entity',)
    45     def __call__(self):
    46     def __call__(self):
    46         try:
    47         try:
    47             sourcecls = SOURCE_TYPES[self.entity.type]
    48             sourcecls = SOURCE_TYPES[self.entity.type]
    48         except KeyError:
    49         except KeyError:
    49             msg = self._cw._('unknown source type')
    50             msg = _('unknown source type')
    50             raise ValidationError(self.entity.eid,
    51             raise validation_error(self.entity, {('type', 'subject'): msg})
    51                                   {role_name('type', 'subject'): msg})
       
    52         sourcecls.check_conf_dict(self.entity.eid, self.entity.host_config,
    52         sourcecls.check_conf_dict(self.entity.eid, self.entity.host_config,
    53                                   fail_if_unknown=not self._cw.vreg.config.repairing)
    53                                   fail_if_unknown=not self._cw.vreg.config.repairing)
    54         SourceAddedOp(self._cw, entity=self.entity)
    54         SourceAddedOp(self._cw, entity=self.entity)
    55 
    55 
    56 
    56 
    63     __regid__ = 'cw.sources.removed'
    63     __regid__ = 'cw.sources.removed'
    64     __select__ = SourceHook.__select__ & is_instance('CWSource')
    64     __select__ = SourceHook.__select__ & is_instance('CWSource')
    65     events = ('before_delete_entity',)
    65     events = ('before_delete_entity',)
    66     def __call__(self):
    66     def __call__(self):
    67         if self.entity.name == 'system':
    67         if self.entity.name == 'system':
    68             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    68             msg = _("You cannot remove the system source")
       
    69             raise validation_error(self.entity, {None: msg})
    69         SourceRemovedOp(self._cw, uri=self.entity.name)
    70         SourceRemovedOp(self._cw, uri=self.entity.name)
    70 
    71 
    71 
    72 
    72 class SourceConfigUpdatedOp(hook.DataOperationMixIn, hook.Operation):
    73 class SourceConfigUpdatedOp(hook.DataOperationMixIn, hook.Operation):
    73 
    74 
   152     __regid__ = 'cw.sources.mapping.immutable'
   153     __regid__ = 'cw.sources.mapping.immutable'
   153     __select__ = SourceHook.__select__ & hook.match_rtype('cw_for_source', 'cw_schema')
   154     __select__ = SourceHook.__select__ & hook.match_rtype('cw_for_source', 'cw_schema')
   154     events = ('before_add_relation',)
   155     events = ('before_add_relation',)
   155     def __call__(self):
   156     def __call__(self):
   156         if not self._cw.added_in_transaction(self.eidfrom):
   157         if not self._cw.added_in_transaction(self.eidfrom):
   157             msg = self._cw._("can't change this relation")
   158             msg = _("You can't change this relation")
   158             raise ValidationError(self.eidfrom, {self.rtype: msg})
   159             raise validation_error(self.eidfrom, {self.rtype: msg})
   159 
   160 
   160 
   161 
   161 class SourceMappingChangedOp(hook.DataOperationMixIn, hook.Operation):
   162 class SourceMappingChangedOp(hook.DataOperationMixIn, hook.Operation):
   162     def check_or_update(self, checkonly):
   163     def check_or_update(self, checkonly):
   163         session = self.session
   164         session = self.session