跳转到主要内容

使用celery发送异步通知。

项目描述

异步通知

使用celery发送电子邮件通知,并提供发送延迟电子邮件(例如每日)的行政视图

功能

  • Celery集成

  • 行政视图

  • 队列邮件系统并立即发送

  • 问题通知

  • 用户过滤电子邮件

  • 具有模板上下文的模板系统

  • 发送到用户、组或外部电子邮件

  • Django cms集成(djcms_async_notifications)和独立

  • 支持自定义文本区域小部件

  • 如果定义了,允许发送群组邮件列表。

  • 具有模型上下文、预览和额外电子邮件配置的通讯录

安装

  1. 从pip安装

    $ pip install async_notifications
  2. 在您的设置中添加所需的应用程序 **

    INSTALLED_APPS = [
        ...
        'async_notifications'
    ]
  3. 在urls.py中添加ajax_select URL

    from django.conf.urls import url, include
    
    urlpatterns = [
        ...
        url(r'^async_notifications/', include('async_notifications.urls')),
    ]
  4. 非常重要,设置CELERY_MODULE指向您的项目celery文件,因为它需要将任务分配给当前项目,并配置一些默认的celery选项

    # settings.py
    CELERY_MODULE = "demo.celery"
    CELERY_TIMEZONE = TIME_ZONE
    CELERY_ACCEPT_CONTENT = ['json']
  5. 配置您的电子邮件设置,例如开发环境

    DEFAULT_FROM_EMAIL="mail@example.com"
    EMAIL_HOST="localhost"
    EMAIL_PORT="1025"

# 从demo将celery应用程序复制到您的项目文件夹中,并调整crontab执行

# celery.py
app.conf.CELERYBEAT_SCHEDULE = {
    # execute 12:30 pm
    'send_daily_emails': {
        'task': 'async_notifications.tasks.send_daily',
        'schedule': crontab(minute=30, hour=0),

    },
}

请记住使用demo/__init__.py来更新您的projectfolder/__init__.py。

  1. 运行迁移

    $ python manage.py migrate

运行项目

您需要运行3个子系统才能运行此应用程序,因此您需要3个xterm,在此说明中我将使用demo项目

  1. 运行SMTP调试客户端

    $ python -m smtpd -n -c DebuggingServer localhost:1025
  2. 运行 celery,如果您还没有配置 celery,请参阅 celery 文档

    $ celery -A demo worker -l info -B
  3. 运行 django

    $ python manage.py runserver

用法

报告您的上下文模板

from async_notifications.register import update_template_context
context = [
    ('fieldname', 'Field description'),
    ('fieldname2', 'Field description'),
    ...
]
update_template_context("yourcode",  'your email subject', context )

如果未找到,将自动创建 Email 模板。

上下文是包含模板上下文中可用字段的元组列表,此上下文添加到包含 send_email_from_template 的同一文件中。

与 django 模板一起使用

update_template_context("yourcode",  'your email subject', context, 'templatepath.html', as_template=True )

现在可以发送字典作为上下文,但请注意,您不能在字典中重复键,因此请谨慎使用。

发送电子邮件 :)

send_email_from_template(code, recipient,
                         context={},
                         enqueued=True,
                         user=None,
                         upfile=None)

参数描述

  • recipient 是电子邮件列表

  • code 是在 update_template_context 中注册的相同代码

  • enqueued 如果 False 则立即发送电子邮件,否则排队等待发送电子邮件任务运行时发送。

  • user 发送电子邮件的用户

  • upfile 邮件中附加的文件

其他可选选项

添加上下文虚拟对象

当您需要传递基于模板的默认模板消息,但没有模板对象,并且需要使用 django 模板语法编写对象时,可以使用 DummyContextObject,它总是返回类似 {{ myobj.attr1.objattr }} 的内容。

from async_notifications.register import update_template_context, DummyContextObject
context = [
    ('myobj', 'Field description'),
    ...
]
message = render_to_string('some/template.html',
                       context={
                           'myobj': DummyContextObject('myobj')
                       }
                       )
update_template_context("yourcode",  'your email subject', context, message=message )

新闻通讯邮件额外配置

建议安装 django-markitup 和 markdown 以使用 django 模板系统生成预览模板,并配置 ASYNC_NEWSLETTER_WIDGET 以覆盖模板新闻通讯中的默认文本区域编辑器。

如果您想合并自定义邮件发送者,可以使用 ASYNC_NEWSLETTER_SEVER_CONFIGS 进行配置。

ASYNC_NEWSLETTER_SEVER_CONFIGS={
    'host': 'localhost',
    'port': '1025',
    'fail_silently': False,
    'backend': None,
    'from': 'From user <user@example.com>'
    'username':'my_username',
    'password':'my_password',
    'use_tls': True
}

新闻通讯设置

在您的应用中,在 admin.py 的末尾编辑以注册您的模型

register_model('app.model_label', model class, prefix='prefix used to include in template')
register_news_basemodel('app.model_label', Title, class manager)

要创建一个新的管理器,您需要创建一个类,例如

from async_notifications.interfaces import NewsLetterInterface
class MembershipManager(NewsLetterInterface):
    name = name used to include in template
    model = Model
    form = Filter form class

查看 NewsLetterInterface 了解需要覆盖哪些方法

Django CMS 集成

此配置可以帮助您与 Django CMS 集成。

包含在您的 INSTALLED_APPS

INSTALLED_APPS = [
    ...
  'async_notifications',
  'async_notifications.djcms_async_notifications',
]

配置模型和字段 async_notifications 将如何使用,例如 aldryn_people

ASYNC_NOTIFICATION_GROUP = 'aldryn_people.Group'
ASYNC_NOTIFICATION_GROUP_LOOKUP_FIELDS = {
    'order_by': 'translations__name',
    'email': 'email',
    'group_lookup': 'translations__name',
    'display': 'name',
    'filter': ['translations__name__icontains']}


ASYNC_NOTIFICATION_USER = 'aldryn_people.Person'

ASYNC_NOTIFICATION_USER_LOOKUP_FIELDS = {
    'order_by': 'translations__name',
    'display': 'name',
    'filter': [
        'user__first_name__icontains',
        'user__last_name__icontains',
        'translations__name__icontains'],
    'group_lookup': 'groups__translations__name'}

cmsplugin-contact-plus

CONTACT_PLUS_SEND_METHOD = 'async_notifications.djcms_async_notifications.contact_plus.send_email' ASYNC_NOTIFICATION_CONTACT_PLUS_EMAIL = 'email'

默认文本区域小部件

例如,使用 ckeditor 小部件

ASYNC_NOTIFICATION_TEXT_AREA_WIDGET = 'ckeditor.widgets.CKEditorWidget'

额外设置配置

  • ASYNC_NOTIFICATION_MAX_PER_MAIL 默认每封电子邮件 40 封邮件

  • ASYNC_BCC 包括始终在 BCC 中分开的电子邮件地址(没有空格),逗号分隔

  • ASYNC_SEND_ONLY_EMAIL 用于测试将所有电子邮件发送到此地址

  • ASYNC_SMTP_DEBUG 配置 smtp 调试以记录

  • ASYNC_TEMPLATES_NOTIFICATION 存储创建在电子邮件中的模板的路径

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

async_notifications-0.2.tar.gz (37.3 kB 查看哈希值)

上传时间

由以下提供支持