Skip to content

Commit

Permalink
Adds Django 1.10 compatibility while maintaining Django 1.7 compatibi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
mkoistinen committed Jan 7, 2017
1 parent 4816664 commit 4c9cae3
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 52 deletions.
20 changes: 18 additions & 2 deletions password_policies/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
Resolver404
from django.http import HttpResponseRedirect
from django.utils import timezone
try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object

from password_policies.conf import settings
from password_policies.models import PasswordChangeRequired, PasswordHistory
from password_policies.utils import PasswordCheck


class PasswordChangeMiddleware(object):
class PasswordChangeMiddleware(MiddlewareMixin):
"""
A middleware to force a password change.
Expand All @@ -33,7 +37,19 @@ class PasswordChangeMiddleware(object):
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'password_policies.middleware.PasswordChangeMiddleware',
# ... other middlewares ...
# ... other middleware ...
)
or ``MIDDLEWARE`` if using Django 1.10 or higher:
MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'password_policies.middleware.PasswordChangeMiddleware',
# ... other middleware ...
)
.. note::
Expand Down
75 changes: 56 additions & 19 deletions password_policies/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os
from distutils.version import LooseVersion
from django import get_version

django_version = get_version()

DEBUG = False

Expand All @@ -21,7 +25,7 @@
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
"password_policies",
'password_policies',
]

DATABASES = {
Expand All @@ -39,25 +43,58 @@

SITE_ID = 1

TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
# This is to maintain compatibility with Django 1.7
if LooseVersion(django_version) < LooseVersion('1.8.0'):
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'password_policies.context_processors.password_status',
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'password_policies.middleware.PasswordChangeMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
else:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), 'templates'), ],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'password_policies.context_processors.password_status',
],
},
},
]

print(os.path.join(os.path.dirname(__file__), 'templates'))

# This is to maintain compatibility with Django < 1.10
if LooseVersion(django_version) < LooseVersion('1.10.0'):
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'password_policies.middleware.PasswordChangeMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
else:
MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'password_policies.middleware.PasswordChangeMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'password_policies.context_processors.password_status',
)

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
3 changes: 1 addition & 2 deletions password_policies/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def test_password_change_success(self):
response = self.client.post(reverse('password_change'), data=data)
self.assertEqual(PasswordHistory.objects.count(), 1)
obj = PasswordHistory.objects.get()
self.assertRedirects(response,
'http://testserver%s' % reverse('password_change_done'))
self.assertTrue(response.url.endswith(reverse('password_change_done')))
obj.delete()
self.client.logout()
19 changes: 14 additions & 5 deletions password_policies/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from django.conf.urls import include, patterns, url


from django.conf.urls import include, url
try:
from django.conf.urls import patterns
except ImportError:
patterns = False

from password_policies.tests.views import TestHomeView


urlpatterns = patterns('',
url(r'^password/', include('password_policies.urls')),
url(r'^$', TestHomeView.as_view(), name='home'),
)
urlpatterns = [
url(r'^password/', include('password_policies.urls')),
url(r'^$', TestHomeView.as_view(), name='home'),
]

if patterns:
urlpatterns = patterns('', *urlpatterns) # noqa
53 changes: 32 additions & 21 deletions password_policies/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from django.conf.urls import patterns, url
from django.conf.urls import url

try:
# patterns was deprecated in Django 1.8
from django.conf.urls import patterns
except ImportError:
# patterns is unavailable in Django 1.10+
patterns = False

from password_policies.views import PasswordChangeFormView
from password_policies.views import PasswordChangeDoneView
Expand All @@ -8,23 +15,27 @@
from password_policies.views import PasswordResetDoneView


urlpatterns = patterns('',
url(r'^change/done/$',
PasswordChangeDoneView.as_view(),
name="password_change_done"),
url(r'^change/$',
PasswordChangeFormView.as_view(),
name="password_change"),
url(r'^reset/$',
PasswordResetFormView.as_view(),
name="password_reset"),
url(r'^reset/complete/$',
PasswordResetCompleteView.as_view(),
name="password_reset_complete"),
url(r'^reset/confirm/([0-9A-Za-z_\-]+)/([0-9A-Za-z]{1,13})/([0-9A-Za-z-=_]{1,32})/$',
PasswordResetConfirmView.as_view(),
name="password_reset_confirm"),
url(r'^reset/done/$',
PasswordResetDoneView.as_view(),
name="password_reset_done"),
)
urlpatterns = [
url(r'^change/done/$',
PasswordChangeDoneView.as_view(),
name="password_change_done"),
url(r'^change/$',
PasswordChangeFormView.as_view(),
name="password_change"),
url(r'^reset/$',
PasswordResetFormView.as_view(),
name="password_reset"),
url(r'^reset/complete/$',
PasswordResetCompleteView.as_view(),
name="password_reset_complete"),
url(r'^reset/confirm/([0-9A-Za-z_\-]+)/([0-9A-Za-z]{1,13})/([0-9A-Za-z-=_]{1,32})/$',
PasswordResetConfirmView.as_view(),
name="password_reset_confirm"),
url(r'^reset/done/$',
PasswordResetDoneView.as_view(),
name="password_reset_done"),
]

if patterns:
# Django 1.7
urlpatterns = patterns('', *urlpatterns)
8 changes: 6 additions & 2 deletions password_policies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def form_valid(self, form):
form.save()
return super(PasswordChangeFormView, self).form_valid(form)

def get_form(self, form_class):
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
return form_class(self.request.user, **self.get_form_kwargs())

def get_success_url(self):
Expand Down Expand Up @@ -184,7 +186,9 @@ def get_context_data(self, **kwargs):
kwargs['validlink'] = self.validlink
return super(PasswordResetConfirmView, self).get_context_data(**kwargs)

def get_form(self, form_class):
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
return form_class(self.user, **self.get_form_kwargs())

def get_success_url(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
django>=1.7,<1.10
django>=1.7,<1.11
django-easysettings>=1.0
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ envlist =
py27-django17,
py27-django18,
py27-django19,
py27-django110,
py33-django17,
py33-django18,
py34-django17,
py34-django18,
py34-django19,
py35-django18,
py35-django19,
py35-django110,
[testenv]
deps =
django17: Django >= 1.7, < 1.8
django18: Django >= 1.8, < 1.9
django19: Django >= 1.9, < 1.10
django110: Django >= 1.10, < 1.11
commands = python setup.py test

0 comments on commit 4c9cae3

Please sign in to comment.