Skip to main content

django app to send notifications using signals

Project description

Django Signal Notification

a django app to send any notifications(sms, email, rocketchat and any media) after triggered a signal. you can add new "media" and new "handler". also you can customize your template messages directly in python handler class or in a django template file.

Setup

Requirements

  • Python >= 3.4
  • Django >= 2.0

Installation

  1. Install django-signal-notifier by pip:
    $ pip install django-signal-notification
    
  2. Add "signal_notification" at the end of INSTALLED_APPS setting like this
    INSTALLED_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        ...
        'signal_notification',
    ]
    
  3. python manage.py migrate
  4. goto admin panel to add new NotificationSetting records.

Settings

# Disable signal notification completely if set as True
SIGNAL_NOTIFICATION_DISABLED = False 

# Add your custom media classes path here.
SIGNAL_NOTIFICATION_MEDIA_CLASSES = [
    'signal_notification.notify_media.EmailMedia',
    'signal_notification.notify_media.SMSMedia',
]

# set your custom NotifyManager class path here
SIGNAL_NOTIFICATION_MANAGER_CLASS = 'signal_notification.notify_manager.NotifyManager'

# Add your handlers class path here.
SIGNAL_NOTIFICATION_HANDLER_CLASSES = [
    'signal_notification.notify_handlers.UserLoggedInHandler',
    'signal_notification.notify_handlers.UserLoginFailedHandler',
    'signal_notification.notify_handlers.NewUserHandler',
]

Add new Media class

  • You should add a new class inherited from "signal_notification.notify_media.NotifyMedia".
  • set unique "name" field of that class
  • override and implement the "send" method
  • set PARAMS_SCHEMA_VALIDATOR field for that class
from signal_notification.notify_media import NotifyMedia
class NewMedia(NotifyMedia):
    name = 'new_media'
    PARAMS_SCHEMA_VALIDATOR = {
        'to': {'type': 'string', 'nullable': False, 'required': True, 'empty': False}
    }
    def send(self, message, subject=None):
        to = self.kwargs['to']
        # ...
  • append path of this class to "SIGNAL_NOTIFICATION_MEDIA_CLASSES" setting
SIGNAL_NOTIFICATION_MEDIA_CLASSES = [
    ...
    'foo.bar.NewMedia'
]

Add new Handler class

  • You should add a new class inherited from "signal_notification.notify_handlers.NotifyHandler".
  • set unique "name" field of that class
  • set subject and message templates(directly in class or as a django templates file)
from django.contrib.auth import user_logged_out
from signal_notification.notify_handlers import NotifyHandler
class StaffUserLoggedOut(NotifyHandler):
    '''Send a notification when an Staff user logged out'''
    name = 'staff_user_logged_out'
    signal = user_logged_out
    subject_template = 'User Exited'
    message_template = 'Staff User "{{user}}" Logged out.'

    def is_triggered(self, notification_args):
        '''Using this method to ignore notification by checking some conditions'''
        user = notification_args.get('user')
        if not user.is_staff:
            return False
        return super().is_triggered(notification_args)
  • append path of this class to "SIGNAL_NOTIFICATION_HANDLER_CLASSES" setting
SIGNAL_NOTIFICATION_HANDLER_CLASSES = [
    ...
    'foo.bar.StaffUserLoggedOut'
]

How to customize the message template of handler?

You have 2 options:

  1. define subject and message directly in Handler class by setting this fields of class:
    • subject_template
    • message_template
  2. define subject and message in templates path as a django template file.
    • add a subject file in this templates path:
      • signal_notification/<handler_name>/subject-<media_name>.html # this is for specific media
      • signal_notification/<handler_name>/subject.html # this is for all media
    • add a message file in this templates path:
      • signal_notification/<handler_name>/message-<media_name>.html # this is for specific media
      • signal_notification/<handler_name>/message.html # this is for all media

Notice: the priorities for templates are:

  1. media template: signal_notification/<handler_name>/message-<media_name>.html
  2. general template: signal_notification/<handler_name>/message.html
  3. class template fields: message_template

Signals

  • You can use predefined django signals(like, post_save, pre_save, ..)
  • You can add your signals and use that in Handler
# in foo/bar/signals.py
from django.dispatch import Signal
test_signal = Signal()

add Custom NotifyManager

some times you want to have your custom manger. for example you want notifications be handled in a background task using celery, apscheduler, huey.

  1. You need to add a new class inherited from signal_notification.notify_manager.NotifyManager
  2. override the "handle_notification" class method.(use "_handle_notification" in your method as a final endpoint of handler method)
import traceback
from signal_notification.notify_manager import NotifyManager

class APSchedulerNotifyManager(NotifyManager):

    @classmethod
    def handle_notification(cls, handler_cls, notification_args):
        apscheduler_client = '<apscheduler_client object>'  
        try:
            apscheduler_client.root.add_job(
                'foo.bar:APSchedulerNotifyManager._handle_notification', 'date',
                args=(handler_cls, notification_args)
            )
        except Exception:
            traceback.print_exc()
  1. set path of this class for SIGNAL_NOTIFICATION_MANAGER_CLASS setting
SIGNAL_NOTIFICATION_MANAGER_CLASS = 'foo.bar.APSchedulerNotifyManager'

Demo

  1. cd django_signal_notification/demo
  2. python manage.py migrate
  3. python manage.py createsuperuser
  4. python manage.py runserver
  5. goto http://127.0.0.1:8000/admin
  6. navigate to /admin/signal_notification/notificationsetting/ and add your NotificationSettings records

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

django-signal-notification-0.0.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

django_signal_notification-0.0.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file django-signal-notification-0.0.1.tar.gz.

File metadata

  • Download URL: django-signal-notification-0.0.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.5

File hashes

Hashes for django-signal-notification-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ffaf9824b6d0f85860367e2baf0ea88243e5ec91bd39ed7fc0bae547292e385b
MD5 99369795736a431d187b05387c79591e
BLAKE2b-256 41a4d92f21d3bc787328263f572c2683f1c1544fa6c90aee7ca64b0a2e23237a

See more details on using hashes here.

File details

Details for the file django_signal_notification-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_signal_notification-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.5

File hashes

Hashes for django_signal_notification-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20c10afef40abcba477a8f237e1ce28b3f8f7e303ed39572c7dfbbcc0a2d1567
MD5 e55be91627d8370a8ceae84180c01dc5
BLAKE2b-256 e990e49b5051fec18f1afea0e92ec2be99aa1e8982b4af1e60050898a7ad08de

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page