- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经构建了一个简单的 Django 照片应用程序。用户可以上传照片、关注其他用户和喜欢照片。为了处理用户之间的关系(关注和取消关注),我使用了一个名为 django-relationships 的包通过科尔弗。这是一个很棒的软件包,使用起来非常简单。
一切正常。我目前有一个工作事件提要。
我将提要过滤成两个部分:关注(我关注的用户的所有事件)和您(发生在我身上的所有事件)。我从我的 iOS 应用程序中发布了下面两张图片,该应用程序使用我的 Django 照片应用程序作为后端:
我想做的是将聚合添加到以下 Feed。如您所见,用户 alexperri 已点赞 5 次。我想将所有这些项目汇总到一行中。我不需要为“你”提要添加聚合,因为我希望看到发生在我身上的每个单独的操作。但是对于以下提要,添加聚合是有意义的。有几个应用程序可以很好地进行聚合。 Fashionlista、Pinterest 和 Instagram 在这方面做得很好。这是 Instagram 的一个示例,展示了我想要实现的目标:
在上面的示例中,您可以看到以下提要,并且 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)
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')
最佳答案
我认为您不想进行任何数据库级别的聚合,因为您可能想要显示各个详细信息以及计数,例如“X 喜欢 5 张照片”并显示 5 张照片。根据定义,聚合将排除单个数据。
相反,您应该使用 Python 代码(或 Javascript,因为我认为您使用的是 HTTP API,但我更喜欢已经组织好的服务器端 API)进行分组和排序。
itertools.groupby可能有帮助。我认为您需要按(用户和操作)分组,然后按每个组中第一项的时间戳排序,这样您可能会看到“乔喜欢 5 张照片”、“安妮发布了 2 张照片”、“乔发布了照片”、“克莱尔喜欢 3 张照片”等。
关于Django Activity Feed(Feedly 集成?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361878/
我已经作为发布者向 feedly 提交了一个站点提要。但我没有找到我有多少来自任何地方的订阅者。有什么方法可以检查订阅者数量吗? 最佳答案 尝试输入如下 curl http://cloud.feedl
我正在设计一个 Feeds 系统,一个人可以发布新闻,其他人可以看到彼此的新闻,就像 Twitter 一样。 现在我将新闻保存在 HBase 中,并将它们缓存在 Redis 中。这种方法有 O(1)
我在 atom 文件中显示图像时遇到问题。它不包括谷歌阅读器、歌剧或火狐的提要中的图像。 作为起点,我在 [Atom 1.0 Syndication Format 概述] 中执行了 list 6 中的
我已经构建了一个简单的 Django 照片应用程序。用户可以上传照片、关注其他用户和喜欢照片。为了处理用户之间的关系(关注和取消关注),我使用了一个名为 django-relationships 的包
我需要在网站中显示 Telegram channel 帖子。但我不知道如何将 Telegram channel 导出为 xml。我需要文本和图像以及其他文件和媒体,例如 mp4 - pdf 或其他内容
我正在使用Google Feed JSAPI读取/解析提要。问题是,当提要更改时,之前的条目将变得无效(链接和图像不起作用),因此我无法加载提要的缓存版本。我认为加载提要时会有一个选项不使用缓存版本,
我正在使用 Facebook 页面插件集成 Facebook 页面提要 https://developers.facebook.com/docs/plugins/page-plugin 虽然它几乎适用
我正在用 PHP 构建一个 RSS 提要聚合器/阅读器。由于 RSS 本质上是用户生成的内容,因此我不想依赖提要内容的安全性。 我正在寻求有关清理供稿内容以便在用户设备上存储和显示的建议。目前,我正在
因为我运行一个博客聚合器网站,它每小时检查大量 RSS 提要列表以获取新帖子,所以如果可以使用 google feed api 或 Google AJAX Feed API 我会很高兴而不是让 cro
嘿,我有这两个 RSS 源 - http://www.petrolprices.com/feeds/averages.xml?search_type=town&search_value=kilmarn
我无法使用 Google Feed API 加载图像,我正在使用 mediaGroup 加载图像。 这是我为获取提要而编写的 javascript 代码 google.load("feeds", "
很抱歉,如果之前有人问过这个问题 - 有一个标题相似的问题,但它不完全是我正在寻找的内容。 我正在做的是从数据库中获取结果并将其打印在适当的标签内以创建 RSS Feed。 唯一的问题是文章正文包含
我尝试发布消息来供稿,但她只显示在个人资料中。 如何使此消息显示在新闻源和个人资料源上? 这是我的示例代码: SBJSON *jsonWriter = [[SBJSON new] autoreleas
我正在尝试使用 Google feeds 将 RSS feed 添加到我的网站。问题是它限制了条目的数量。我只看到 4 个条目,但当我 curl RSS 时,我看到 28 个条目。我怎样才能让它加载其
我试图在不使用任何插件的情况下在 jQuery 中构建 rss 提要,我在这里找到了解决方案:designshack.net它使用不再使用的 Google Feed API。我发现解决方案很简单,但它
我从 Node.js 服务器连接到 Google Feed API(使用 https://stackoverflow.com/a/22821516/3303704 )。但每次我使用它时,它似乎都会加载
我正在使用以下代码获取提要: NSDictionary *dirTemp; NSError *error; NSStringEncoding encoding; NSString *strUrl =
为什么我的日历没有将 JSON Feed 中的数据放入我的网页上? $(document).ready(function() { var date = new Date(); var
旧版 Facebook News Feed 和新版之间是否存在问题? 我的位置开放图集合的输出之间存在冲突。 在旧的新闻提要中,我在使用 Open Graph 进行跨平台 checkin 时得到了这个
我正在尝试在 Django (Python) 环境中使用 Amazon API 为产品设置最高价格。我已经通过计算 md5 函数解决了这个问题,该函数的值与 Amazon MWS Scratchpad
我是一名优秀的程序员,十分优秀!