一个使用多个后端(数据库、Redis等)实现关注系统和时间轴的Django应用程序
项目描述
一个使用多个后端(数据库、Redis等)实现关注系统和时间轴的Django应用程序。
时间轴引擎可以在sequere.contrib.timeline中找到。
安装
从GitHub GitHub 检出包,或者通过PyPI拉取发布版本。
pip install django-sequere
将sequere添加到你的INSTALLED_APPS
INSTALLED_APPS = (
'sequere',
)
使用方法
在Sequere中,任何资源都可以关注任何资源,反之亦然。
假设你有两个模型
# models.py
from django.db import models
class User(models.Model):
username = models.CharField(max_length=150)
class Project(models.Model):
name = models.CharField(max_length=150)
现在你需要在Sequere中注册它们,以便在资源关注另一个资源时识别它们。
# sequere_registry.py
from .models import Project, User
import sequere
sequere.register(User)
sequere.register(Project)
Sequere使用与Django Admin相同的概念,所以如果你已经使用过它,你不会感到迷茫。
你现在可以使用Sequere就像使用任何其他应用程序一样,让我们来玩一玩
>>> from sequere.models import (follow, unfollow, get_followings_count, is_following,
get_followers_count, get_followers, get_followings)
>>> from myapp.models import User, Project
>>> user = User.objects.create(username='thoas')
>>> project = Project.objects.create(name='La classe americaine')
>>> follow(user, project) # thoas will now follow "La classe americaine"
>>> is_following(user, project)
True
>>> get_followers_count(project)
1
>>> get_followings_count(user)
1
>>> get_followers(user)
[]
>>> get_followers(project)
[(<User: thoas>, datetime.datetime(2013, 10, 25, 4, 41, 31, 612067))]
>>> get_followings(user)
[(<Project: La classe americaine, datetime.datetime(2013, 10, 25, 4, 41, 31, 612067))]
如果你和我一样懒惰,不想在每个Sequere调用中提供原始实例,可以使用SequereMixin
# models.py
from django.db import models
from sequere.mixin import SequereMixin
class User(SequereMixin, models.Model):
username = models.Charfield(max_length=150)
class Project(SequereMixin, models.Model):
name = models.Charfield(max_length=150)
现在你可以直接从实例调用
>>> from myapp.models import User, Project
>>> user = User.objects.create(username='thoas')
>>> project = Project.objects.create(name'La classe americaine')
>>> user.follow(project) # thoas will now follow "La classe americaine"
>>> user.is_following(project)
True
>>> project.get_followers_count()
1
>>> user.get_followings_count()
1
>>> user.get_followers()
[]
>>> project.get_followers()
[(<User: thoas>, datetime.datetime(2013, 10, 25, 4, 41, 31, 612067))]
>>> user.get_followings()
[(<Project: La classe americaine, datetime.datetime(2013, 10, 25, 4, 41, 31, 612067))]
真是有趣!
后端
sequere.backends.database.DatabaseBackend
一个数据库后端,使用Django的ORM将你的关注存储在你喜欢的数据库中。
要使用这个后端,您需要将 sequere.backends.database 添加到您的 INSTALLED_APPS 中。
INSTALLED_APPS = (
'sequere',
'sequere.backends.database',
)
关注者将通过 (from_identifier, from_object_id) 对来识别,而被关注者通过 (to_identifier, to_object_id) 对来识别。
每个标识符都来自注册表。例如,如果您想从一个模型创建一个自定义标识键,可以按如下方式自定义:
# sequere_registry.py
from myapp.models import Project
from sequere.base import ModelBase
import sequere
class ProjectSequere(ModelBase):
identifier = 'projet' # the french way ;)
sequere.registry(Project, ProjectSequere)
sequere.backends.redis.RedisBackend
在这个 Redis 实现中,我们只使用 有序集合。
为新的资源创建一个 uid
INCR sequere:global:uid => 1 SET sequere:uid:{identifier}:{id} 1 HMSET sequere:uid::{id} identifier {identifier} object_id {id}
存储关注者数量
INCR sequere:uid:{to_uid}:followers:count => 1 INCR sequere:uid:{to_uid}:followers:{from_identifier}:count => 1
存储被关注者数量
INCR sequere:uid:{from_uid}:followings:count => 1 INCR sequere:uid:{from_uid}:followings:{to_identifier}:count => 1
添加新的关注者
ZADD sequere:uid:{to_uid}:followers {from_uid} {timestamp} ZADD sequere:uid:{to_uid}:followers:{from_identifier} {from_uid} {timestamp}
添加新的被关注者
ZADD sequere:uid:{from_uid}:followings {to_uid} {timestamp} ZADD sequere:uid:{from_uid}:followings{to_identifier} {to_uid} {timestamp}
检索关注者的 uid
ZRANGEBYSCORE sequere:uid:{uid}:followers -inf +inf
检索被关注者的 uid
ZRANGEBYSCORE sequere:uid:{uid}:followings =inf +inf
使用此实现,您可以按顺序检索您的关注者
ZREVRANGEBYSCORE sequere:uid:{uid}:followers +inf -inf
时间线
时间线引擎直接基于 sequere 资源系统。
概念
时间线基本上是动作列表。
动作表示为:
actor,动作的执行者
verb,动作名称
target,动作的目标(非必需)
date,动作执行的日期
安装
在安装 sequere.contrib.timeline 之前,您必须首先遵循 sequere 的安装说明。
将 sequere.contrib.timeline 添加到您的 INSTALLED_APPS 中
INSTALLED_APPS = (
'sequere.contrib.timeline',
)
sequere.contrib.timeline 需要使用 celery 才能正常工作,因此您需要安装它。
使用方法
您需要根据您的资源注册您的动作,例如
# sequere_registry.py
from .models import Project, User
from sequere.contrib.timeline import Action
from sequere import register
from sequere.base import ModelBase
# actions
class JoinAction(Action):
verb = 'join'
class LikeAction(Action):
verb = 'like'
# resources
class ProjectSequere(ModelBase):
identifier = 'project'
class UserSequere(ModelBase):
identifier = 'user'
actions = (JoinAction, LikeAction, )
# register resources
register(User, UserSequere)
register(Project, ProjectSequere)
现在我们已经注册了我们的动作,我们可以玩时间线 API 了
>>> from sequere.models import (follow, unfollow)
>>> from sequere.contrib.timeline import Timeline
>>> from myapp.models import User, Project
>>> from myapp.sequere_registry import JoinAction, LikeAction
>>> thoas = User.objects.create(username='thoas')
>>> project = Project.objects.create(name='La classe americaine')
>>> timeline = Timeline(thoas) # create a timeline
>>> timeline.save(JoinAction(actor=thoas)) # save the action in the timeline
>>> timeline.get_private()
[<JoinAction: thoas join>]
>>>: timeline.get_public()
[<JoinAction: thoas join>]
当资源是自身动作的执行者时,我们将动作同时推送到 私人 和 公共 时间线。
现在我们必须通过关注过程测试系统
>>> newbie = User.objects.create(username='newbie')
>>> follow(newbie, thoas) # newbie is now following thoas
>>> Timeline(newbie).get_private() # thoas actions now appear in the private timeline of newbie
[<JoinAction: thoas join>]
>>> Timeline(newbie).get_public()
[]
当 A 关注 B 时,我们在 A 的私人时间线中复制 B 的动作,celery 需要处理这些异步任务。
>>> unfollow(newbie, thoas)
>>> Timeline(newbie).get_private()
[]
当 A 取消关注 B 时,我们从 A 的私人时间线中删除 B 的动作。
如您所注意到的,JoinAction 是一个不需要目标的动作,一些动作将需要目标,sequere.contrib.timeline 提供了一种快速查询特定目标的动作的方法。
>>> timeline = Timeline(thoas)
>>> timeline.save(LikeAction(actor=thoas, target=project))
>>> timeline.get_private()
[<JoinAction: thoas join>, <LikeAction: thoas like La classe americaine>]
>>> timeline.get_private(target=Project) # only retrieve actions with Project resource as target
[<LikeAction: thoas like La classe americaine>]
>>> timeline.get_private(target='project') # only retrieve actions with 'project' identifier as target
[<LikeAction: thoas like La classe americaine>]
配置
SEQUERE_BACKEND
用于存储关注的后端
默认为 sequere.backends.database.DatabaseBackend。
SEQUERE_BACKEND_OPTIONS
传递给后端的参数字典,例如使用 redis
SEQUERE_BACKEND = 'sequere.backends.redis.RedisBackend'
SEQUERE_BACKEND_OPTIONS = {
'client_class': 'myproject.myapp.mockup.Connection',
'options': {
'host': 'localhost',
'port': 6379,
'db': 0,
},
'prefix': 'prefix-used:'
}
在 Redis 数据库中存储键时使用的(可选)前缀。
默认为 sequere:。
SEQUERE_TIMELINE_BACKEND
用于存储关注的后端
默认为 sequere.contrib.timeline.redis.RedisBackend。
SEQUERE_TIMELINE_BACKEND_OPTIONS
传递给后端的参数字典,例如使用 redis
SEQUERE_TIMELINE_BACKEND = 'sequere.contrib.timeline.redis.RedisBackend'
SEQUERE_TIMELINE_BACKEND_OPTIONS = {
'client_class': 'myproject.myapp.mockup.Connection',
'options': {
'host': 'localhost',
'port': 6379,
'db': 0,
},
'prefix': 'prefix-used:'
}
在 Redis 数据库中存储键时使用的(可选)前缀。
默认为 sequere:timeline:。
资源
haplocheirus:由 Scala 编写的 Redis 支持的时间线存储引擎
Redis 文档中的案例研究:编写一个推特克隆
Amico:由 Redis 支持的关系
django-constance:一个多后端设置管理应用程序
项目详情
django-sequere-0.4.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5308c1401974d792af25e0bdff4c858892186d5a9356fe8353c554930072aca3 |
|
MD5 | 95ad7f9784dcb56bf51dea957d00851f |
|
BLAKE2b-256 | f9b7076498ed80d4f198066cbc020d3bd920c6b21f9232d4e3974f8a495da77c |