Skip to content

valentinogagliardi/django-monitus

Repository files navigation

django-monitus

https://img.shields.io/twitter/follow/gagliardi_vale?style=social

Tiny error reporting middleware(s) for Django. Includes the following middlewares:

  • Error403EmailsMiddleware: sends an email to ADMINS on 403.
  • FailedLoginMiddleware: sends an email to ADMINS on failed logins.

Requirements

Tested on Python 3.8.

Tested on Django 3.0 and 3.1.

Setup

Install from pip:

python -m pip install django-monitus

Then add it to the list of installed apps:

INSTALLED_APPS = [
...
"monitus"
...
]

Enable the desired middleware:

MIDDLEWARE = [
...
"monitus.middleware.Error403EmailsMiddleware"
...
]

Setup ADMINS in your settings:

MANAGERS = [("Juliana C.", "[email protected]")]

Usage with asynchronous Django

As of now only Error403EmailsMiddleware can run asynchronously under ASGI. If you plan to use FailedLoginMiddleware and Error403EmailsMiddleware together make sure to place Error403EmailsMiddleware before (reading top to bottom) FailedLoginMiddleware:

MIDDLEWARE = [
"monitus.middleware.FailedLoginMiddleware",
"monitus.middleware.Error403EmailsMiddleware" # this should go before FailedLoginMiddleware
...
]

This way the async chain doesn't get broken when the response traverses the middleware chain to exit out to the user.

Development and testing

To test on your local machine with Postgres, make sure to have a role with enough privileges:

CREATE ROLE monitustestuser WITH LOGIN PASSWORD 'monitustestpassword' CREATEDB;

Then run

DATABASE_URL=postgres://monitustestuser:monitustestpassword@localhost/monitustestdb tox