Skip to content

Commit

Permalink
Merge pull request #248 from pinax/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
paltman committed Apr 18, 2017
2 parents 676f428 + ec110fc commit bc63575
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 48 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Expand Up @@ -5,10 +5,12 @@ python:
- "3.3"
- "3.4"
- "3.5"
- "3.6"
env:
- DJANGO=1.8
- DJANGO=1.9
- DJANGO=1.10
- DJANGO=1.11
- DJANGO=master
matrix:
exclude:
Expand All @@ -17,7 +19,19 @@ matrix:
- python: "3.3"
env: DJANGO=1.10
- python: "3.3"
env: DJANGO=1.11
- python: "2.7"
env: DJANGO=master
- python: "3.3"
env: DJANGO=master
- python: "3.4"
env: DJANGO=master
- python: "3.6"
env: DJANGO=1.8
- python: "3.6"
env: DJANGO=1.9
- python: "3.6"
env: DJANGO=1.10
install:
- pip install tox coveralls
script:
Expand Down
13 changes: 13 additions & 0 deletions account/compat.py
@@ -0,0 +1,13 @@
import django

try:
from django.core.urlresolvers import resolve, reverse, NoReverseMatch
except ImportError:
from django.urls import resolve, reverse, NoReverseMatch # noqa


def is_authenticated(user):
if django.VERSION >= (1, 10):
return user.is_authenticated
else:
return user.is_authenticated()
3 changes: 2 additions & 1 deletion account/decorators.py
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.decorators import available_attrs

from account.compat import is_authenticated
from account.utils import handle_redirect_to_login


