Skip to content

biohub.notices

hsfzxjy edited this page Aug 3, 2017 · 2 revisions

概述

为使用户能接收系统中各种事件的通知(帖子被回复等等),biohub 引入通知系统。系统包括一个模型 Notice 和若干辅助发通知的工具。

models

Notice

描述

Notice 的一个实例代表一条通知。

字段

  • id
  • user:被通知的用户,related_namenotices
  • created:事件被触发的时间
  • message:通知正文
  • has_read:是否已阅的标志位
  • category:通知分类

实例属性

.mark_read()

将自己标为已读

QuerySet 方法

.user_notices(user)

筛选属于用户 user 的通知。

.unread()

筛选未读的通知。

.mark_read()

把当前结果集标为已读。

.categories()

返回当前结果集包含的分类列表。

.stats()

返回当前结果集中分类的统计信息,返回值类似于

[{
  "category": "A",
  "count": 10,
  "unread": 9
}, {
  "category": "B",
  "count": 5,
  "unread": 1
}]

tool

biohub.notices.tool 定义了通知派发器类 Dispatcher。同一个派发器生成的通知将具有相同的分类。为简化通知文本的编写,派发器允许你使用 Django Template Syntax 生成文本。

示例:

from biohub.notices.tool import Dispatcher

forum_dispatcher = Dispatcher('Forum') # 此后由 forum_dispatcher 分发的通知其 `category` 将自动被设为 'Forum'

# 假设 `user1.username` 为 `user1`,`user2.username` 为 `user2`

# 单发
forum_dispatcher.send(user1, '{{user.username}}-{{category}}') # 'user1-Forum'
forum_dispatcher.send(user1, '{{context.a}}', context={ 'a': 'Hello' }) # 'Hello'

# 群发
forum_dispatcher.group_send([user1, user2], '{{user.username}}') # 'user1','user2'

注意 当你需要在 message 中插入链接时,使用 url filter,如:

dispatcher.send(user1, 'Link: {{ "link title" | url:user1.api_url }}') # 'Link: [[link title]]((/api/users/1/))'

此 filter 会将参数转为 [[text]]((url)) 一类的文本,如此设计是为了给前端的渲染提供更大的自由度。

Clone this wiki locally