From 3eed182793138d3bbbf5da9c73099e5e1c7e3b9a Mon Sep 17 00:00:00 2001 From: Levi Payne Date: Tue, 3 Oct 2017 21:33:12 -0400 Subject: [PATCH] Extract modern code from is_authenticated() in compat.py and remove. --- rest_framework/compat.py | 7 ------- rest_framework/permissions.py | 7 +++---- rest_framework/throttling.py | 7 +++---- tests/test_authentication.py | 3 +-- tests/test_requests_client.py | 4 ++-- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index a6a481d7c..419d8bb99 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -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): diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index dee0032f9..a48058e66 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -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) diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 57f24d13f..422431566 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -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) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index fdbc28a2a..8149ddd81 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -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' diff --git a/tests/test_requests_client.py b/tests/test_requests_client.py index 791ca4ff2..161429f73 100644 --- a/tests/test_requests_client.py +++ b/tests/test_requests_client.py @@ -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