equal
deleted
inserted
replaced
|
1 from cubicweb import ValidationError |
|
2 from cubicweb.selectors import is_instance |
|
3 from cubicweb.server import hook |
|
4 |
|
5 class SourceHook(hook.Hook): |
|
6 __abstract__ = True |
|
7 category = 'cw.sources' |
|
8 |
|
9 |
|
10 class SourceAddedOp(hook.Operation): |
|
11 def precommit_event(self): |
|
12 self.session.repo.add_source(self.entity) |
|
13 |
|
14 class SourceAddedHook(SourceHook): |
|
15 __regid__ = 'cw.sources.added' |
|
16 __select__ = SourceHook.__select__ & is_instance('CWSource') |
|
17 events = ('after_add_entity',) |
|
18 def __call__(self): |
|
19 SourceAddedOp(self._cw, entity=self.entity) |
|
20 |
|
21 |
|
22 class SourceRemovedOp(hook.Operation): |
|
23 def precommit_event(self): |
|
24 self.session.repo.remove_source(self.uri) |
|
25 |
|
26 class SourceRemovedHook(SourceHook): |
|
27 __regid__ = 'cw.sources.removed' |
|
28 __select__ = SourceHook.__select__ & is_instance('CWSource') |
|
29 events = ('before_delete_entity',) |
|
30 def __call__(self): |
|
31 if self.entity.name == 'system': |
|
32 raise ValidationError(self.entity.eid, {None: 'cant remove system source'}) |
|
33 SourceRemovedOp(self._cw, uri=self.entity.name) |