Django-Notification - 邮件后端替代方案
我正在用Django做一个项目,现在想用django-notification来跟踪用户的活动。我已经成功安装了它,并创建了一些通知,但这些通知只通过电子邮件发送,并没有存储在数据库里,所以我无法在信息流中显示它们。
目前访问/notifications/feed/时出现了类型错误,我不确定这是否相关?
类型错误在/notifications/feed/: init()需要3个参数(只给了1个)
如果有任何建议,我将非常感激。我查看了Pinax是如何使用通知的,但没弄明白他们是如何解决仅通过电子邮件发送的问题的。
在settings.py中,我已经启用了'notification',并且添加了模板上下文处理器'notification.context_processors.notification'。
urls.py
url(r'^note/', include('notification.urls')),
app/management.py
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("messages_received", _("Message Received"), _("you have received a message"), default=2)
signals.post_syncdb.connect(create_notice_types, sender=notification)
app/view.py
...
if notification:
notification.send([user], "messages_received", {'message': message,})
...
我检查过notification.send这个功能被执行了,但似乎没有任何内容存储在"notice"数据库里……
我还要补充一下,我正在使用django-notification的Brian Rosner分支(https://github.com/brosner/django-notification)。
1 个回答
1
看起来,brosner对django-notifications的修改和jtauber的版本有些不同。具体来说,send_now()
这个方法实际上并不会把通知实例保存到数据库里,默认的EmailBackend
通知后端也是这样。
你需要自己写一个通知后端类,当调用deliver()
时,它会创建一个通知实例,并把它添加到NOTIIFICATION_BACKENDS
里。
下面是一个(未经测试的)示例,模仿jtauber的行为:
class MyBackend(BaseBackend):
def deliver(self, recepient, sender, notice_type, extra_context):
messages = self.get_formatted_messages(["notice.html"],
notice_type.label, extra_context)
notice = Notice.objects.create(recipient=recepient,
message=messages['notice.html'], notice_type=notice_type,
on_site=on_site, sender=sender)
notice.save()