mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 04:20:12 +03:00
Extract modern code from is_authenticated() in compat.py and remove.
This commit is contained in:
parent
1e20f5c961
commit
3eed182793
|
@ -115,13 +115,6 @@ def _resolve_model(obj):
|
||||||
raise ValueError("{0} is not a Django model".format(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
|
# TODO: Remove
|
||||||
def is_anonymous(user):
|
def is_anonymous(user):
|
||||||
if django.VERSION < (1, 10):
|
if django.VERSION < (1, 10):
|
||||||
|
|
|
@ -6,7 +6,6 @@ from __future__ import unicode_literals
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
|
||||||
from rest_framework import exceptions
|
from rest_framework import exceptions
|
||||||
from rest_framework.compat import is_authenticated
|
|
||||||
|
|
||||||
SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
|
SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
|
||||||
|
|
||||||
|
@ -47,7 +46,7 @@ class IsAuthenticated(BasePermission):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def has_permission(self, request, view):
|
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):
|
class IsAdminUser(BasePermission):
|
||||||
|
@ -68,7 +67,7 @@ class IsAuthenticatedOrReadOnly(BasePermission):
|
||||||
return (
|
return (
|
||||||
request.method in SAFE_METHODS or
|
request.method in SAFE_METHODS or
|
||||||
request.user and
|
request.user and
|
||||||
is_authenticated(request.user)
|
request.user.is_authenticated
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -136,7 +135,7 @@ class DjangoModelPermissions(BasePermission):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if not request.user or (
|
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
|
return False
|
||||||
|
|
||||||
queryset = self._queryset(view)
|
queryset = self._queryset(view)
|
||||||
|
|
|
@ -8,7 +8,6 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
@ -174,7 +173,7 @@ class AnonRateThrottle(SimpleRateThrottle):
|
||||||
scope = 'anon'
|
scope = 'anon'
|
||||||
|
|
||||||
def get_cache_key(self, request, view):
|
def get_cache_key(self, request, view):
|
||||||
if is_authenticated(request.user):
|
if request.user.is_authenticated:
|
||||||
return None # Only throttle unauthenticated requests.
|
return None # Only throttle unauthenticated requests.
|
||||||
|
|
||||||
return self.cache_format % {
|
return self.cache_format % {
|
||||||
|
@ -194,7 +193,7 @@ class UserRateThrottle(SimpleRateThrottle):
|
||||||
scope = 'user'
|
scope = 'user'
|
||||||
|
|
||||||
def get_cache_key(self, request, view):
|
def get_cache_key(self, request, view):
|
||||||
if is_authenticated(request.user):
|
if request.user.is_authenticated:
|
||||||
ident = request.user.pk
|
ident = request.user.pk
|
||||||
else:
|
else:
|
||||||
ident = self.get_ident(request)
|
ident = self.get_ident(request)
|
||||||
|
@ -242,7 +241,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 is_authenticated(request.user):
|
if request.user.is_authenticated:
|
||||||
ident = request.user.pk
|
ident = request.user.pk
|
||||||
else:
|
else:
|
||||||
ident = self.get_ident(request)
|
ident = self.get_ident(request)
|
||||||
|
|
|
@ -20,7 +20,6 @@ from rest_framework.authentication import (
|
||||||
TokenAuthentication)
|
TokenAuthentication)
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.authtoken.views import obtain_auth_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.response import Response
|
||||||
from rest_framework.test import APIClient, APIRequestFactory
|
from rest_framework.test import APIClient, APIRequestFactory
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
@ -450,7 +449,7 @@ class FailingAuthAccessedInRenderer(TestCase):
|
||||||
|
|
||||||
def render(self, data, media_type=None, renderer_context=None):
|
def render(self, data, media_type=None, renderer_context=None):
|
||||||
request = renderer_context['request']
|
request = renderer_context['request']
|
||||||
if is_authenticated(request.user):
|
if request.user.is_authenticated:
|
||||||
return b'authenticated'
|
return b'authenticated'
|
||||||
return b'not authenticated'
|
return b'not authenticated'
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ from django.test import override_settings
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
|
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.response import Response
|
||||||
from rest_framework.test import APITestCase, RequestsClient
|
from rest_framework.test import APITestCase, RequestsClient
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
@ -72,7 +72,7 @@ class SessionView(APIView):
|
||||||
class AuthView(APIView):
|
class AuthView(APIView):
|
||||||
@method_decorator(ensure_csrf_cookie)
|
@method_decorator(ensure_csrf_cookie)
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
if is_authenticated(request.user):
|
if request.user.is_authenticated:
|
||||||
username = request.user.username
|
username = request.user.username
|
||||||
else:
|
else:
|
||||||
username = None
|
username = None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user