【发布时间】:2013-12-20 03:26:26
【问题描述】:
我已经构建了一个简单的 Django 照片应用程序。用户可以上传照片、关注其他用户和喜欢照片。为了处理用户之间的关系(关注和取消关注),我使用 coleifer 的一个名为 django-relationships 的包。这是一个很棒的软件包,使用起来非常简单。
一切正常。我目前有一个工作活动提要。
我将提要过滤为两个部分:关注(我关注的用户的所有活动)和您(发生在我身上的所有活动)。我从我的 iOS 应用程序中发布了下面两张图片,该应用程序使用我的 Django 照片应用程序作为后端:
我想做的是将聚合添加到以下 Feed。如您所见,用户 alexperri 已点赞 5 次。我想将所有这些项目汇总到一行中。我不需要为“你”提要添加聚合,因为我希望看到发生在我身上的每个单独的操作。但是对于以下提要,添加聚合是有意义的。有几个应用程序可以很好地进行聚合。 Fashionlista、Pinterest 和 Instagram 在这方面做得很好。这是 Instagram 的一个示例,展示了我想要实现的目标:
在上面的示例中,您可以看到以下提要,并且 lovetoronto 赞了 5 张照片。我开始在 Instagram 上玩转,看看它是如何工作的。 Instagram 以下供稿最多显示 35 个活动条目,每个条目最多可以有 5 个该操作类型的活动。 “lovetoronto赞了5张照片”是一个活动条目,它显示了他最近点赞的5张照片。自从lovetoronto执行了最新的动作,他就位居榜首。
我想实现相同的设置。
这是我当前的模型设置:
models.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 对象,actor 是执行该操作的人,接收者是执行该操作的人,该操作(在本例中为string, 'liked'), post(照片) 和活动对象的创建时间。
我使用 django-tastypie 来获取和创建“Like”和“Activity”对象。
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 项目。如何在我当前的设置中使用它?
【问题讨论】:
-
你还需要按
action分组吗?除了liked,还有其他动作类型吗? -
@mariodev 感谢您的快速回复。是的,我想按用户分组,然后按他们所做的操作进行分组。此外还有其他动作。有追随、评论和提及。差不多就是这样。因此,我不想让提要显示每个 alexperri 的类似活动,而是汇总它们。 “Alexperri 点赞了 5 张照片”(显示最近一小时内最新的 5 张照片……即使 Alexperri 在过去一小时内点赞的照片超过 5 张,我也只想显示最新的 5 张)。
-
@mariodev,如果 alexperri 点赞了 10 张图片,并且 alexperri 在最后一小时关注了 bob 和 kyle。我想按最近执行的操作进行排序。因此,如果 alexperri 的最新动作是“点赞”,并且如果他是我关注的所有人中最新做出动作的人,我会检查我关注的所有人的最后 100 条活动,获取最新的 5喜欢该列表中来自 alexperri 的操作并将其汇总(alexperri 喜欢 5 张照片)。然后我会检查我关注的人中的下一个人是谁做了第二次最新的行动。运行相同的逻辑。等等……
-
那么其余的操作是如何工作的.. 如果关注,您是否列出最新关注的用户?对于每种操作类型,您会列出什么?在我看来,
Activity表仅用于喜欢.. -
@mariodev 查看 LikeResource,我在其中创建了 newActivity = Activity()。我对 FollowResource、CommentResource 和 MentioningResource 做同样的事情。我只是没有将这些资源添加到问题中,因为问题太大了。我想让这个问题变得简单。例如,在 MentioningResource 中,我执行提及所需的逻辑,然后执行 newActivity = Activity()。 newActivity.action 将是“提及”。 NewActivity 接收者 = user_being_提到。 newActivity.post= 拍摄。 newActivity.save() 这有意义吗?如果我不清楚,请告诉我
标签: django tastypie feed django-aggregation