Send transactional notifications to any service.
Project description
Django notifications
Install
pip install django-transactional-notifications
# settings.py
INSTALLED_APPS += ["txn_notifications"]
DJANGO_NOTIFICATIONS = {
"email": {
"enabled": True,
}
}
Features
Template based notifications. You can use markdown
- Edit messages from django admin
- Template based message
- Rich html message format using markdown
- Customizable and extensible handler
- Add your own handler for the services you use
- User settings (allow/disallow notification by handler or category)
- Check message status (
is_sent)- Update message status (using callbacks)
Wrappers
NotificationWrapper
Handlers
DjangoHandlerEmailHandler(generic)MailgunHandlerwithwebhooksSendgrid
TwilioHandlerwithcallbacksSMSWhatsapp
Models
Notification structure
CategoryTemplateNotification
User preferences
UserCategorySettingUserHandlerSetting
Settings
Optional. Override the models.
# settings.py
NOTIFICATIONS_CATEGORY_MODEL = "notifications.Category"
NOTIFICATIONS_TEMPLATE_MODEL = "notifications.Template"
NOTIFICATIONS_NOTIFICATION_MODEL = "notifications.Notification"
NOTIFICATIONS_USERHANDLERSETTING_MODEL = "notifications.UserHandlerSetting"
NOTIFICATIONS_USERCATEGORYSETTING_MODEL = "notifications.UserCategorySetting"
# your custom_module.py
import swapper
from django.db import models
from txn_notifications.base.models import AbstractNotification
class MyNotification(AbstractNotification):
# `get_model_name` is required if you want to override a ForeignKey
# related to any override notification.model.
template = models.ForeignKey(
swapper.get_model_name("txn_notifications", "Template"),
on_delete=models.CASCADE,
related_name="notifications",
)
class Meta(AbstractNotification.Meta):
swappable = swapper.swappable_setting("txn_notifications", "Notification")
abstract = False
# email
DEFAULT_FROM_EMAIL = "Test <test@test.com>"
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
DJANGO_NOTIFICATIONS = {
"email": {
"enabled": False,
# user model attribute
"recipient_attr": "email",
# templates
"html_template": "notifications/templates/email.html",
"txt_template": "notifications/templates/email.txt",
},
"twilio": {
"enabled": False,
"recipient_attr": None,
# account settings
"account_sid": None,
"auth_token": None,
"sms_sender": None,
},
}
settings
You can override it in the settings or in the data payload in the notification.
Using configurations:
DJANGO_NOTIFICATIONS = {
"email": {
"enabled": False,
"html_template": "notifications/templates/email.html",
"txt_template": "notifications/templates/email.txt",
"recipient_attr": "email",
},
}
Using notification data:
data = {
# tempalte data (created by the template)
"title": "",
"body": "",
"url": "", # button url
"url_msg": "", # button copy
# template data (optional)
"prehead": "", # optional
"image": "url to the image", # optional
"image_alt": "", # optional
"intro": "", # optional (before body)
"outro": "", # optional (after body)
"footer": "", # optional (outside the box)
# or you can chage the email template
"txt_template": "path/to/template.txt",
"html_template": "path/to/template.html",
# other options related to the email
"bcc": [],
"connection": None,
"attachments": [],
"headers": [],
"alternatives": [],
"cc": [],
"reply_to": []
}
Getting the recipient attr
You can chage the recipient_attr in the settings. This will try to get
the attribute from the User model.
If that attribute comes from another model, or it must be calculated by a
function, then you can override the get_reecipient function by extending
from the EmailHandler or any other handler inherit from it.
from txn_notifications.handlers.email import EmailHandler
class MyEmailHandler(EmailHandler):
def get_recipient(self) -> list:
return self.user.settings.any_other_attribute
Templates
If you want to use your own templates ...
- Change
html_templateandtxt_templatein the settings or in thedatapayload. - Set the django template tags
{{ title }},{{ body }}, etc. - Remember to validate inline your html/css.
Inliners: https://templates.mailchimp.com/resources/inline-css/
Check/update message status
Callbacks
notifications/callbacks/{callback_id}/
- (
mailgun) https://documentation.mailgun.com/en/latest/quickstart-events.html#events - (
twillio) https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-python
Testing
- Install pipenv
pip install pipenv
- Install dependencies
pipenv install --dev
- Run tests
pipenv run pytest --cov=txn_notifications .
Runing Django
- Run migrations
pipenv run python manage.py migrate
- Create Super User
pipenv run python manage.py createsuperuser
- Run server
pipenv run python manage.py runserver
- Open admin http://localhost:8000/admin/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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-transactional-notifications-0.3.4.tar.gz.
File metadata
- Download URL: django-transactional-notifications-0.3.4.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
783b8f5453b31d2d031a640d0c124377efeaaef4cc603bde19500f7f479e0554
|
|
| MD5 |
2c2851a328cb424166a596d571e78504
|
|
| BLAKE2b-256 |
2a919b38a135d7f6c9ce08f1981a488af415a723d2bc0f8afdea452d1bdbd8dc
|
File details
Details for the file django_transactional_notifications-0.3.4-py3-none-any.whl.
File metadata
- Download URL: django_transactional_notifications-0.3.4-py3-none-any.whl
- Upload date:
- Size: 29.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1aed460de9568284320c8a54a65ad01a266438000d9820f775c12fb657145767
|
|
| MD5 |
238c2ebef0ec2aadaa93c39eb3350868
|
|
| BLAKE2b-256 |
a81359607012a06924cd17b17216075f1ab97ae2cbe87ce884158b5c1029baf1
|