def action_handler(verb, **kwargs): """ qevent.signals.action 的处理函数 """ kwargs.pop('signal', None) actor = kwargs.pop('sender') if hasattr(verb, '_proxy____args'): verb = verb . _proxy____args[0] event = Action( actor_type=get_contenttype(actor), actor_object_id=actor.pk if actor else None, verb=str(verb), timestamp=kwargs.pop('timestamp', timezone.now()), description=kwargs.pop('description', None), private=bool(kwargs.pop('private', False)), ) for opt in ('target', 'relative'): obj = kwargs.pop(opt, None) if obj is not None: check(obj) setattr(event, opt+'_type', get_contenttype(obj)) setattr(event, opt+'_object_id', obj.pk) hasattr(event, 'data') and len(kwargs) and setattr(event, 'data', kwargs) event.save() return event
def content(self, model, **kwargs): """ 指定 model 的所有 actions """ check(model) ct = get_contenttype(model) return self.public( (models.Q(actor_type_id=ct.pk) | models.Q(target_type_id=ct.pk) | models.Q(relative_type_id=ct.pk) ), **kwargs)
def _object_actions(self, obj): check(obj) ct = get_contenttype(obj) return models.Q( actor_type_id=ct.pk, actor_object_id=obj.pk, ) | models.Q( target_type_id=ct.pk, target_object_id=obj.pk, ) | models.Q( relative_type_id=ct.pk, relative_object_id=obj.pk, )
def relative(self, obj, **kwargs): """ 指定 object AS relative 的所有 actions """ check(obj) return obj.relative_actions.public(**kwargs)
def target(self, obj, **kwargs): """ 指定 object AS target 的所有 actions """ check(obj) return obj.target_actions.public(**kwargs)
def actor(self, obj, **kwargs): """ 指定 object AS actor 的所有 actions """ check(obj) return obj.actor_actions.public(**kwargs)