Skip to content

Commit

Permalink
Django 2.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mpasternak committed Dec 9, 2017
1 parent 4c9cae3 commit 17fbe35
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 24 deletions.
8 changes: 7 additions & 1 deletion password_policies/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ def password_status(request):
)
"""
d = {}
if request.user.is_authenticated():
try:
# Did this ever worked? It gives error on Django 2.0
# and I haven't ran the test suite before that...
auth = request.user.is_authenticated()
except TypeError:
auth = request.user.is_authenticated
if auth is True:
if '_password_policies_change_required' not in request.session:
r = PasswordHistory.objects.change_required(request.user)
else:
Expand Down
19 changes: 16 additions & 3 deletions password_policies/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import re
from datetime import timedelta
from django.core.urlresolvers import resolve, reverse, NoReverseMatch, \
Resolver404
try:
from django.core.urlresolvers import resolve, reverse, NoReverseMatch, \
Resolver404
except ImportError:
from django.urls.base import reverse, resolve, NoReverseMatch, \
Resolver404

from django.http import HttpResponseRedirect
from django.utils import timezone
try:
Expand Down Expand Up @@ -160,8 +165,16 @@ def process_request(self, request):
return
self.now = timezone.now()
self.url = reverse('password_change')

try:
# Did this ever worked? It gives error on Django 2.0
# and I haven't ran the test suite before that...
auth = request.user.is_authenticated()
except TypeError:
auth = request.user.is_authenticated

if settings.PASSWORD_DURATION_SECONDS and \
request.user.is_authenticated() and \
(auth is True) and \
not self._is_excluded_path(request.path):
self.check = PasswordCheck(request.user)
self.expiry_datetime = self.check.get_expiry_datetime()
Expand Down
30 changes: 15 additions & 15 deletions password_policies/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Generated by Django 2.0 on 2017-12-08 19:54

from django.db import models, migrations
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
Expand All @@ -15,32 +17,30 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='PasswordChangeRequired',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(auto_now_add=True, help_text='The date the entry was created.', verbose_name='created', db_index=True)),
('user', models.OneToOneField(related_name='password_change_required', verbose_name='user', to=settings.AUTH_USER_MODEL, help_text='The user who needs to change his/her password.')),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True, db_index=True, help_text='The date the entry was created.', verbose_name='created')),
('user', models.OneToOneField(help_text='The user who needs to change his/her password.', on_delete=django.db.models.deletion.CASCADE, related_name='password_change_required', to=settings.AUTH_USER_MODEL, verbose_name='user')),
],
options={
'ordering': ['-created'],
'get_latest_by': 'created',
'verbose_name': 'enforced password change',
'verbose_name_plural': 'enforced password changes',
'ordering': ['-created'],
'get_latest_by': 'created',
},
bases=(models.Model,),
),
migrations.CreateModel(
name='PasswordHistory',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(auto_now_add=True, help_text='The date the entry was created.', verbose_name='created', db_index=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True, db_index=True, help_text='The date the entry was created.', verbose_name='created')),
('password', models.CharField(help_text='The encrypted password.', max_length=128, verbose_name='password')),
('user', models.ForeignKey(related_name='password_history_entries', verbose_name='user', to=settings.AUTH_USER_MODEL, help_text='The user this password history entry belongs to.')),
('user', models.ForeignKey(help_text='The user this password history entry belongs to.', on_delete=django.db.models.deletion.CASCADE, related_name='password_history_entries', to=settings.AUTH_USER_MODEL, verbose_name='user')),
],
options={
'ordering': ['-created'],
'get_latest_by': 'created',
'verbose_name': 'password history entry',
'verbose_name_plural': 'password history entries',
'ordering': ['-created'],
'get_latest_by': 'created',
},
bases=(models.Model,),
),
]
6 changes: 4 additions & 2 deletions password_policies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class PasswordChangeRequired(models.Model):
verbose_name=_('user'),
help_text=_('The user who needs to change '
'his/her password.'),
related_name='password_change_required')
related_name='password_change_required',
on_delete=models.CASCADE)

class Meta:
get_latest_by = 'created'
Expand All @@ -43,7 +44,8 @@ class PasswordHistory(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
help_text=_('The user this password history '
'entry belongs to.'),
related_name='password_history_entries')
related_name='password_history_entries',
on_delete=models.CASCADE)

objects = PasswordHistoryManager()

Expand Down
6 changes: 5 additions & 1 deletion password_policies/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
except ImportError:
from urlparse import urljoin

from django.core.urlresolvers import reverse
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls.base import reverse

from django.utils import timezone

from password_policies.conf import settings
Expand Down
5 changes: 4 additions & 1 deletion password_policies/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.core.urlresolvers import reverse
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls.base import reverse

from password_policies.forms import PasswordPoliciesChangeForm
from password_policies.models import PasswordHistory
Expand Down
5 changes: 4 additions & 1 deletion password_policies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user_model
from django.core import signing
from django.core.urlresolvers import reverse
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls.base import reverse
from django.shortcuts import resolve_url
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ envlist =
py35-django18,
py35-django19,
py35-django110,
py36-django20,
[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
django20: Django >= 2.0, < 2.1
commands = python setup.py test

0 comments on commit 17fbe35

Please sign in to comment.