Skip to content

Commit

Permalink
Access request.user.is_authenticated as property not method, under …
Browse files Browse the repository at this point in the history
…Django 1.10+ (#4358)

* For Django >=1.10 use user.is_authenticated, not user.is_authenticated()
  • Loading branch information
tomchristie authored Aug 5, 2016
1 parent aff146a commit 11a2468
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions rest_framework/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

from django.http import Http404

from rest_framework.compat import is_authenticated


SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')


Expand Down Expand Up @@ -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):
Expand All @@ -65,7 +68,7 @@ def has_permission(self, request, view):
return (
request.method in SAFE_METHODS or
request.user and
request.user.is_authenticated()
is_authenticated(request.user)
)


Expand Down Expand Up @@ -127,7 +130,7 @@ def has_permission(self, request, view):

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)
)

Expand Down
7 changes: 4 additions & 3 deletions rest_framework/throttling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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


Expand Down Expand Up @@ -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 % {
Expand All @@ -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)
Expand Down Expand Up @@ -241,7 +242,7 @@ def get_cache_key(self, request, view):
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)
Expand Down

0 comments on commit 11a2468

Please sign in to comment.