我已经构建了一个简单的 Django 照片应用程序。用户可以上传照片、关注其他用户和喜欢照片。为了处理用户之间的关系(关注和取消关注),我使用了coleifer的一个名为django-relationships的包。这是一个很棒的软件包,使用起来非常简单。
一切正常。我目前有一个工作活动提要。
我将提要过滤为两个部分:关注(我关注的用户的所有活动)和您(发生在我身上的所有活动)。我从我的 iOS 应用程序中发布了下面两张图片,该应用程序使用我的 Django 照片应用程序作为后端:
我想做的是将聚合添加到以下 Feed。如您所见,用户 alexperri 已点赞 5 次。我想将所有这些项目汇总到一行中。我不需要为“你”提要添加聚合,因为我希望看到发生在我身上的每个单独的操作。但是对于以下提要,添加聚合是有意义的。有几个应用程序可以很好地进行聚合。Fashionlista、Pinterest 和 Instagram 在这方面做得很好。这是 Instagram 的一个示例,展示了我想要实现的目标:
在上面的示例中,您可以看到以下提要,并且 lovetoronto 赞了 5 张照片。我开始在 Instagram 上玩转,看看它是如何工作的。Instagram 以下供稿最多显示 35 个活动条目,每个条目最多可以有 5 个该操作类型的活动。“lovetoronto赞了5张照片”是一个活动条目,它显示了他最近点赞的5张照片。自从lovetoronto执行了最新的动作,他就位居榜首。
我想实现相同的设置。
这是我当前的模型设置:
模型.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项目。如何在我当前的设置中使用它?