django web框架的用户通知管理

geonode-notification的Python项目详细描述


用法

将通知支持集成到应用程序中是一个简单的三步过程。

  • create your notice types
  • create your notice templates
  • send notifications

创建通知类型

您需要调用create_notice_type(label, display, description)一次 在数据库中为应用程序创建通知类型。label只是 将用于类型display的内部短名称是 用户将看到通知类型的名称,并且说明是 简短的描述。

例如:

notification.create_notice_type("friends_invite", "Invitation Received", "you have received an invitation")

自动创建通知类型的一个好方法是 management.py应用程序的文件,附加到syncdb信号。 下面是一个示例:

from django.conf import settings
from django.db.models import signals
from django.utils.translation import ugettext_noop as _

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def create_notice_types(app, created_models, verbosity, **kwargs):
        notification.create_notice_type("friends_invite", _("Invitation Received"), _("you have received an invitation"))
        notification.create_notice_type("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))

    signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
    print "Skipping creation of NoticeTypes as notification app not found"

注意,代码被包装在一个条件子句中,因此如果 未安装django通知,您的应用程序仍将继续。

注意,display和description参数被标记为 使用ugettext_noop。这将使您能够使用Django的makeMessages 管理命令和使用django通知的i18n功能。

通知模板

对于通知的实际内容,可以写入四个不同的模板:

  • ^{tt5}$ is a very short, text-only version of the notice (suitable for things like email subjects)
  • ^{tt6}$ is a longer, text-only version of the notice (suitable for things like email bodies)
  • ^{tt7}$ is a short, html version of the notice, displayed in a user’s notice list on the website
  • ^{tt8}$ is a long, html version of the notice (not currently used for anything)

每一个都应该放在模板路径上名为notification/<notice_type_label>/<template_name>的目录中。 如果其中任何一个丢失,将使用默认值。实际上,应该至少提供notice.htmlfull.txt

例如,notification/friends_invite/notice.html可能包含:

{% load i18n %}{% url invitations as invitation_page %}{% url profile_detail username=invitation.from_user.username as user_url %}
{% blocktrans with invitation.from_user as invitation_from_user %}<a href="{{ user_url }}">{{ invitation_from_user }}</a> has requested to add you as a friend (see <a href="{{ invitation_page }}">invitations</a>){% endblocktrans %}

并且notification/friends/full.txt可能包含:

{% load i18n %}{% url invitations as invitation_page %}{% blocktrans with invitation.from_user as invitation_from_user %}{{ invitation_from_user }} has requested to add you as a friend. You can accept their invitation at:

http://{{ current_site }}{{ invitation_page }}
{% endblocktrans %}

发送通知时提供上下文变量。

发送通知

发送通知有两种不同的方式。我们有支持 用于发送通知的阻塞和非阻塞方法。最多的 发送通知的简单方法,例如:

notification.send([to_user], "friends_invite", {"from_user": from_user})

需要注意的是send是围绕send_nowqueue。它们都有相同的签名:

send(users, label, extra_context)

参数为:

  • ^{tt17}$ is an iterable of ^{tt18}$ objects to send the notification to.
  • ^{tt2}$ is the label you used in the previous step to identify the notice type.
  • ^{tt20}$ is a dictionary to add custom context entries to the template used to render to notification. This is optional.

send_nowvs.queuevs.send

让我们先分析一下每个人的工作。

send_now

这是一个阻塞调用,它将检查每个用户 请注意并实际发送。

queue

这是一个非阻塞调用,它将把调用排队到send_nowto 以后再执行。以后执行需要使用的调用 emit_notices管理命令。

send

围绕send_nowqueue的代理。它的行为来自全球 正在设置名为NOTIFICATION_QUEUE_ALL。默认情况下是False。这个 设置旨在帮助控制是否要将任何呼叫排队到 send

send还接受nowqueue关键字参数。默认情况下 每个选项都设置为False,以执行全局设置False。 这使您能够在每次调用的基础上覆盖它是否应该调用 send_nowqueue

可选通知支持

如果您想在可重用的应用程序中使用django通知,可以 在测试的条件子句中包装django通知的导入 如果是在发送通知之前安装的。因此,您的应用程序或 项目仍在不通知的情况下运行。

例如:

from django.conf import settings

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

然后,稍后:

if notification:
    notification.send([to_user], "friends_invite", {"from_user": from_user})

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
javajavax。艾尔。PropertyNotFoundException:   java为什么通过TCP发送的文件比它本身包含的文件包含更多的数据?   java为什么字符开关/案例不起作用?   php到java连接器,在哪里可以找到好的连接器   需要帮助Java简单规则形状面积计算器和if语句吗   macos如何从newstyle Oracle Java OSX捆绑包结构启动帮助页?   java既然所有的类都扩展对象,而对象是一个类,那么对象如何扩展对象呢?   java从JavaPairdd<String,Tuple2<Integer,Integer>>转换为JavaPairdd<String,Integer>   java微调器值未从一个活动传递到另一个活动   参数化类型的java数组   java不提供类。getCanonicalName是否存在性能问题?   java输入键JTextField   为什么我不能在java中调用nextLine()方法两次?   JTextPane中的java JProgressBar   java如何获取Crudepository实例?   用于提取几个<div>标记的java正则表达式   java如何使用JUnit Testrunner生成html图像   Go中Java静态属性的等价性