cubicweb/hooks/syncsources.py
branch3.25
changeset 12152 f34d18d0603f
parent 12150 6260c88e0ff5
child 12153 0ff0aff4413d
equal deleted inserted replaced
12151:569dce882f60 12152:f34d18d0603f
    30 
    30 
    31 class SourceHook(hook.Hook):
    31 class SourceHook(hook.Hook):
    32     __abstract__ = True
    32     __abstract__ = True
    33     category = 'cw.sources'
    33     category = 'cw.sources'
    34 
    34 
       
    35     def get_source(self, source_entity):
       
    36         if source_entity.name == 'system':
       
    37             return self._cw.repo.system_source
       
    38         return self._cw.repo.get_source(source_entity.type, source_entity.name,
       
    39                                         {}, source_entity.eid)
       
    40 
    35 
    41 
    36 # repo sources synchronization #################################################
    42 # repo sources synchronization #################################################
    37 
    43 
    38 class SourceAddedHook(SourceHook):
    44 class SourceAddedHook(SourceHook):
    39     __regid__ = 'cw.sources.added'
    45     __regid__ = 'cw.sources.added'
    40     __select__ = SourceHook.__select__ & is_instance('CWSource')
    46     __select__ = SourceHook.__select__ & is_instance('CWSource')
    41     events = ('after_add_entity',)
    47     events = ('after_add_entity',)
    42     def __call__(self):
    48     def __call__(self):
    43         try:
    49         if self.entity.type not in SOURCE_TYPES:
    44             sourcecls = SOURCE_TYPES[self.entity.type]
       
    45         except KeyError:
       
    46             msg = _('Unknown source type')
    50             msg = _('Unknown source type')
    47             raise validation_error(self.entity, {('type', 'subject'): msg})
    51             raise validation_error(self.entity, {('type', 'subject'): msg})
    48         # ignore creation of the system source done during database
    52 
    49         # initialisation, as config for this source is in a file and handling
    53         source = self.get_source(self.entity)
    50         # is done separatly (no need for the operation either)
    54         source.check_config(self.entity)
    51         if self.entity.name != 'system':
       
    52             sourcecls._check_config_dict(self.entity.eid, self.entity.host_config,
       
    53                                          raise_on_error=not self._cw.vreg.config.repairing)
       
    54 
    55 
    55 
    56 
    56 class SourceRemovedHook(SourceHook):
    57 class SourceRemovedHook(SourceHook):
    57     __regid__ = 'cw.sources.removed'
    58     __regid__ = 'cw.sources.removed'
    58     __select__ = SourceHook.__select__ & is_instance('CWSource')
    59     __select__ = SourceHook.__select__ & is_instance('CWSource')
    71         if 'name' in self.entity.cw_edited:
    72         if 'name' in self.entity.cw_edited:
    72             oldname, newname = self.entity.cw_edited.oldnewvalue('name')
    73             oldname, newname = self.entity.cw_edited.oldnewvalue('name')
    73             if oldname == 'system':
    74             if oldname == 'system':
    74                 msg = _("You cannot rename the system source")
    75                 msg = _("You cannot rename the system source")
    75                 raise validation_error(self.entity, {('name', 'subject'): msg})
    76                 raise validation_error(self.entity, {('name', 'subject'): msg})
    76         if 'config' in self.entity.cw_edited or 'url' in self.entity.cw_edited:
    77 
    77             if self.entity.name == 'system' and self.entity.config:
    78         source = self.get_source(self.entity)
    78                 msg = _("Configuration of the system source goes to "
    79         if 'config' in self.entity.cw_edited:
    79                         "the 'sources' file, not in the database")
    80             source.check_config(self.entity)
    80                 raise validation_error(self.entity, {('config', 'subject'): msg})