middleware.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django.conf import settings
  2. from django.contrib.auth import views as auth_views
  3. from axes.decorators import watch_login
  4. class FailedLoginMiddleware(object):
  5. def __init__(self, *args, **kwargs):
  6. super(FailedLoginMiddleware, self).__init__(*args, **kwargs)
  7. # watch the auth login
  8. auth_views.login = watch_login(auth_views.login)
  9. class ViewDecoratorMiddleware(object):
  10. """
  11. When the django_axes middleware is installed, by default it watches the
  12. django.auth.views.login.
  13. This middleware allows adding protection to other views without the need
  14. to change any urls or dectorate them manually.
  15. Add this middleware to your MIDDLEWARE settings after
  16. `axes.middleware.FailedLoginMiddleware` and before the django
  17. flatpages middleware.
  18. """
  19. watched_logins = getattr(
  20. settings, 'AXES_PROTECTED_LOGINS', (
  21. '/accounts/login/',
  22. )
  23. )
  24. def process_view(self, request, view_func, view_args, view_kwargs):
  25. if request.path in self.watched_logins:
  26. return watch_login(view_func)(request, *view_args, **view_kwargs)
  27. return None