cubicweb/hooks/syncsources.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 12 Apr 2017 16:07:25 +0200
branch3.25
changeset 12152 f34d18d0603f
parent 12150 6260c88e0ff5
child 12153 0ff0aff4413d
permissions -rw-r--r--
[sources] Check sources configuration is fine on creation/modification Reintroduce usage of 'source.check_config' which had almost disappeared, as well as tests for the currently detected errors. Part of the system source specific checking done in syncsources reimplemented in a specific check_config implementation. Tests are dispatched among ldap / datafeed and syncsources tests but are not strictly correctly located (notably syncsources tests behaviour of the native source's check_config). The system source url checking part which disappears from syncsources will be reintroduced in a follow-up.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
     1
# copyright 2010-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6957
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     3
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     4
# This file is part of CubicWeb.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     5
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     9
# any later version.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    10
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    14
# details.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    15
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    18
"""hooks for repository sources synchronization"""
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    19
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9619
diff changeset
    20
from cubicweb import _
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    21
7024
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
    22
from socket import gethostname
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
    23
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    24
from logilab.common.decorators import clear_cache
6957
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    25
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    26
from cubicweb import validation_error
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    27
from cubicweb.predicates import is_instance
6724
24bf6f181d0e [pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    28
from cubicweb.server import SOURCE_TYPES, hook
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
    30
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
class SourceHook(hook.Hook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    __abstract__ = True
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
    category = 'cw.sources'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
12152
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    35
    def get_source(self, source_entity):
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    36
        if source_entity.name == 'system':
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    37
            return self._cw.repo.system_source
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    38
        return self._cw.repo.get_source(source_entity.type, source_entity.name,
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    39
                                        {}, source_entity.eid)
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    40
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
7399
972ed1843bd8 [multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7049
diff changeset
    42
# repo sources synchronization #################################################
972ed1843bd8 [multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7049
diff changeset
    43
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
class SourceAddedHook(SourceHook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    __regid__ = 'cw.sources.added'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
    __select__ = SourceHook.__select__ & is_instance('CWSource')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
    events = ('after_add_entity',)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
    def __call__(self):
12152
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    49
        if self.entity.type not in SOURCE_TYPES:
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    50
            msg = _('Unknown source type')
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    51
            raise validation_error(self.entity, {('type', 'subject'): msg})
12152
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    52
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    53
        source = self.get_source(self.entity)
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    54
        source.check_config(self.entity)
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
class SourceRemovedHook(SourceHook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
    __regid__ = 'cw.sources.removed'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
    __select__ = SourceHook.__select__ & is_instance('CWSource')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
    events = ('before_delete_entity',)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
    def __call__(self):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
        if self.entity.name == 'system':
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    63
            msg = _("You cannot remove the system source")
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    64
            raise validation_error(self.entity, {None: msg})
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    65
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    66
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    67
class SourceUpdatedHook(SourceHook):
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    68
    __regid__ = 'cw.sources.configupdate'
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    69
    __select__ = SourceHook.__select__ & is_instance('CWSource')
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    70
    events = ('before_update_entity',)
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    71
    def __call__(self):
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    72
        if 'name' in self.entity.cw_edited:
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    73
            oldname, newname = self.entity.cw_edited.oldnewvalue('name')
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    74
            if oldname == 'system':
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    75
                msg = _("You cannot rename the system source")
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    76
                raise validation_error(self.entity, {('name', 'subject'): msg})
12152
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    77
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    78
        source = self.get_source(self.entity)
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    79
        if 'config' in self.entity.cw_edited:
f34d18d0603f [sources] Check sources configuration is fine on creation/modification
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12150
diff changeset
    80
            source.check_config(self.entity)