Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix misc django deprecations
  • Loading branch information
Ryan P Kilby committed Sep 15, 2016
commit a084924ced2ee4cb18d0f8c48b603f71ce6d2429
6 changes: 6 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def is_authenticated(user):
return user.is_authenticated


def is_anonymous(user):
if django.VERSION < (1, 10):
return user.is_anonymous()
return user.is_anonymous


def get_related_model(field):
if django.VERSION < (1, 9):
return _resolve_model(field.rel.to)
Expand Down
1 change: 1 addition & 0 deletions tests/browsable_api/test_form_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class BasicSerializer(serializers.ModelSerializer):
class Meta:
model = BasicModel
fields = '__all__'


class ManyPostView(generics.GenericAPIView):
Expand Down
15 changes: 9 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
def pytest_configure():
from django.conf import settings

MIDDLEWARE = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

settings.configure(
DEBUG_PROPAGATE_EXCEPTIONS=True,
DATABASES={
Expand All @@ -21,12 +28,8 @@ def pytest_configure():
'APP_DIRS': True,
},
],
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
),
MIDDLEWARE=MIDDLEWARE,
MIDDLEWARE_CLASSES=MIDDLEWARE,
INSTALLED_APPS=(
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down
32 changes: 16 additions & 16 deletions tests/test_atomic_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf.urls import url
from django.db import connection, connections, transaction
from django.http import Http404
from django.test import TestCase, TransactionTestCase
from django.test import TestCase, TransactionTestCase, override_settings
from django.utils.decorators import method_decorator

from rest_framework import status
Expand Down Expand Up @@ -36,6 +36,20 @@ def post(self, request, *args, **kwargs):
raise APIException


class NonAtomicAPIExceptionView(APIView):
@method_decorator(transaction.non_atomic_requests)
def dispatch(self, *args, **kwargs):
return super(NonAtomicAPIExceptionView, self).dispatch(*args, **kwargs)

def get(self, request, *args, **kwargs):
BasicModel.objects.all()
raise Http404

urlpatterns = (
url(r'^$', NonAtomicAPIExceptionView.as_view()),
)


@unittest.skipUnless(
connection.features.uses_savepoints,
"'atomic' requires transactions and savepoints."
Expand Down Expand Up @@ -124,22 +138,8 @@ def test_api_exception_rollback_transaction(self):
connection.features.uses_savepoints,
"'atomic' requires transactions and savepoints."
)
@override_settings(ROOT_URLCONF='tests.test_atomic_requests')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this test did not fail despite the urls property having been fully deprecated in 1.10

class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase):
@property
def urls(self):
class NonAtomicAPIExceptionView(APIView):
@method_decorator(transaction.non_atomic_requests)
def dispatch(self, *args, **kwargs):
return super(NonAtomicAPIExceptionView, self).dispatch(*args, **kwargs)

def get(self, request, *args, **kwargs):
BasicModel.objects.all()
raise Http404

return (
url(r'^$', NonAtomicAPIExceptionView.as_view()),
)

def setUp(self):
connections.databases['default']['ATOMIC_REQUESTS'] = True

Expand Down
3 changes: 2 additions & 1 deletion tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
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
Expand Down Expand Up @@ -408,7 +409,7 @@ class AuthAccessingRenderer(renderers.BaseRenderer):

def render(self, data, media_type=None, renderer_context=None):
request = renderer_context['request']
if request.user.is_authenticated():
if is_authenticated(request.user):
return b'authenticated'
return b'not authenticated'

Expand Down
2 changes: 1 addition & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class AttributeModel(models.Model):

class SearchFilterModelFk(models.Model):
title = models.CharField(max_length=20)
attribute = models.ForeignKey(AttributeModel)
attribute = models.ForeignKey(AttributeModel, on_delete=models.CASCADE)


class SearchFilterFkSerializer(serializers.ModelSerializer):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ class OneToOneTargetTestModel(models.Model):


class OneToOneSourceTestModel(models.Model):
target = models.OneToOneField(OneToOneTargetTestModel, primary_key=True)
target = models.OneToOneField(OneToOneTargetTestModel, primary_key=True, on_delete=models.CASCADE)


class TestModelFieldValues(TestCase):
Expand Down Expand Up @@ -990,6 +990,7 @@ class Meta:
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = TestModel
fields = '__all__'
extra_kwargs = {'field_1': {'required': False}}

fields = TestSerializer().fields
Expand Down
5 changes: 3 additions & 2 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from rest_framework import status
from rest_framework.authentication import SessionAuthentication
from rest_framework.compat import is_anonymous
from rest_framework.parsers import BaseParser, FormParser, MultiPartParser
from rest_framework.request import Request
from rest_framework.response import Response
Expand Down Expand Up @@ -169,9 +170,9 @@ def test_user_can_login(self):

def test_user_can_logout(self):
self.request.user = self.user
self.assertFalse(self.request.user.is_anonymous())
self.assertFalse(is_anonymous(self.request.user))
logout(self.request)
self.assertTrue(self.request.user.is_anonymous())
self.assertTrue(is_anonymous(self.request.user))

def test_logged_in_user_is_set_on_wrapped_request(self):
login(self.request, self.user)
Expand Down