[selectors] modify workflow selectors: is_in_state, on_transition
- factorize `is_on_state` selector
- add new `on_transition` selector
Especially useful to match pending transitions to enable notifications when
your workflow allows several transition to the same states.
Note that if workflow `change_state` adapter method is used, this selector
will not be triggered.
In debug mode:
These both selectors will check against the entity current workflow if expected
values given in selector argument are valid.
ValueError exception will be raised for unmatching state/transition
names against the current workflow (generic etype workflow).
(check against custom workflow is not implemented)
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)