示例#1
0
文件: models.py 项目: gzqichang/wa
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
示例#2
0
文件: models.py 项目: gzqichang/wa
 def register(self, models={}):
     for alias, conf in models.items():
         handler = self.supported_content_type.setdefault(alias, PurifyHandler())
         handler.model = model = get_model(conf['model'])
         handler.manager = model.objects
         handler.content_type = get_contenttype(model)
         handler.methods = conf.get('methods', {})
示例#3
0
文件: models.py 项目: gzqichang/wa
 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)
示例#4
0
文件: models.py 项目: gzqichang/wa
 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,
     )
示例#5
0
文件: registry.py 项目: gzqichang/wa
def setup_generic_relations(model_actor, relation_methods):
    """
    注册过的 actor_model_class 实例可以如此操作:
        actor.METHOD(target) # 建立 target 的特定 RELATION
        actor.dont_METHOD(target) # 或 no_METHOD 取消 target 的特定 RELATION
        #
        actor.actor_relations.filter(...)
        actor.METHOD_relations(...) # AS actor 的特定 RELATION filter
        actor.has_METHOD(target) # 与 target 是否存在特定 RELATION
    target_model_instance 可以有如下操作:
        target.target_relations.filter(...)
        target.METHOD_related(...) # AS target 的特定 RELATION filter
        target.was_METHOD(actor) # 与 actor 是否存在特定 RELATION
    owner_model_instance 可以有如下操作:
        owner.owner_relations.filter(...)
        owner.METHOD_related(...) # AS owner 的特定 RELATION filter
    """
    from qrelation import models
    #
    rel = GenericRelation(models.Relation,
        content_type_field='actor_type',
        object_id_field='actor_id',
        related_query_name='relations_with_%s_as_%s' % (label(model_actor), 'actor'))
    rel.contribute_to_class(model_actor, 'actor_relations')
    #
    actor_type = get_contenttype(model_actor)
    for method, kwargs in relation_methods.items():
        # 'follow': {
        #     'relation': models.REL_USER_FOLLOW,
        #     'target': 'quser.User',
        # }
        model_target = validate(kwargs.pop('target'))
        if not hasattr(model_target, 'target_relations'):
            rel = GenericRelation(models.Relation,
                content_type_field='target_type',
                object_id_field='target_id',
                related_query_name='relations_with_%s_as_%s' % (label(model_target), 'target'))
            rel.contribute_to_class(model_target, 'target_relations')
        model_owner = kwargs.pop('owner', None)
        if model_owner:
            model_owner = validate(model_owner)
            owner_type = get_contenttype(model_owner)
        if model_owner and not hasattr(model_owner, 'owner_relations'):
            rel = GenericRelation(models.Relation,
                content_type_field='owner_type',
                object_id_field='owner_id',
                related_query_name='relations_with_%s_as_%s' % (label(model_owner), 'owner'))
            rel.contribute_to_class(model_owner, 'owner_relations')
        #
        target_type = get_contenttype(model_target)
        relation, owner_field = kwargs.pop('relation'), kwargs.pop('owner_field', None)
        # 建立 relation
        setattr(model_actor, method, functools.partialmethod(
            relate, relation=relation, target_type=target_type, model_target=model_target, actor_type=actor_type, owner_field=owner_field))
        cancel_method = functools.partialmethod(
            relate, relation=relation, target_type=target_type, model_target=model_target, actor_type=actor_type, deleted=True)
        # 取消 relation
        setattr(model_actor, 'dont_%s' % method, cancel_method)
        setattr(model_actor, 'no_%s' % method, cancel_method)
        # 给 actor_model 增加类方法 METHOD_relations
        setattr(model_actor, '%s_relations' % method, functools.partialmethod(
            filter_relations, relation=relation))
        # 给 actor_model 增加类方法 has_METHOD
        setattr(model_actor, 'has_%s' % method, functools.partialmethod(
            check_relation, relation=relation, target_type=target_type, model_target=model_target))
        # 给 target_model 增加类方法 METHOD_related
        setattr(model_target, '%s_related' % method, functools.partialmethod(
            filter_related, relation=relation))
        # 给 target_model 增加类方法 was_METHOD
        setattr(model_target, 'was_%s' % method, functools.partialmethod(
            check_related, relation=relation, actor_type=actor_type, model_actor=model_actor))
        if not model_owner:
            continue
        # 给 owner_model 增加类方法 
        setattr(model_owner, '%s_related' % method, functools.partialmethod(
            filter_owner, relation=relation))