Django library for separating the message content from transmission method
Project description
# django-herald
[](https://pypi.python.org/pypi/django-herald)
[](https://travis-ci.org/worthwhile/django-herald)
[](https://coveralls.io/github/worthwhile/django-herald?branch=master)
[](https://github.com/worthwhile/django-herald)
Django library for separating the message content from transmission method
# Installation
1. `pip install django-herald`
2. Add `herald` and `django.contrib.sites` to `INSTALLED_APPS`.
3. Add herald's URLS:
if settings.DEBUG:
urlpatterns = [
url(r'^herald/', include('herald.urls')),
] + urlpatterns
# Usage
1. Create a `notifications.py` file in any django app. This is where your notification classes will live. Add a class like this:
from herald import registry
from herald.base import EmailNotification
class WelcomeEmail(EmailNotification): # extend from EmailNotification for emails
template_name = 'welcome_email' # name of template, without extension
subject = 'Welcome' # subject of email
def __init__(self, user): # optionally customize the initialization
self.context = {'user': user} # set context for the template rendering
self.to_emails = [user.email] # set list of emails to send to
@staticmethod
def get_demo_args(): # define a static method to return list of args needed to initialize class for testing
from users.models import User
return [User.objects.order_by('?')[0]]
registry.register(WelcomeEmail) # finally, register your notification class
2. Create templates for rendering the email using this file structure:
templates/
herald/
text/
welcome_email.txt
html/
welcome_email.html
3. Test how your email looks by navigating to `/herald/`.
4. Send your email wherever you need in your code:
WelcomeEmail(user).send()
5. View the sent emails in django admin and even be able to resend it.
## Deleting Old Notifications
The `delnotifs` command is useful for purging the notification history.
The default usage will delete everything from sent during today:
python manage.py delnotifs
However, you can also pass arguments for `start` or `end` dates. `end` is up to, but not including that date.
python manage.py delnotifs --start='2016-01-01' --end='2016-01-10'
## Asynchronous Email Sending
If you are sending slightly different emails to a large number of people, it might take quite a while to process. By default, Django will process this all synchronously. For asynchronous support, we recommend django-celery-email. It is very straightfoward to setup and integrate: https://github.com/pmclanahan/django-celery-email
## herald.contrib.auth
Django has built-in support for sending password reset emails. If you would like to send those emails using herald, you can use the notification class in herald.contrib.auth.
First, add `herald.contrib.auth` to `INSTALLED_APPS` (in addition to `herald`).
Second, use the `HeraldPasswordResetForm` in place of django's built in `PasswordResetForm`. This step is entirely dependant on your project structure, but it essentially just involves changing the form class on the password reset view in some way:
# 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:
class MyPasswordResetView(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:
class MyPasswordResetForm(HeraldPasswordResetForm):
...
# alternatively, you could even just send the notification wherever you wish, seperate from the form:
PasswordResetEmail(some_user).send()
Third, you may want to customize the templates for the email. By default, herald will use the `registration/password_reset_email.html` that is provided by django for both the html and text versions of the email. But you can simply override `herald/html/password_reset.html` and/or `herald/text/password_reset.txt` to suit your needs.
# Running Tests
python runtests.py
[](https://pypi.python.org/pypi/django-herald)
[](https://travis-ci.org/worthwhile/django-herald)
[](https://coveralls.io/github/worthwhile/django-herald?branch=master)
[](https://github.com/worthwhile/django-herald)
Django library for separating the message content from transmission method
# Installation
1. `pip install django-herald`
2. Add `herald` and `django.contrib.sites` to `INSTALLED_APPS`.
3. Add herald's URLS:
if settings.DEBUG:
urlpatterns = [
url(r'^herald/', include('herald.urls')),
] + urlpatterns
# Usage
1. Create a `notifications.py` file in any django app. This is where your notification classes will live. Add a class like this:
from herald import registry
from herald.base import EmailNotification
class WelcomeEmail(EmailNotification): # extend from EmailNotification for emails
template_name = 'welcome_email' # name of template, without extension
subject = 'Welcome' # subject of email
def __init__(self, user): # optionally customize the initialization
self.context = {'user': user} # set context for the template rendering
self.to_emails = [user.email] # set list of emails to send to
@staticmethod
def get_demo_args(): # define a static method to return list of args needed to initialize class for testing
from users.models import User
return [User.objects.order_by('?')[0]]
registry.register(WelcomeEmail) # finally, register your notification class
2. Create templates for rendering the email using this file structure:
templates/
herald/
text/
welcome_email.txt
html/
welcome_email.html
3. Test how your email looks by navigating to `/herald/`.
4. Send your email wherever you need in your code:
WelcomeEmail(user).send()
5. View the sent emails in django admin and even be able to resend it.
## Deleting Old Notifications
The `delnotifs` command is useful for purging the notification history.
The default usage will delete everything from sent during today:
python manage.py delnotifs
However, you can also pass arguments for `start` or `end` dates. `end` is up to, but not including that date.
python manage.py delnotifs --start='2016-01-01' --end='2016-01-10'
## Asynchronous Email Sending
If you are sending slightly different emails to a large number of people, it might take quite a while to process. By default, Django will process this all synchronously. For asynchronous support, we recommend django-celery-email. It is very straightfoward to setup and integrate: https://github.com/pmclanahan/django-celery-email
## herald.contrib.auth
Django has built-in support for sending password reset emails. If you would like to send those emails using herald, you can use the notification class in herald.contrib.auth.
First, add `herald.contrib.auth` to `INSTALLED_APPS` (in addition to `herald`).
Second, use the `HeraldPasswordResetForm` in place of django's built in `PasswordResetForm`. This step is entirely dependant on your project structure, but it essentially just involves changing the form class on the password reset view in some way:
# 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:
class MyPasswordResetView(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:
class MyPasswordResetForm(HeraldPasswordResetForm):
...
# alternatively, you could even just send the notification wherever you wish, seperate from the form:
PasswordResetEmail(some_user).send()
Third, you may want to customize the templates for the email. By default, herald will use the `registration/password_reset_email.html` that is provided by django for both the html and text versions of the email. But you can simply override `herald/html/password_reset.html` and/or `herald/text/password_reset.txt` to suit your needs.
# Running Tests
python runtests.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-herald-0.1.3.tar.gz
(16.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-herald-0.1.3.tar.gz.
File metadata
- Download URL: django-herald-0.1.3.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd5990c807cc261cd107094b15a26e8271610d6b949825937ca08bb1550761b8
|
|
| MD5 |
cc8520830fc8a56b01e544cdeb598544
|
|
| BLAKE2b-256 |
85a588a5f7f0b0f96d253c56f9f25b4352cf73abd12a46acdf6b8ac4f6224fab
|
File details
Details for the file django_herald-0.1.3-py2.py3-none-any.whl.
File metadata
- Download URL: django_herald-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 24.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab0b2666972d212aa97bed728267c5edb46043ae0de5e094e803ce4f79522b01
|
|
| MD5 |
1bdedfe5af8bf673883bcb92a57d0e14
|
|
| BLAKE2b-256 |
8da56abc3e037a7f84e5cef60df57b915763cc45f831740804353afd49a0a077
|