Categories
django

Sending Notifications with Django and almost falling into a Trap

I almost fell into a trap…

I have been developing a new product that sends emails to certain manager users when certain events happen. I am manually handling these tasks with ye old send_email. I also wanted to  bring in a facebook/twitter style notifications so managers can see things that require their attention when logging in.

I couldn’t see the wood for the trees for a while there, I was looking at how to create bootstrap notifications on the nav bar. I had even written a test for it.

Avoiding the Trap

Then it occurred to me that an email notification and a notification bell on the nav bar is kind of the same thing and manually managing both events for the many events to be done on the site in the future is quite simply madness.

Go back to the fundamentals of python:

Don't reinvent the wheel and simple is better than complex

The Requirements

Let us just revisit what we want it to do, as simply as possible:

  1. Send emails to users when certain events happen
  2. Have notifications linked to specific users they can view when they are on the site
  3. Ability to mark some notifications as read

Notification Frameworks

So I scrapped the manually sending out notifications, although I had already created a few events that sent emails out. I am going to reuse a solution to a problem that surely has been solved already.

To start I searched google and github with terms like django notifications and I found a few potentials. One thing to note is that sometimes you don’t find your required simple solution easily and have to do a lot of testing first. You can see this from my django moderation article.

Here is a list of the frameworks I found:

  • django-notifications – GitHub notifications alike app for Django [595 stars]
  • pinax-notifications – user notification management for the Django web framework [573 stars]
  • django-nyt – Notification system for Django with batteries included: Email digests, user settings, JSON API [83 stars]
  • django-notify-x – Notification system for Django [122 stars]
  • django-webline-notifications – A python library, which allow you notify everything to user(s) simply

Django Notifications

After reading through the readme, Sending email to users has not been integrated into this library. So I will have to implement myself, other than that the way it works seems nice and simple.

There were a few issues when I installed it, like the docs were not in line with the pip installed version of the code. There are a few open issues and does not support python 3.6 and django 2.0.

It works okay but the templates are very basic and although the fundamentals behind it (including the models) are great: Actor -> Verb -> Action Object -> Target.

However you need to do quite a bit of heavily lifting by overriding templates and creating custom js callbacks to make it into a professional notification system. So I decided to try pinax-notifications instead

Pinax Notifications

Pinax is a fork of django-notifications there were quite a few issues on it at the time of writing however they do support python 3.6 and django 2

Their build was passing while django-notifications was failing.

Unfortunately after setting it up it lacks a few key features like the notification templates and stream of content that was available in django-notifications. It does send emails quite well but doesn’t have views for viewing all your notifications.

So it seems to be primarily focused on emails. The only view it provides is the notification/settings where you can set which notifications you want to receive.

It is not on the level and would require even more customisation that django-notifications-hq