【问题标题】:How is django-messages creating notification types during syncdb?django-messages 如何在 syncdb 期间创建通知类型?
【发布时间】:2012-08-12 03:37:56
【问题描述】:

我同时使用 django-notificationdjango-messages 项目,并利用 django-messages 中内置的 django-notifications 集成及其默认通知类型,用于接收、回复等消息。

但是,我无法确定这些默认 NoticeType 对象是如何创建的。 django-notification 文档建议在 management.py 文件中使用 post_syncdb 信号,这是我为自己的自定义通知所做的。我在任何代码中都找不到定义这些通知类型的任何地方。然而,每次我在新数据库上运行 syncdb 时,它们都会神奇地出现。

django-messages 应用程序正在创建的通知类型的“标签”属性如下:

  • messages_received
  • messages_sent
  • messages_replied
  • messages_reply_received
  • messages_deleted
  • messages_recovered

【问题讨论】:

    标签: python django django-notification


    【解决方案1】:

    django_messages/management.py:

    from django.db.models import get_models, signals
    from django.conf import settings
    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("messages_received", _("Message Received"), _("you have received a message"), default=2)
            notification.create_notice_type("messages_sent", _("Message Sent"), _("you have sent a message"), default=1)
            notification.create_notice_type("messages_replied", _("Message Replied"), _("you have replied to a message"), default=1)
            notification.create_notice_type("messages_reply_received", _("Reply Received"), _("you have received a reply to a message"), default=2)
            notification.create_notice_type("messages_deleted", _("Message Deleted"), _("you have deleted a message"), default=1)
            notification.create_notice_type("messages_recovered", _("Message Recovered"), _("you have undeleted a message"), default=1)
    
        signals.post_syncdb.connect(create_notice_types, sender=notification)
    else:
        print "Skipping creation of NoticeTypes as notification app not found"
    

    https://github.com/arneb/django-messages/blob/master/django_messages/management.py

    类型在此处定义并连接到 post_syncdb 信号。

    【讨论】:

    • 好吧,我现在觉得自己蠢得可笑。我一直在 Eclipse/PyDev 中查看它,它隐藏了 .pyc 文件。出于某种原因,management.py 不存在,但 management.pyc 存在。这正是我一直在寻找的,但还不够难...... :-(
    • 天色已晚。是时候喝杯啤酒了!
    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2011-05-31
    相关资源
    最近更新 更多