fromyams.schemaimportrole_namefromcubicwebimportValidationErrorfromcubicweb.selectorsimportis_instancefromcubicweb.serverimportSOURCE_TYPES,hookclassSourceHook(hook.Hook):__abstract__=Truecategory='cw.sources'classSourceAddedOp(hook.Operation):defpostcommit_event(self):self.session.repo.add_source(self.entity)classSourceAddedHook(SourceHook):__regid__='cw.sources.added'__select__=SourceHook.__select__&is_instance('CWSource')events=('after_add_entity',)def__call__(self):ifnotself.entity.typeinSOURCE_TYPES:msg=self._cw._('unknown source type')raiseValidationError(self.entity.eid,{role_name('type','subject'):msg})SourceAddedOp(self._cw,entity=self.entity)classSourceRemovedOp(hook.Operation):defprecommit_event(self):self.session.repo.remove_source(self.uri)classSourceRemovedHook(SourceHook):__regid__='cw.sources.removed'__select__=SourceHook.__select__&is_instance('CWSource')events=('before_delete_entity',)def__call__(self):ifself.entity.name=='system':raiseValidationError(self.entity.eid,{None:'cant remove system source'})SourceRemovedOp(self._cw,uri=self.entity.name)classSourceRemovedHook(SourceHook):__regid__='cw.sources.removed'__select__=SourceHook.__select__&hook.match_rtype('cw_support','cw_may_cross')events=('after_add_relation',)def__call__(self):entity=self._cw.entity_from_eid(self.eidto)ifentity.__regid__=='CWRType'andentity.namein('is','is_instance_of','cw_source'):msg=self._cw._('the %s relation type can\'t be used here')%entity.nameraiseValidationError(self.eidto,{role_name(self.rtype,'subject'):msg})