Django Activity Feed(Feedly 集成?)

标签 django tastypie feed django-aggregation

我已经构建了一个简单的 Django 照片应用程序。用户可以上传照片、关注其他用户和喜欢照片。为了处理用户之间的关系(关注和取消关注),我使用了一个名为 django-relationships 的包通过科尔弗。这是一个很棒的软件包,使用起来非常简单。

一切正常。我目前有一个工作事件提要。

我将提要过滤成两个部分:关注(我关注的用户的所有事件)和您(发生在我身上的所有事件)。我从我的 iOS 应用程序中发布了下面两张图片,该应用程序使用我的 Django 照片应用程序作为后端:

enter image description here
enter image description here

我想做的是将聚合添加到以下 Feed。如您所见,用户 alexperri 已点赞 5 次。我想将所有这些项目汇总到一行中。我不需要为“你”提要添加聚合,因为我希望看到发生在我身上的每个单独的操作。但是对于以下提要,添加聚合是有意义的。有几个应用程序可以很好地进行聚合。 Fashionlista、Pinterest 和 Instagram 在这方面做得很好。这是 Instagram 的一个示例,展示了我想要实现的目标:

enter image description here

在上面的示例中,您可以看到以下提要,并且 lovetoronto 赞了 5 张照片。我开始在 Instagram 上玩转,看看它是如何工作的。 Instagram 以下供稿最多显示 35 个事件条目,每个条目最多可以有 5 个该操作类型的事件。 “lovetoronto赞了5张照片”是一个事件条目,它显示了他最近点赞的5张照片。自从lovetoronto执行了最新的 Action ,他就位居榜首。

我想实现相同的设置。

这是我当前的模型设置:

模型.py

from django.db import models
from django.contrib.auth.models import User

class Photographer(models.Model):
    user = models.OneToOneField(User, primary_key=True
    likes = models.ManyToManyField('Photo', through = 'Likes', 
                                   related_name = 'likedby', blank = True)

class Photo(models.Model):
    photographer = models.ForeignKey(Photographer, related_name = 'shot_owner')
    created = models.DateTimeField(auto_now_add=True)
    url = models.CharField(max_length=128)

class Likes(models.Model):
    liked_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    photographer = models.ForeignKey(Photographer, related_name = 'liked_by')
    photo = models.ForeignKey(Photo, null=True)

class Activity(models.Model):
    actor = models.ForeignKey(Photographer, related_name = 'actor')
    receiver = models.ForeignKey(Photographer, related_name = 'receiver')
    action = models.CharField(max_length=12)
    post = models.ForeignKey(Photo, null=True, blank=True)
    time = models.DateTimeField(auto_now_add=True)

每次创建“Like”对象时,我也会创建一个 Activity 对象,参与者是执行该操作的人,接收者是执行该操作的人,该操作(在本例中为字符串,'喜欢的'),帖子(照片)和事件对象的创建时间。

我使用 django-tastypie 来获取和创建“喜欢”和“事件”对象。

api.py
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization
from photoapp.photodb.models import *
from tastypie.serializers import Serializer
from relationships.utils import positive_filter
from relationships.models import Relationship
from relationships.models import RelationshipStatus

class LikeResource(ModelResource):
    user = fields.ForeignKey(BasicUserResource, 'user', full=True)
    class Meta:
        queryset = Photographer.objects.all()
        allowed_methods = ['put']
        resource_name = 'like'
        fields = ['user']
        default_format = 'application/json'
        authorization = Authorization()
        authentication = BasicAuthentication()
        serializer = Serializer(formats=['json'])
        always_return_data = True
        include_resource_uri = False

        def hydrate(self, bundle):
            shot = Photo.objects.all().get(id = bundle.data['photo id'])
            user = Photographer.objects.all().get(user = bundle.request.user)
            if(bundle.obj.likes.filter(id = bundle.data['photo id']).exists()):
                Likes.objects.all().filter(photographer=user).filter(photo=shot).delete()

                Activity.objects.filter(actor__user = bundle.request.user,
                    post = shot, action = 'liked').delete()

            else:
                like = Likes(photographer = user, photo=shot)
                like.save()
                user_doing_the_liking = User.objects.get(
                    username=bundle.request.user.username)
                user = Photographer.objects.all().get(user = bundle.request.user)
                user_getting_liked = shot.photographer.user
                photographer_getting_liked = shot.photographer
                newActivity = Activity()
                newActivity.actor = user
                newActivity.receiver = photographer_getting_liked
                newActivity.action = 'liked'
                newActivity.post = shot
                newActivity.save()

    return bundle 

class FollowingFeed(ModelResource):
    actor = fields.ForeignKey(BasicPhotographerResource, 'actor', full=True)
    receiver = fields.ForeignKey(BasicPhotographerResource, 'receiver', full=True)
    post = fields.ForeignKey(BasicPostResource, attribute = 'post', full=True, null=True)
    class Meta:
        queryset = Activity.objects.all()
        allowed_methods = ['get']
        resource_name = 'following-feed'
        fields = ['actor', 'receiver', 'action', 'post', 'id', 'time']
        default_format = "application/json"
        authorization = Authorization()
        authentication = BasicAuthentication()
        serializer = Serializer(formats=['json'])
        always_return_data = True
        include_resource_uri = False

    def get_object_list(self, request):
        return super(FollowingFeed, self).get_object_list(request)\
            .filter(actor__user__in = request.user.relationships.following())\
            .exclude(receiver__user = request.user)\
            .exclude(actor__user = request.user).order_by('-time') 

如何以聚合事件对象的方式修改 FollowFeed 资源?我遇到了Feedly项目。如何在我当前的设置中使用它?

最佳答案

我认为您不想进行任何数据库级别的聚合,因为您可能想要显示各个详细信息以及计数,例如“X 喜欢 5 张照片”并显示 5 张照片。根据定义,聚合将排除单个数据。

相反,您应该使用 Python 代码(或 Javascript,因为我认为您使用的是 HTTP API,但我更喜欢已经组织好的服务器端 API)进行分组和排序。

itertools.groupby可能有帮助。我认为您需要按(用户和操作)分组,然后按每个组中第一项的时间戳排序,这样您可能会看到“乔喜欢 5 张照片”、“安妮发布了 2 张照片”、“乔发布了照片”、“克莱尔喜欢 3 张照片”等。

关于Django Activity Feed(Feedly 集成?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361878/

相关文章:

facebook - 七层 hell (Feed 问题、照片发布问题等)

python - Django 模板中的嵌套点查找

python - 一对一字段 Django Admin

python - Django:防止编辑/删除对象的更干的方法?

ruby-on-rails - neo4j 与 "traditional"RDBMS 和 Memcached/消息传递/Redis 解决方案

python - 新闻聚类程序不显示Python中的链接

python - 在开发与生产之间分离 Django 安装的应用程序

django - 尝试在 Heroku 上设置 Django 和 postgreSQL

python - 无法通过 django-tastypie 发布来保存具有外键的模型

python - Tastypie 。如何添加响应的执行时间?