Expand All @@ -16,7 +17,7 @@ def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url
def decorator(view_func):
@functools.wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
if is_authenticated(request.user):
return view_func(request, *args, **kwargs)
return handle_redirect_to_login(
request,
Expand Down
6 changes: 3 additions & 3 deletions account/middleware.py
Expand Up @@ -9,13 +9,13 @@

from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.urlresolvers import resolve, reverse
from django.http import HttpResponseRedirect, QueryDict
from django.utils import translation, timezone
from django.utils.cache import patch_vary_headers
from django.utils.translation import ugettext_lazy as _

from account import signals
from account.compat import resolve, reverse, is_authenticated
from account.conf import settings
from account.models import Account
from account.utils import check_password_expired
Expand All @@ -37,7 +37,7 @@ class LocaleMiddleware(BaseMiddleware):
"""

def get_language_for_user(self, request):
if request.user.is_authenticated():
if is_authenticated(request.user):
try:
account = Account.objects.get(user=request.user)
return account.language
Expand Down Expand Up @@ -76,7 +76,7 @@ def process_request(self, request):
class ExpiredPasswordMiddleware(BaseMiddleware):

def process_request(self, request):
if request.user.is_authenticated() and not request.user.is_staff:
if is_authenticated(request.user) and not request.user.is_staff:
next_url = resolve(request.path).url_name
# Authenticated users must be allowed to access
# "change password" page and "log out" page.
Expand Down
12 changes: 6 additions & 6 deletions account/migrations/0001_initial.py

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions account/migrations/0004_auto_20170416_1821.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-16 18:21
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('account', '0003_passwordexpiry_passwordhistory'),
]

operations = [
migrations.AlterModelOptions(
name='passwordhistory',
options={'verbose_name': 'password history', 'verbose_name_plural': 'password histories'},
),
]
3 changes: 2 additions & 1 deletion account/mixins.py
Expand Up @@ -2,6 +2,7 @@

from django.contrib.auth import REDIRECT_FIELD_NAME

from account.compat import is_authenticated
from account.conf import settings
from account.utils import handle_redirect_to_login

Expand All @@ -15,7 +16,7 @@ def dispatch(self, request, *args, **kwargs):
self.request = request
self.args = args
self.kwargs = kwargs
if request.user.is_authenticated():
if is_authenticated(request.user):
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
return self.redirect_to_login()

Expand Down
20 changes: 10 additions & 10 deletions account/models.py
Expand Up @@ -8,7 +8,6 @@
except ImportError: # python 2
from urllib import urlencode

from django.core.urlresolvers import reverse
from django.db import models, transaction
from django.db.models import Q
from django.db.models.signals import post_save
Expand All @@ -23,6 +22,7 @@
import pytz

from account import signals
from account.compat import reverse, is_authenticated
from account.conf import settings
from account.fields import TimeZoneField
from account.hooks import hookset
Expand All @@ -33,7 +33,7 @@
@python_2_unicode_compatible
class Account(models.Model):

user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"))
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"), on_delete=models.CASCADE)
timezone = TimeZoneField(_("timezone"))
language = models.CharField(
_("language"),
Expand All @@ -45,7 +45,7 @@ class Account(models.Model):
@classmethod
def for_request(cls, request):
user = getattr(request, "user", None)
if user and user.is_authenticated():
if user and is_authenticated(user):
try:
return Account._default_manager.get(user=user)
except Account.DoesNotExist:
Expand Down Expand Up @@ -140,7 +140,7 @@ class InvalidCode(Exception):
code = models.CharField(_("code"), max_length=64, unique=True)
max_uses = models.PositiveIntegerField(_("max uses"), default=0)
expiry = models.DateTimeField(_("expiry"), null=True, blank=True)
inviter = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
inviter = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
email = models.EmailField(max_length=254, blank=True)
notes = models.TextField(_("notes"), blank=True)
sent = models.DateTimeField(_("sent"), null=True, blank=True)
Expand Down Expand Up @@ -242,8 +242,8 @@ def send(self, **kwargs):

class SignupCodeResult(models.Model):

signup_code = models.ForeignKey(SignupCode)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
signup_code = models.ForeignKey(SignupCode, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
timestamp = models.DateTimeField(default=timezone.now)

def save(self, **kwargs):
Expand All @@ -254,7 +254,7 @@ def save(self, **kwargs):
@python_2_unicode_compatible
class EmailAddress(models.Model):

user = models.ForeignKey(settings.AUTH_USER_MODEL)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
email = models.EmailField(max_length=254, unique=settings.ACCOUNT_EMAIL_UNIQUE)
verified = models.BooleanField(_("verified"), default=False)
primary = models.BooleanField(_("primary"), default=False)
Expand Down Expand Up @@ -305,7 +305,7 @@ def change(self, new_email, confirm=True):
@python_2_unicode_compatible
class EmailConfirmation(models.Model):

email_address = models.ForeignKey(EmailAddress)
email_address = models.ForeignKey(EmailAddress, on_delete=models.CASCADE)
created = models.DateTimeField(default=timezone.now)
sent = models.DateTimeField(null=True)
key = models.CharField(max_length=64, unique=True)
Expand Down Expand Up @@ -400,7 +400,7 @@ class Meta:
verbose_name = _("password history")
verbose_name_plural = _("password histories")

user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="password_history")
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="password_history", on_delete=models.CASCADE)
password = models.CharField(max_length=255) # encrypted password
timestamp = models.DateTimeField(default=timezone.now) # password creation time

Expand All @@ -409,5 +409,5 @@ class PasswordExpiry(models.Model):
"""
Holds the password expiration period for a single user.
"""
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="password_expiry", verbose_name=_("user"))
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="password_expiry", verbose_name=_("user"), on_delete=models.CASCADE)
expiry = models.PositiveIntegerField(default=0)
2 changes: 1 addition & 1 deletion account/tests/test_password.py
Expand Up @@ -8,13 +8,13 @@
make_password,
)
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import (
TestCase,
modify_settings,
override_settings,
)

from ..compat import reverse
from ..models import (
PasswordExpiry,
PasswordHistory,
Expand Down
2 changes: 1 addition & 1 deletion account/tests/test_views.py
@@ -1,10 +1,10 @@
from django.conf import settings
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings

from django.contrib.auth.models import User

from account.compat import reverse
from account.models import SignupCode, EmailConfirmation


Expand Down
10 changes: 5 additions & 5 deletions account/utils.py
Expand Up @@ -8,12 +8,12 @@
except ImportError: # python 2
from urlparse import urlparse, urlunparse

from django.core import urlresolvers
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponseRedirect, QueryDict

from django.contrib.auth import get_user_model

from account.compat import reverse, NoReverseMatch
from account.conf import settings
from .models import PasswordHistory

Expand Down Expand Up @@ -45,8 +45,8 @@ def default_redirect(request, fallback_url, **kwargs):
return next_url
else:
try:
fallback_url = urlresolvers.reverse(fallback_url)
except urlresolvers.NoReverseMatch:
fallback_url = reverse(fallback_url)
except NoReverseMatch:
if callable(fallback_url):
raise
if "/" not in fallback_url and "." not in fallback_url:
Expand Down Expand Up @@ -89,8 +89,8 @@ def handle_redirect_to_login(request, **kwargs):
if next_url is None:
next_url = request.get_full_path()
try:
login_url = urlresolvers.reverse(login_url)
except urlresolvers.NoReverseMatch:
login_url = reverse(login_url)
except NoReverseMatch:
if callable(login_url):
raise
if "/" not in login_url and "." not in login_url:
Expand Down
18 changes: 9 additions & 9 deletions account/views.py
@@ -1,6 +1,5 @@
from __future__ import unicode_literals

from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.utils.http import base36_to_int, int_to_base36
Expand All @@ -15,6 +14,7 @@
from django.contrib.sites.shortcuts import get_current_site

from account import signals
from account.compat import reverse, is_authenticated
from account.conf import settings
from account.forms import SignupForm, LoginUsernameForm
from account.forms import ChangePasswordForm, PasswordResetForm, PasswordResetTokenForm
Expand Down Expand Up @@ -156,14 +156,14 @@ def setup_signup_code(self):
self.signup_code_present = False

def get(self, *args, **kwargs):
if self.request.user.is_authenticated():
if is_authenticated(self.request.user):
return redirect(default_redirect(self.request, settings.ACCOUNT_LOGIN_REDIRECT_URL))
if not self.is_open():
return self.closed()
return super(SignupView, self).get(*args, **kwargs)

def post(self, *args, **kwargs):
if self.request.user.is_authenticated():
if is_authenticated(self.request.user):
raise Http404()
if not self.is_open():
return self.closed()
Expand Down Expand Up @@ -343,7 +343,7 @@ class LoginView(FormView):
redirect_field_name = "next"

def get(self, *args, **kwargs):
if self.request.user.is_authenticated():
if is_authenticated(self.request.user):
return redirect(self.get_success_url())
return super(LoginView, self).get(*args, **kwargs)

Expand Down Expand Up @@ -404,13 +404,13 @@ class LogoutView(TemplateResponseMixin, View):
redirect_field_name = "next"

def get(self, *args, **kwargs):
if not self.request.user.is_authenticated():
if not is_authenticated(self.request.user):
return redirect(self.get_redirect_url())
ctx = self.get_context_data()
return self.render_to_response(ctx)

def post(self, *args, **kwargs):
if self.request.user.is_authenticated():
if is_authenticated(self.request.user):
auth.logout(self.request)
return redirect(self.get_redirect_url())

Expand Down Expand Up @@ -495,7 +495,7 @@ def get_context_data(self, **kwargs):
return ctx

def get_redirect_url(self):
if self.user.is_authenticated():
if is_authenticated(self.user):
if not settings.ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL:
return settings.ACCOUNT_LOGIN_REDIRECT_URL
return settings.ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL
Expand Down Expand Up @@ -528,12 +528,12 @@ class ChangePasswordView(PasswordMixin, FormView):
fallback_url_setting = "ACCOUNT_PASSWORD_CHANGE_REDIRECT_URL"

def get(self, *args, **kwargs):
if not self.request.user.is_authenticated():
if not is_authenticated(self.request.user):
return redirect("account_password_reset")
return super(ChangePasswordView, self).get(*args, **kwargs)

def post(self, *args, **kwargs):
if not self.request.user.is_authenticated():
if not is_authenticated(self.request.user):
return HttpResponseForbidden()
return super(ChangePasswordView, self).post(*args, **kwargs)

Expand Down
19 changes: 13 additions & 6 deletions runtests.py
Expand Up @@ -52,12 +52,19 @@
]
)

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",
]
if django.VERSION >= (1, 10):
DEFAULT_SETTINGS["MIDDLEWARE"] = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
]
else:
DEFAULT_SETTINGS["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):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -14,6 +14,7 @@
url="http://github.com/pinax/django-user-accounts",
packages=find_packages(),
install_requires=[
"Django>=1.8",
"django-appconf>=1.0.1",
"pytz>=2015.6"
],
Expand Down

0 comments on commit bc63575

Please sign in to comment.