diff --git a/password_policies/context_processors.py b/password_policies/context_processors.py index 03d146e..29bda3f 100644 --- a/password_policies/context_processors.py +++ b/password_policies/context_processors.py @@ -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: diff --git a/password_policies/middleware.py b/password_policies/middleware.py index 74a16a3..0644ab5 100644 --- a/password_policies/middleware.py +++ b/password_policies/middleware.py @@ -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: @@ -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() diff --git a/password_policies/migrations/0001_initial.py b/password_policies/migrations/0001_initial.py index eeb6e34..e9afa50 100644 --- a/password_policies/migrations/0001_initial.py +++ b/password_policies/migrations/0001_initial.py @@ -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), ] @@ -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,), ), ] diff --git a/password_policies/models.py b/password_policies/models.py index af3c1a3..6987f6b 100644 --- a/password_policies/models.py +++ b/password_policies/models.py @@ -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' @@ -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() diff --git a/password_policies/tests/test_middleware.py b/password_policies/tests/test_middleware.py index 6db2f9c..dc0fc77 100644 --- a/password_policies/tests/test_middleware.py +++ b/password_policies/tests/test_middleware.py @@ -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 diff --git a/password_policies/tests/test_views.py b/password_policies/tests/test_views.py index 0d4a6a9..cacb06d 100644 --- a/password_policies/tests/test_views.py +++ b/password_policies/tests/test_views.py @@ -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 diff --git a/password_policies/views.py b/password_policies/views.py index db43146..05cab13 100644 --- a/password_policies/views.py +++ b/password_policies/views.py @@ -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 diff --git a/tox.ini b/tox.ini index a561b1e..70538b4 100644 --- a/tox.ini +++ b/tox.ini @@ -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