我已经构建了一个简单的Django照片应用程序.用户可以上传照片,关注其他用户和照片.为了处理用户之间的关系(跟随和取消关注),我使用了一个名为django-relations by coleifer的软件包.这是一个很棒的包,使用起来非常简单.
一切都按预期工作.我目前有一个工作活动Feed.
我将Feed分为两部分:跟随(我关注的用户的所有活动)和你(发生在我身上的所有活动).我在我的iOS应用程序下面发布了两张图片,它使用我的Django照片应用程序,因为它是后端:
我想要做的是添加聚合到以下Feed.正如你所看到的,用户alexperri喜欢5次射击.我想将所有这些项目合并为一行.我不需要为"你"提要添加聚合,因为我希望看到每个单独的操作都发生在我身上.但是对于以下Feed,添加聚合是有意义的.有几个应用程序可以很好地进行聚合.Fashionlista,Pinterest和Instagram做得很好.这是一个来自Instagram的例子来展示我想要实现的目标:
在上面的示例中,您可以看到以下Feed,而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是执行动作的人,接收者是动作完成的人,动作(在这种情况下是一个字符串,'喜欢'),发布(照片)和创建活动对象的时间.
我使用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')
如何以一种聚合活动对象的方式修改FollowingFeed资源?我遇到了Feedly项目.如何在当前设置中使用它?