Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ in ``settings.py``. Here's the default settings:
'DEFAULT': {
'persistent': True, # stays until dismissed
'send_as_email': False, # send as email
'headline_template_name': 'headline.txt', # Django template file for headline
'body_template_name': 'body.txt', # Django template file for body
'headline_template': '{{headline}}', # Django template for headline
'body_template': '{{body}}', # Django template for body
'email_field': 'email' # Assume field named 'email' is user's email
Expand All @@ -56,8 +58,10 @@ notification is another key in the ``NOTIFICATIONS`` dictionary.

- ``persistent``: Whether or not notifications are marked as inactive once emailed to a user.
- ``send_as_email``: Whether or not to send this kind of notification as an email.
- ``headline_template``: A Django template string that'll be rendered with a context dictionary for the headline.
- ``body_template``: A Django template string that'll be rendered with a context dictionary for the body.
- ``headline_template_name``: A Django template file that'll be rendered with a context dictionary for the headline.
- ``body_template_name``: A Django template file that'll be rendered with a context dictionary for the body.
- ``headline_template``: A Django template string that'll be rendered with a context dictionary for the headline. Only used if ``headline_template_name`` is not provided.
- ``body_template``: A Django template string that'll be rendered with a context dictionary for the body. Only used if ``body_template_name`` is not provided.
- ``email_field``: The field on your user model that holds the user's email address.

.. _methods:
Expand Down
31 changes: 23 additions & 8 deletions heythere/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db import models
from django.utils.functional import lazy
from django.utils.timezone import now
from django.template import loader

from .settings import get_notifications
from .utils import render
Expand Down Expand Up @@ -120,14 +121,28 @@ def save(self, *args, **kwargs):
headline_dict = self.headline_dict
body_dict = self.body_dict

self.headline = render(
self.notification_dict['headline_template'],
headline_dict
)
self.body = render(
self.notification_dict['body_template'],
body_dict
)
if 'headline_template_name' in self.notification_dict:
self.headline = loader.render_to_string(
self.notification_dict['headline_template_name'],
headline_dict
).strip()
else:
self.headline = render(
self.notification_dict['headline_template'],
headline_dict
)

if 'body_template_name' in self.notification_dict:
self.body = loader.render_to_string(
self.notification_dict['body_template_name'],
body_dict
).strip()
else:
self.body = render(
self.notification_dict['body_template'],
body_dict
)

if not self.persistent and self.sent_at:
self.active = False
super(Notification, self).save(*args, **kwargs)
Expand Down
7 changes: 7 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,12 @@
'send_as_email': True,
'headline_template': 'My headline: {{headline}}',
'body_template': 'My body: {{body}}',
},
'TEMPLATE_FILES': {
'persistent': True,
'send_onsite': False,
'send_as_email': True,
'headline_template_name': 'headline.txt',
'body_template_name': 'body.txt',
}
}
1 change: 1 addition & 0 deletions tests/templates/body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My body from file: {{body}}
1 change: 1 addition & 0 deletions tests/templates/headline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My headline from file: {{headline}}
5 changes: 5 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ def test_change_email_field(self):
'notification_type')[0].get_choices())
notification = self._create_notification('CUSTOM_USER')
self.assertIn('My body:', notification.body)

def test_using_template_files(self):
notification = self._create_notification('TEMPLATE_FILES')
self.assertIn('My headline from file:', notification.headline)
self.assertIn('My body from file:', notification.body)