mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
2.2's PendingDeprecationWarnings now become DeprecationWarnings. 2.3's PendingDeprecationWarnings added.
This commit is contained in:
parent
70831ad0bb
commit
dc7b1d6430
|
@ -200,9 +200,9 @@ class WritableField(Field):
|
|||
|
||||
# 'blank' is to be deprecated in favor of 'required'
|
||||
if blank is not None:
|
||||
warnings.warn('The `blank` keyword argument is due to deprecated. '
|
||||
warnings.warn('The `blank` keyword argument is deprecated. '
|
||||
'Use the `required` keyword argument instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
required = not(blank)
|
||||
|
||||
super(WritableField, self).__init__(source=source)
|
||||
|
|
|
@ -2,13 +2,16 @@
|
|||
Generic views that provide commonly needed behaviour.
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
from rest_framework import views, mixins
|
||||
from rest_framework.settings import api_settings
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.paginator import Paginator, InvalidPage
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import ugettext as _
|
||||
from rest_framework import views, mixins
|
||||
from rest_framework.exceptions import ConfigurationError
|
||||
from rest_framework.settings import api_settings
|
||||
import warnings
|
||||
|
||||
|
||||
class GenericAPIView(views.APIView):
|
||||
|
@ -94,7 +97,12 @@ class GenericAPIView(views.APIView):
|
|||
"""
|
||||
deprecated_style = False
|
||||
if page_size is not None:
|
||||
# TODO: Deperecation warning
|
||||
warnings.warn('The `page_size` parameter to `paginate_queryset()` '
|
||||
'is due to be deprecated. '
|
||||
'Note that the return style of this method is also '
|
||||
'changed, and will simply return a page object '
|
||||
'when called without a `page_size` argument.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
deprecated_style = True
|
||||
else:
|
||||
# Determine the required page size.
|
||||
|
@ -155,7 +163,9 @@ class GenericAPIView(views.APIView):
|
|||
Otherwise defaults to using `self.paginate_by`.
|
||||
"""
|
||||
if queryset is not None:
|
||||
pass # TODO: Deprecation warning
|
||||
warnings.warn('The `queryset` parameter to `get_paginate_by()` '
|
||||
'is due to be deprecated.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
|
||||
if self.paginate_by_param:
|
||||
query_params = self.request.QUERY_PARAMS
|
||||
|
@ -226,17 +236,27 @@ class GenericAPIView(views.APIView):
|
|||
|
||||
if lookup is not None:
|
||||
filter_kwargs = {self.lookup_field: lookup}
|
||||
elif pk is not None:
|
||||
# TODO: Deprecation warning
|
||||
elif pk is not None and self.lookup_field == 'pk':
|
||||
warnings.warn(
|
||||
'The `pk_url_kwarg` attribute is due to be deprecated. '
|
||||
'Use the `lookup_field` attribute instead',
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
filter_kwargs = {'pk': pk}
|
||||
elif slug is not None:
|
||||
# TODO: Deprecation warning
|
||||
elif slug is not None and self.lookup_field == 'pk':
|
||||
warnings.warn(
|
||||
'The `slug_url_kwarg` attribute is due to be deprecated. '
|
||||
'Use the `lookup_field` attribute instead',
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
filter_kwargs = {self.slug_field: slug}
|
||||
else:
|
||||
# TODO: Fix error message
|
||||
raise AttributeError("Generic detail view %s must be called with "
|
||||
"either an object pk or a slug."
|
||||
% self.__class__.__name__)
|
||||
raise ConfigurationError(
|
||||
'Expected view %s to be called with a URL keyword argument '
|
||||
'named "%s". Fix your URL conf, or set the `.lookup_field` '
|
||||
'attribute on the view correctly.' %
|
||||
(self.__class__.__name__, self.lookup_field)
|
||||
)
|
||||
|
||||
obj = get_object_or_404(queryset, **filter_kwargs)
|
||||
|
||||
|
@ -391,8 +411,20 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
|||
##########################
|
||||
|
||||
class MultipleObjectAPIView(GenericAPIView):
|
||||
pass
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn(
|
||||
'Subclassing `MultipleObjectAPIView` is due to be deprecated. '
|
||||
'You should simply subclass `GenericAPIView` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2
|
||||
)
|
||||
super(MultipleObjectAPIView, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class SingleObjectAPIView(GenericAPIView):
|
||||
pass
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn(
|
||||
'Subclassing `SingleObjectAPIView` is due to be deprecated. '
|
||||
'You should simply subclass `GenericAPIView` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2
|
||||
)
|
||||
super(SingleObjectAPIView, self).__init__(*args, **kwargs)
|
||||
|
|
|
@ -26,9 +26,11 @@ class BasePermission(object):
|
|||
Return `True` if permission is granted, `False` otherwise.
|
||||
"""
|
||||
if len(inspect.getargspec(self.has_permission).args) == 4:
|
||||
warnings.warn('The `obj` argument in `has_permission` is due to be deprecated. '
|
||||
'Use `has_object_permission()` instead for object permissions.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
warnings.warn(
|
||||
'The `obj` argument in `has_permission` is deprecated. '
|
||||
'Use `has_object_permission()` instead for object permissions.',
|
||||
DeprecationWarning, stacklevel=2
|
||||
)
|
||||
return self.has_permission(request, view, obj)
|
||||
return True
|
||||
|
||||
|
|
|
@ -42,9 +42,9 @@ class RelatedField(WritableField):
|
|||
|
||||
# 'null' is to be deprecated in favor of 'required'
|
||||
if 'null' in kwargs:
|
||||
warnings.warn('The `null` keyword argument is due to be deprecated. '
|
||||
warnings.warn('The `null` keyword argument is deprecated. '
|
||||
'Use the `required` keyword argument instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
kwargs['required'] = not kwargs.pop('null')
|
||||
|
||||
self.queryset = kwargs.pop('queryset', None)
|
||||
|
@ -328,9 +328,9 @@ class HyperlinkedRelatedField(RelatedField):
|
|||
|
||||
if request is None:
|
||||
warnings.warn("Using `HyperlinkedRelatedField` without including the "
|
||||
"request in the serializer context is due to be deprecated. "
|
||||
"request in the serializer context is deprecated. "
|
||||
"Add `context={'request': request}` when instantiating the serializer.",
|
||||
PendingDeprecationWarning, stacklevel=4)
|
||||
DeprecationWarning, stacklevel=4)
|
||||
|
||||
pk = getattr(obj, 'pk', None)
|
||||
if pk is None:
|
||||
|
@ -443,9 +443,9 @@ class HyperlinkedIdentityField(Field):
|
|||
|
||||
if request is None:
|
||||
warnings.warn("Using `HyperlinkedIdentityField` without including the "
|
||||
"request in the serializer context is due to be deprecated. "
|
||||
"request in the serializer context is deprecated. "
|
||||
"Add `context={'request': request}` when instantiating the serializer.",
|
||||
PendingDeprecationWarning, stacklevel=4)
|
||||
DeprecationWarning, stacklevel=4)
|
||||
|
||||
# By default use whatever format is given for the current context
|
||||
# unless the target is a different type to the source.
|
||||
|
@ -488,35 +488,35 @@ class HyperlinkedIdentityField(Field):
|
|||
|
||||
class ManyRelatedField(RelatedField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn('`ManyRelatedField()` is due to be deprecated. '
|
||||
warnings.warn('`ManyRelatedField()` is deprecated. '
|
||||
'Use `RelatedField(many=True)` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
kwargs['many'] = True
|
||||
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ManyPrimaryKeyRelatedField(PrimaryKeyRelatedField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn('`ManyPrimaryKeyRelatedField()` is due to be deprecated. '
|
||||
warnings.warn('`ManyPrimaryKeyRelatedField()` is deprecated. '
|
||||
'Use `PrimaryKeyRelatedField(many=True)` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
kwargs['many'] = True
|
||||
super(ManyPrimaryKeyRelatedField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ManySlugRelatedField(SlugRelatedField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn('`ManySlugRelatedField()` is due to be deprecated. '
|
||||
warnings.warn('`ManySlugRelatedField()` is deprecated. '
|
||||
'Use `SlugRelatedField(many=True)` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
kwargs['many'] = True
|
||||
super(ManySlugRelatedField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ManyHyperlinkedRelatedField(HyperlinkedRelatedField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn('`ManyHyperlinkedRelatedField()` is due to be deprecated. '
|
||||
warnings.warn('`ManyHyperlinkedRelatedField()` is deprecated. '
|
||||
'Use `HyperlinkedRelatedField(many=True)` instead.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
kwargs['many'] = True
|
||||
super(ManyHyperlinkedRelatedField, self).__init__(*args, **kwargs)
|
||||
|
|
|
@ -13,6 +13,8 @@ For example, you might have a `urls.py` that looks something like this:
|
|||
|
||||
urlpatterns = router.urls
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from collections import namedtuple
|
||||
from django.conf.urls import url, patterns
|
||||
from django.db import models
|
||||
|
|
|
@ -436,9 +436,9 @@ class BaseSerializer(WritableField):
|
|||
else:
|
||||
many = hasattr(data, '__iter__') and not isinstance(data, (Page, dict, six.text_type))
|
||||
if many:
|
||||
warnings.warn('Implict list/queryset serialization is due to be deprecated. '
|
||||
warnings.warn('Implict list/queryset serialization is deprecated. '
|
||||
'Use the `many=True` flag when instantiating the serializer.',
|
||||
PendingDeprecationWarning, stacklevel=3)
|
||||
DeprecationWarning, stacklevel=3)
|
||||
|
||||
if many:
|
||||
ret = []
|
||||
|
@ -498,9 +498,9 @@ class BaseSerializer(WritableField):
|
|||
else:
|
||||
many = hasattr(obj, '__iter__') and not isinstance(obj, (Page, dict))
|
||||
if many:
|
||||
warnings.warn('Implict list/queryset serialization is due to be deprecated. '
|
||||
warnings.warn('Implict list/queryset serialization is deprecated. '
|
||||
'Use the `many=True` flag when instantiating the serializer.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
DeprecationWarning, stacklevel=2)
|
||||
|
||||
if many:
|
||||
self._data = [self.to_native(item) for item in obj]
|
||||
|
@ -606,13 +606,25 @@ class ModelSerializer(Serializer):
|
|||
|
||||
if model_field.rel and nested:
|
||||
if len(inspect.getargspec(self.get_nested_field).args) == 2:
|
||||
# TODO: deprecation warning
|
||||
warnings.warn(
|
||||
'The `get_nested_field(model_field)` call signature '
|
||||
'is due to be deprecated. '
|
||||
'Use `get_nested_field(model_field, related_model, '
|
||||
'to_many) instead',
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
field = self.get_nested_field(model_field)
|
||||
else:
|
||||
field = self.get_nested_field(model_field, related_model, to_many)
|
||||
elif model_field.rel:
|
||||
if len(inspect.getargspec(self.get_nested_field).args) == 3:
|
||||
# TODO: deprecation warning
|
||||
warnings.warn(
|
||||
'The `get_related_field(model_field, to_many)` call '
|
||||
'signature is due to be deprecated. '
|
||||
'Use `get_related_field(model_field, related_model, '
|
||||
'to_many) instead',
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
field = self.get_related_field(model_field, to_many=to_many)
|
||||
else:
|
||||
field = self.get_related_field(model_field, related_model, to_many)
|
||||
|
|
|
@ -357,7 +357,6 @@ class CustomValidationTests(TestCase):
|
|||
|
||||
def validate_email(self, attrs, source):
|
||||
value = attrs[source]
|
||||
|
||||
return attrs
|
||||
|
||||
def validate_content(self, attrs, source):
|
||||
|
@ -1103,7 +1102,7 @@ class DeserializeListTestCase(TestCase):
|
|||
|
||||
def test_no_errors(self):
|
||||
data = [self.data.copy() for x in range(0, 3)]
|
||||
serializer = CommentSerializer(data=data)
|
||||
serializer = CommentSerializer(data=data, many=True)
|
||||
self.assertTrue(serializer.is_valid())
|
||||
self.assertTrue(isinstance(serializer.object, list))
|
||||
self.assertTrue(
|
||||
|
@ -1115,7 +1114,7 @@ class DeserializeListTestCase(TestCase):
|
|||
invalid_item['email'] = ''
|
||||
data = [self.data.copy(), invalid_item, self.data.copy()]
|
||||
|
||||
serializer = CommentSerializer(data=data)
|
||||
serializer = CommentSerializer(data=data, many=True)
|
||||
self.assertFalse(serializer.is_valid())
|
||||
expected = [{}, {'email': ['This field is required.']}, {}]
|
||||
self.assertEqual(serializer.errors, expected)
|
||||
|
|
|
@ -109,7 +109,7 @@ class WritableNestedSerializerBasicTests(TestCase):
|
|||
}
|
||||
]
|
||||
|
||||
serializer = self.AlbumSerializer(data=data)
|
||||
serializer = self.AlbumSerializer(data=data, many=True)
|
||||
self.assertEqual(serializer.is_valid(), False)
|
||||
self.assertEqual(serializer.errors, expected_errors)
|
||||
|
||||
|
@ -241,6 +241,6 @@ class WritableNestedSerializerObjectTests(TestCase):
|
|||
)
|
||||
]
|
||||
|
||||
serializer = self.AlbumSerializer(data=data)
|
||||
serializer = self.AlbumSerializer(data=data, many=True)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
self.assertEqual(serializer.object, expected_object)
|
||||
|
|
|
@ -16,6 +16,8 @@ automatically.
|
|||
router.register(r'users', UserViewSet, 'user')
|
||||
urlpatterns = router.urls
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from functools import update_wrapper
|
||||
from django.utils.decorators import classonlymethod
|
||||
from rest_framework import views, generics, mixins
|
||||
|
|
Loading…
Reference in New Issue
Block a user