Skip to content

Commit

Permalink
Added Django 1.10 middleware support
Browse files Browse the repository at this point in the history
This commit is from previous work.
  • Loading branch information
brosner committed Oct 18, 2016
1 parent 7012eb0 commit 74434fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
14 changes: 11 additions & 3 deletions account/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
22 changes: 16 additions & 6 deletions account/tests/test_password.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import pytz

import django

from django.contrib.auth.hashers import (
check_password,
make_password,
Expand All @@ -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):

Expand Down Expand Up @@ -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):
"""
Expand Down
13 changes: 7 additions & 6 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 74434fc

Please sign in to comment.