Permalink
Browse files

Added Django 1.10 middleware support

This commit is from previous work.
  • Loading branch information...
brosner committed Oct 18, 2016
1 parent 7012eb0 commit 74434fc5453c19efa165edc078c0a25410fd4999
Showing with 34 additions and 15 deletions.
  1. +11 −3 account/middleware.py
  2. +16 −6 account/tests/test_password.py
  3. +7 −6 runtests.py
View
@@ -5,6 +5,8 @@
except ImportError: # python 2
from urlparse import urlparse, urlunparse
+import django
+
from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.urlresolvers import resolve, reverse
@@ -19,7 +21,13 @@
from account.utils import check_password_expired
-class LocaleMiddleware(object):
+if django.VERSION >= (1, 10):
+ from django.utils.deprecation import MiddlewareMixin as BaseMiddleware
+else:
+ BaseMiddleware = object
+
+
+class LocaleMiddleware(BaseMiddleware):
"""
This is a very simple middleware that parses a request
and decides what translation object to install in the current
@@ -48,7 +56,7 @@ def process_response(self, request, response):
return response
-class TimezoneMiddleware(object):
+class TimezoneMiddleware(BaseMiddleware):
"""
This middleware sets the timezone used to display dates in
templates to the user's timezone.
@@ -65,7 +73,7 @@ def process_request(self, request):
timezone.activate(tz)
-class ExpiredPasswordMiddleware(object):
+class ExpiredPasswordMiddleware(BaseMiddleware):
def process_request(self, request):
if request.user.is_authenticated() and not request.user.is_staff:
@@ -1,6 +1,8 @@
import datetime
import pytz
+import django
+
from django.contrib.auth.hashers import (
check_password,
make_password,
@@ -20,13 +22,21 @@
from ..utils import check_password_expired
+def middleware_kwarg(value):
+ if django.VERSION >= (1, 10):
+ kwarg = "MIDDLEWARE"
+ else:
+ kwarg = "MIDDLEWARE_CLASSES"
+ return {kwarg: value}
+
+
@override_settings(
ACCOUNT_PASSWORD_USE_HISTORY=True
)
@modify_settings(
- MIDDLEWARE_CLASSES={
- 'append': 'account.middleware.ExpiredPasswordMiddleware'
- }
+ **middleware_kwarg({
+ "append": "account.middleware.ExpiredPasswordMiddleware"
+ })
)
class PasswordExpirationTestCase(TestCase):
@@ -133,9 +143,9 @@ def test_password_expiration_reset(self):
@modify_settings(
- MIDDLEWARE_CLASSES={
- 'append': 'account.middleware.ExpiredPasswordMiddleware'
- }
+ **middleware_kwarg({
+ "append": "account.middleware.ExpiredPasswordMiddleware"
+ })
)
class ExistingUserNoHistoryTestCase(TestCase):
"""
View
@@ -19,12 +19,6 @@
"account",
"account.tests",
],
- MIDDLEWARE_CLASSES=[
- "django.contrib.sessions.middleware.SessionMiddleware",
- "django.contrib.auth.middleware.AuthenticationMiddleware",
- "django.contrib.auth.middleware.SessionAuthenticationMiddleware",
- "django.contrib.messages.middleware.MessageMiddleware",
- ],
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
@@ -58,6 +52,13 @@
]
)
+DEFAULT_SETTINGS["MIDDLEWARE" if django.VERSION >= (1, 10) else "MIDDLEWARE_CLASSES"] = [
+ "django.contrib.sessions.middleware.SessionMiddleware",
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
+ "django.contrib.auth.middleware.SessionAuthenticationMiddleware",
+ "django.contrib.messages.middleware.MessageMiddleware",
+]
+
def runtests(*test_args):
if not settings.configured:

0 comments on commit 74434fc

Please sign in to comment.