From 8e896a54f9ad6c77bfbf85fd47a59928c22f2844 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 25 Nov 2018 00:59:39 -0400 Subject: [PATCH] Middleware: Modernize middleware classes Make the custom middleware provided by Mayan to use the MiddlewareMixin provide by Django. This make the middleware classes behave like classes or callables. This change ensures compatibility with Django 2.x. Signed-off-by: Roberto Rosario --- mayan/apps/common/middleware/ajax_redirect.py | 3 ++- mayan/apps/common/middleware/error_logging.py | 3 ++- mayan/apps/common/middleware/timezone.py | 3 ++- mayan/settings/base.py | 10 +++++----- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mayan/apps/common/middleware/ajax_redirect.py b/mayan/apps/common/middleware/ajax_redirect.py index 24166d685a..b8ac222564 100644 --- a/mayan/apps/common/middleware/ajax_redirect.py +++ b/mayan/apps/common/middleware/ajax_redirect.py @@ -2,9 +2,10 @@ from __future__ import unicode_literals from django.conf import settings from django.http import HttpResponseRedirect +from django.utils.deprecation import MiddlewareMixin -class AjaxRedirect(object): +class AjaxRedirect(MiddlewareMixin): def process_request(self, request): ajax_referer = request.META.get('HTTP_X_ALT_REFERER') diff --git a/mayan/apps/common/middleware/error_logging.py b/mayan/apps/common/middleware/error_logging.py index a90490cb5c..e7ffaa0b9d 100644 --- a/mayan/apps/common/middleware/error_logging.py +++ b/mayan/apps/common/middleware/error_logging.py @@ -4,11 +4,12 @@ import logging from django.core.exceptions import PermissionDenied from django.http import Http404 +from django.utils.deprecation import MiddlewareMixin logger = logging.getLogger(__name__) -class ErrorLoggingMiddleware(object): +class ErrorLoggingMiddleware(MiddlewareMixin): def process_exception(self, request, exception): if not isinstance(exception, (PermissionDenied, Http404)): # Don't log non critical exceptions diff --git a/mayan/apps/common/middleware/timezone.py b/mayan/apps/common/middleware/timezone.py index 491abed3e2..cfc4119390 100644 --- a/mayan/apps/common/middleware/timezone.py +++ b/mayan/apps/common/middleware/timezone.py @@ -4,9 +4,10 @@ import pytz from django.conf import settings from django.utils import timezone +from django.utils.deprecation import MiddlewareMixin -class TimezoneMiddleware(object): +class TimezoneMiddleware(MiddlewareMixin): def process_request(self, request): if hasattr(request, 'session'): tzname = request.session.get(settings.TIMEZONE_SESSION_KEY) diff --git a/mayan/settings/base.py b/mayan/settings/base.py index d865dd7e66..736d64a90a 100644 --- a/mayan/settings/base.py +++ b/mayan/settings/base.py @@ -125,7 +125,7 @@ INSTALLED_APPS = ( 'drf_yasg', ) -MIDDLEWARE_CLASSES = ( +MIDDLEWARE = ( 'common.middleware.error_logging.ErrorLoggingMiddleware', 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', @@ -144,17 +144,17 @@ MIDDLEWARE_CLASSES = ( ROOT_URLCONF = 'mayan.urls' -TEMPLATES = [ +TEMPLATES = ( { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'OPTIONS': { 'context_processors': [ + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.debug', 'django.template.context_processors.i18n', 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', ], 'loaders': [ 'django.template.loaders.filesystem.Loader', @@ -162,7 +162,7 @@ TEMPLATES = [ ] }, }, -] +) WSGI_APPLICATION = 'mayan.wsgi.application'