Access request.user.is_authenticated as property not method, under Django 1.10+ (#4358)

* For Django >=1.10 use user.is_authenticated, not user.is_authenticated()
This commit is contained in:
Tom Christie 2016-08-05 11:04:01 +01:00 committed by GitHub
parent aff146ae83
commit 11a2468379
3 changed files with 16 additions and 6 deletions

View File

@ -122,6 +122,12 @@ def _resolve_model(obj):
raise ValueError("{0} is not a Django model".format(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): def get_related_model(field):
if django.VERSION < (1, 9): if django.VERSION < (1, 9):
return _resolve_model(field.rel.to) return _resolve_model(field.rel.to)

View File

@ -5,6 +5,9 @@ from __future__ import unicode_literals
from django.http import Http404 from django.http import Http404
from rest_framework.compat import is_authenticated
SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS') SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
@ -44,7 +47,7 @@ class IsAuthenticated(BasePermission):
""" """
def has_permission(self, request, view): 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): class IsAdminUser(BasePermission):
@ -65,7 +68,7 @@ class IsAuthenticatedOrReadOnly(BasePermission):
return ( return (
request.method in SAFE_METHODS or request.method in SAFE_METHODS or
request.user and request.user and
request.user.is_authenticated() is_authenticated(request.user)
) )
@ -127,7 +130,7 @@ class DjangoModelPermissions(BasePermission):
return ( return (
request.user and 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) request.user.has_perms(perms)
) )

View File

@ -8,6 +8,7 @@ import time
from django.core.cache import cache as default_cache from django.core.cache import cache as default_cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from rest_framework.compat import is_authenticated
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -173,7 +174,7 @@ class AnonRateThrottle(SimpleRateThrottle):
scope = 'anon' scope = 'anon'
def get_cache_key(self, request, view): def get_cache_key(self, request, view):
if request.user.is_authenticated(): if is_authenticated(request.user):
return None # Only throttle unauthenticated requests. return None # Only throttle unauthenticated requests.
return self.cache_format % { return self.cache_format % {
@ -193,7 +194,7 @@ class UserRateThrottle(SimpleRateThrottle):
scope = 'user' scope = 'user'
def get_cache_key(self, request, view): def get_cache_key(self, request, view):
if request.user.is_authenticated(): if is_authenticated(request.user):
ident = request.user.pk ident = request.user.pk
else: else:
ident = self.get_ident(request) ident = self.get_ident(request)
@ -241,7 +242,7 @@ class ScopedRateThrottle(SimpleRateThrottle):
Otherwise generate the unique cache key by concatenating the user id Otherwise generate the unique cache key by concatenating the user id
with the '.throttle_scope` property of the view. with the '.throttle_scope` property of the view.
""" """
if request.user.is_authenticated(): if is_authenticated(request.user):
ident = request.user.pk ident = request.user.pk
else: else:
ident = self.get_ident(request) ident = self.get_ident(request)