hooks/syncsources.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Sat, 09 Oct 2010 00:05:52 +0200
changeset 6427 c8a5ac2d1eaa
child 6724 24bf6f181d0e
permissions -rw-r--r--
[schema / sources] store data sources as cubicweb entities this implies several changes: * new CWSource / CWSourceHostConfig entity types * only the system sources and default admin login/password stored in sources file (other stuff will be ignored) * on startup, get sources definition from the database * every entities have a cw_source relation * a facet allow filtering

from cubicweb import ValidationError
from cubicweb.selectors import is_instance
from cubicweb.server import hook

class SourceHook(hook.Hook):
    __abstract__ = True
    category = 'cw.sources'


class SourceAddedOp(hook.Operation):
    def precommit_event(self):
        self.session.repo.add_source(self.entity)

class SourceAddedHook(SourceHook):
    __regid__ = 'cw.sources.added'
    __select__ = SourceHook.__select__ & is_instance('CWSource')
    events = ('after_add_entity',)
    def __call__(self):
        SourceAddedOp(self._cw, entity=self.entity)


class SourceRemovedOp(hook.Operation):
    def precommit_event(self):
        self.session.repo.remove_source(self.uri)

class SourceRemovedHook(SourceHook):
    __regid__ = 'cw.sources.removed'
    __select__ = SourceHook.__select__ & is_instance('CWSource')
    events = ('before_delete_entity',)
    def __call__(self):
        if self.entity.name == 'system':
            raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
        SourceRemovedOp(self._cw, uri=self.entity.name)