Package: django-signal-notification
Django Signal Notificationa 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.SetupRequirementsPython >= 3.4Django >= 2.0InstallationInstall django-signal-notifier by pip:$ pip install django-signal-notificationAdd "signal_notification" at the end of INSTALLED_APPS setting like thisINSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    ...
    'signal_notification',
]python manage.py migrategoto admin panel to add new NotificationSetting records.Settings# Disable signal notification completely if set as TrueSIGNAL_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 hereSIGNAL_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 classYou should add a new class inherited from "signal_notification.notify_media.NotifyMedia".set unique "name" field of that classoverride and implement the "send" methodset PARAMS_SCHEMA_VALIDATOR field for that classfromsignal_notification.notify_mediaimportNotifyMediaclassNewMedia(NotifyMedia):name='new_media'PARAMS_SCHEMA_VALIDATOR={'to':{'type':'string','nullable':False,'required':True,'empty':False}}defsend(self,message,subject=None):to=self.kwargs['to']# ...append path of this class to "SIGNAL_NOTIFICATION_MEDIA_CLASSES" settingSIGNAL_NOTIFICATION_MEDIA_CLASSES = [
    ...
    'foo.bar.NewMedia'
]Add new Handler classYou should add a new class inherited from "signal_notification.notify_handlers.NotifyHandler".set unique "name" field of that classset subject and message templates(directly in class or as a django templates file)fromdjango.contrib.authimportuser_logged_outfromsignal_notification.notify_handlersimportNotifyHandlerclassStaffUserLoggedOut(NotifyHandler):'''Send a notification when an Staff user logged out'''name='staff_user_logged_out'signal=user_logged_outsubject_template='User Exited'message_template='Staff User "{{user}}" Logged out.'defis_triggered(self,notification_args):'''Using this method to ignore notification by checking some conditions'''user=notification_args.get('user')ifnotuser.is_staff:returnFalsereturnsuper().is_triggered(notification_args)append path of this class to "SIGNAL_NOTIFICATION_HANDLER_CLASSES" settingSIGNAL_NOTIFICATION_HANDLER_CLASSES = [
    ...
    'foo.bar.StaffUserLoggedOut'
]How to customize the message template of handler?You have 2 options:define subject and message directly in Handler class by setting this fields of class:subject_templatemessage_templatedefine 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 mediasignal_notification/<handler_name>/subject.html  # this is for all mediaadd a message file in this templates path:signal_notification/<handler_name>/message-<media_name>.html  # this is for specific mediasignal_notification/<handler_name>/message.html  # this is for all mediaNotice: the priorities for templates are:media template: signal_notification/<handler_name>/message-<media_name>.htmlgeneral template: signal_notification/<handler_name>/message.htmlclass template fields: message_templateSignalsYou can use predefined django signals(like, post_save, pre_save, ..)You can add your signals and use that in Handler# in foo/bar/signals.pyfromdjango.dispatchimportSignaltest_signal=Signal()add Custom NotifyManagersome times you want to have your custom manger. for example you want notifications be handled in a background task using celery, apscheduler, huey.You need to add a new class inherited from signal_notification.notify_manager.NotifyManageroverride the "handle_notification" class method.(use "_handle_notification" in your method as a final endpoint of handler method)importtracebackfromsignal_notification.notify_managerimportNotifyManagerclassAPSchedulerNotifyManager(NotifyManager):@classmethoddefhandle_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))exceptException:traceback.print_exc()set path of this class for SIGNAL_NOTIFICATION_MANAGER_CLASS settingSIGNAL_NOTIFICATION_MANAGER_CLASS='foo.bar.APSchedulerNotifyManager'Democd django_signal_notification/demopython manage.py migratepython manage.py createsuperuserpython manage.py runservergotohttp://127.0.0.1:8000/adminnavigate to /admin/signal_notification/notificationsetting/ and add your NotificationSettings records