From bf20cb38bf161ff914b557dab35d870ecf61e39a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 5 Aug 2016 10:14:42 +0100 Subject: [PATCH] For Django >=1.10 use user.is_authenticated, not user.is_authenticated() --- rest_framework/compat.py | 6 ++++++ rest_framework/permissions.py | 9 ++++++--- rest_framework/throttling.py | 7 ++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 3143e7654..1ab1478f1 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -122,6 +122,12 @@ def _resolve_model(obj): raise ValueError("{0} is not a Django model".format(obj)) +def is_authenticated(user): + if django.VERSION < (1, 10): + return user.is_authenticated() + return user.is_authenticated + + def get_related_model(field): if django.VERSION < (1, 9): return _resolve_model(field.rel.to) diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index 8f5de0256..dd2d35ccd 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -5,6 +5,9 @@ from __future__ import unicode_literals from django.http import Http404 +from rest_framework.compat import is_authenticated + + SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS') @@ -44,7 +47,7 @@ class IsAuthenticated(BasePermission): """ def has_permission(self, request, view): - return request.user and request.user.is_authenticated() + return request.user and is_authenticated(request.user) class IsAdminUser(BasePermission): @@ -65,7 +68,7 @@ class IsAuthenticatedOrReadOnly(BasePermission): return ( request.method in SAFE_METHODS or request.user and - request.user.is_authenticated() + is_authenticated(request.user) ) @@ -127,7 +130,7 @@ class DjangoModelPermissions(BasePermission): return ( request.user and - (request.user.is_authenticated() or not self.authenticated_users_only) and + (is_authenticated(request.user) or not self.authenticated_users_only) and request.user.has_perms(perms) ) diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 1449f501b..57f24d13f 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -8,6 +8,7 @@ import time from django.core.cache import cache as default_cache from django.core.exceptions import ImproperlyConfigured +from rest_framework.compat import is_authenticated from rest_framework.settings import api_settings @@ -173,7 +174,7 @@ class AnonRateThrottle(SimpleRateThrottle): scope = 'anon' def get_cache_key(self, request, view): - if request.user.is_authenticated(): + if is_authenticated(request.user): return None # Only throttle unauthenticated requests. return self.cache_format % { @@ -193,7 +194,7 @@ class UserRateThrottle(SimpleRateThrottle): scope = 'user' def get_cache_key(self, request, view): - if request.user.is_authenticated(): + if is_authenticated(request.user): ident = request.user.pk else: ident = self.get_ident(request) @@ -241,7 +242,7 @@ class ScopedRateThrottle(SimpleRateThrottle): Otherwise generate the unique cache key by concatenating the user id with the '.throttle_scope` property of the view. """ - if request.user.is_authenticated(): + if is_authenticated(request.user): ident = request.user.pk else: ident = self.get_ident(request)