Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions rest_framework/compat.py
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
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
2 changes: 1 addition & 1 deletion rest_framework/schemas.py
Expand Up @@ -167,7 +167,7 @@ def get_allowed_methods(self, callback):

return [
method for method in
callback.cls().allowed_methods if method != 'OPTIONS'
callback.cls().allowed_methods if method not in ('OPTIONS', 'HEAD')
]

def get_key(self, path, method, callback):
Expand Down
7 changes: 4 additions & 3 deletions rest_framework/throttling.py
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