用于分离消息内容和传输方法的django库

mindbogglr-django-herald的Python项目详细描述


django-herald-msg91

最新pypi版本原始自述文件中提到的安装步骤

设置差异:

安装

pip安装mindbogglr django herald

设置

在django设置文件中,添加以下设置:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'

将"socket"替换为用户文本消息收件箱中所需的任何6个Alphebet系列。 将"228352HGNG5DF65B57672G"替换为来自msg91帐户的authkey

注意:它将显示为bt-socket或类似字符…前两个字母表示文本消息提供程序和区域,不可控制。

用法

fromheraldimportregistryfromherald.text_notificationimportMsg91TextNotification@registry.register_decorator()classInvitationMessage(Msg91TextNotification):template_name='invitation_message'# name of template, without extensiondef__init__(self,to_number,invited_by_user,user_full_name,invitation_url):# set context for the template renderingself.context={'invited_by_user':invited_by_user,'user_full_name':user_full_name,'invitation_url':invitation_url}self.to_number=to_number

原始自述文件

logopy年2.7 py第3.3页 py第3.4页 py年3.5 PY 3.6< /广告><正文>DJ 1.8<;0.2<;0.2<;0.2<;0.2--DJ 1.9-1.10<;0.2--<;0.2<;0.2--DJ 1.11>;=0.1.5-->;=0.1.5>;=0.1.5>;=0.1.5DJ 2.0---->;=0.1.5>;=0.1.5>;=0.1.5

安装

  1. pip安装django herald
  2. 添加heralddjango.contrib.sites到已安装的应用程序
  3. 添加先驱报的网址:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns

用法

  1. 在任何django应用程序中创建notifications.py文件。这就是您的通知类所在的位置。添加这样的类:
fromheraldimportregistryfromherald.baseimportEmailNotificationclassWelcomeEmail(EmailNotification):# extend from EmailNotification for emailstemplate_name='welcome_email'# name of template, without extensionsubject='Welcome'# subject of emaildef__init__(self,user):# optionally customize the initializationself.context={'user':user}# set context for the template renderingself.to_emails=[user.email]# set list of emails to send to@staticmethoddefget_demo_args():# define a static method to return list of args needed to initialize class for testingfromusers.modelsimportUserreturn[User.objects.order_by('?')[0]]registry.register(WelcomeEmail)# finally, register your notification class# Alternatively, a class decorator can be used to register the notification:@registry.register_decorator()classWelcomeEmail(EmailNotification):...
  1. 使用此文件结构创建用于呈现电子邮件的模板:

     templates/
         herald/
             text/
                 welcome_email.txt
             html/
                 welcome_email.html
    
  2. 导航到/herald/,测试电子邮件的外观

  3. 在代码中的任何需要的地方发送电子邮件:

     WelcomeEmail(user).send()
    
  4. 在django admin中查看已发送的电子邮件,甚至可以重新发送。

删除旧通知

delnotifs命令可用于清除通知历史记录。

默认用法将删除今天发送的所有内容:

python manage.py delnotifs

但是,您也可以为startend日期传递参数。结束是截止日期,但不包括该日期。

python manage.py delnotifs --start='2016-01-01' --end='2016-01-10'

异步电子邮件发送

如果你给很多人发送的电子邮件略有不同,可能需要相当长的一段时间才能处理。默认情况下,django将同步处理这一切。对于异步支持,我们推荐django芹菜电子邮件。它非常易于设置和集成:https://github.com/pmclanahan/django-celerie email

herald.contrib.auth

django内置了发送密码重置电子邮件的支持。如果要使用herald发送这些电子邮件,可以使用herald.contrib.auth中的通知类。

首先,将herald.contrib.auth添加到已安装的应用程序中(除了herald)。

其次,使用heraldpasswordresetform代替django内置的passwordresetform。这一步完全取决于您的项目结构,但它实际上只涉及以某种方式更改密码重置视图上的窗体类:

# you may simply just need to override the password reset url like so:url(r'^password_reset/$',password_reset,name='password_reset',{'password_reset_form':HeraldPasswordResetForm}),# of if you are using something like django-authtools:url(r'^password_reset/$',PasswordResetView.as_view(form_class=HeraldPasswordResetForm),name='password_reset'),# or you may have a customized version of the password reset view:classMyPasswordResetView(FormView):form_class=HeraldPasswordResetForm# change the form class here# or, you may have a custom password reset form already. In that case, you will want to extend from the HeraldPasswordResetForm:classMyPasswordResetForm(HeraldPasswordResetForm):...# alternatively, you could even just send the notification wherever you wish, seperate from the form:PasswordResetEmail(some_user).send()

第三,您可能需要自定义电子邮件的模板。默认情况下,herald将使用django为电子邮件的html和文本版本提供的registration/password_reset_email.html。但您可以简单地覆盖herald/html/password_reset.html和/或herald/text/password_reset.txt以满足您的需要。

用户禁用通知

如果要禁用每个用户的某些通知,请将记录添加到usernotification表中,然后 将通知添加到禁用的通知多对多表。

例如:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
0

默认情况下,可以禁用通知。您可以在通知类中设置can disable=false,系统将 使用此默认值填充数据库。通知类还可以通过在 继承的通知类。像这样:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
1

电子邮件附件

要发送附件,请将附件列表分配给emailnotification实例的attachments属性,或重写get_attachments()方法。

列表中的每个附件都可以是以下附件之一:

  1. 由文件名、原始附件数据和mimetype组成的元组。附件数据由你来决定。像这样:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
2
  1. 一个mimebase对象。请参阅Django文档中电子邮件对象/附件下的附件文档。

  2. 一个django文件对象。

内联附件

有时你想直接在电子邮件内容中嵌入一个图像。使用mimeimage将内容id头分配给mimeimage,如下所示:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
3

您可以使用如下内容ID(CID)在HTML电子邮件模板中引用这些图像:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
4

您当然需要将"图像文件"添加到模板cont上面例子中的ext。您还可以使用文件操作来完成此操作。在本例中,我们重写了电子邮件通知的get_attachments方法。

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
5

在模板中,您可以这样引用它,而不需要向上下文中添加任何内容:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
6

其他mime附件

您也可以将任何mimebase对象作为常规附件附加,但必须添加内容处置头,否则将无法访问它们:

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
7

附件会导致数据库变得很大,因此您应该确保运行管理命令以清除数据库中的旧邮件。

运行测试

MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
8

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

推荐PyPI第三方库


热门话题
java什么会导致程序在它似乎拥有的监视器上被阻止?   java Android studio设置视图的背景色   java我可以保存一个文本文件而不给用户修改它的能力吗?   pdfbox PDFBOX2。0:java堆堆栈错误   java是维护和操作AllowList的有效方法   JAVAsql。SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/asd性爱   如何使用java。lang.NullPointerException:void 安卓。支持v7。应用程序。ActionBar。setElevation(float)“”在空对象引用上'   java调试空指针异常   java正则表达式,以按令牌的特定匹配项拆分,同时忽略其他匹配项   java为JPanel设置边框上的笔划   并发@Schedule方法的java行为   如何在Java中使用泛型与语言运算符和泛型类扩展数   java Rhino Javascript如何为异常堆栈跟踪标记字符串源   运行可执行jar时发生java错误,无法找到或加载主类