Extract modern code from is_authenticated() in compat.py and remove.

This commit is contained in:
Levi Payne 2017-10-03 21:33:12 -04:00
parent 3a43224fed
commit 0d2f8d3b3c
5 changed files with 9 additions and 19 deletions

View File

@ -115,13 +115,6 @@ def _resolve_model(obj):
raise ValueError("{0} is not a Django model".format(obj))
# TODO: Remove
def is_authenticated(user):
if django.VERSION < (1, 10):
return user.is_authenticated()
return user.is_authenticated
# TODO: Remove
def is_anonymous(user):
if django.VERSION < (1, 10):

View File

@ -6,7 +6,6 @@ from __future__ import unicode_literals
from django.http import Http404
from rest_framework import exceptions
from rest_framework.compat import is_authenticated
SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
@ -47,7 +46,7 @@ class IsAuthenticated(BasePermission):
"""
def has_permission(self, request, view):
return request.user and is_authenticated(request.user)
return request.user and request.user.is_authenticated
class IsAdminUser(BasePermission):
@ -68,7 +67,7 @@ class IsAuthenticatedOrReadOnly(BasePermission):
return (
request.method in SAFE_METHODS or
request.user and
is_authenticated(request.user)
request.user.is_authenticated
)
@ -136,7 +135,7 @@ class DjangoModelPermissions(BasePermission):
return True
if not request.user or (
not is_authenticated(request.user) and self.authenticated_users_only):
not request.user.is_authenticated and self.authenticated_users_only):
return False
queryset = self._queryset(view)

View File

@ -8,7 +8,6 @@ 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
@ -174,7 +173,7 @@ class AnonRateThrottle(SimpleRateThrottle):
scope = 'anon'
def get_cache_key(self, request, view):
if is_authenticated(request.user):
if request.user.is_authenticated:
return None # Only throttle unauthenticated requests.
return self.cache_format % {
@ -194,7 +193,7 @@ class UserRateThrottle(SimpleRateThrottle):
scope = 'user'
def get_cache_key(self, request, view):
if is_authenticated(request.user):
if request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)
@ -242,7 +241,7 @@ class ScopedRateThrottle(SimpleRateThrottle):
Otherwise generate the unique cache key by concatenating the user id
with the '.throttle_scope` property of the view.
"""
if is_authenticated(request.user):
if request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)

View File

@ -20,7 +20,6 @@ from rest_framework.authentication import (
TokenAuthentication)
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework.compat import is_authenticated
from rest_framework.response import Response
from rest_framework.test import APIClient, APIRequestFactory
from rest_framework.views import APIView
@ -450,7 +449,7 @@ class FailingAuthAccessedInRenderer(TestCase):
def render(self, data, media_type=None, renderer_context=None):
request = renderer_context['request']
if is_authenticated(request.user):
if request.user.is_authenticated:
return b'authenticated'
return b'not authenticated'

View File

@ -10,7 +10,7 @@ from django.test import override_settings
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
from rest_framework.compat import is_authenticated, requests
from rest_framework.compat import requests
from rest_framework.response import Response
from rest_framework.test import APITestCase, RequestsClient
from rest_framework.views import APIView
@ -72,7 +72,7 @@ class SessionView(APIView):
class AuthView(APIView):
@method_decorator(ensure_csrf_cookie)
def get(self, request):
if is_authenticated(request.user):
if request.user.is_authenticated:
username = request.user.username
else:
username = None