This commit is contained in:
lijingang 2013-07-24 02:50:26 -07:00
commit a1caf5bf18
8 changed files with 61 additions and 149 deletions

View File

@ -536,6 +536,8 @@ try:
# Any other supported version does use timezone aware datetimes # Any other supported version does use timezone aware datetimes
from django.utils.timezone import now as provider_now from django.utils.timezone import now as provider_now
except ImportError: except ImportError:
import traceback
traceback.print_exc()
oauth2_provider = None oauth2_provider = None
oauth2_provider_models = None oauth2_provider_models = None
oauth2_provider_forms = None oauth2_provider_forms = None

View File

@ -237,13 +237,6 @@ class WritableField(Field):
validators=[], error_messages=None, widget=None, validators=[], error_messages=None, widget=None,
default=None, blank=None): default=None, blank=None):
# 'blank' is to be deprecated in favor of 'required'
if blank is not None:
warnings.warn('The `blank` keyword argument is deprecated. '
'Use the `required` keyword argument instead.',
DeprecationWarning, stacklevel=2)
required = not(blank)
super(WritableField, self).__init__(source=source, label=label, help_text=help_text) super(WritableField, self).__init__(source=source, label=label, help_text=help_text)
self.read_only = read_only self.read_only = read_only

View File

@ -108,11 +108,11 @@ class GenericAPIView(views.APIView):
deprecated_style = False deprecated_style = False
if page_size is not None: if page_size is not None:
warnings.warn('The `page_size` parameter to `paginate_queryset()` ' warnings.warn('The `page_size` parameter to `paginate_queryset()` '
'is due to be deprecated. ' 'is deprecated. '
'Note that the return style of this method is also ' 'Note that the return style of this method is also '
'changed, and will simply return a page object ' 'changed, and will simply return a page object '
'when called without a `page_size` argument.', 'when called without a `page_size` argument.',
PendingDeprecationWarning, stacklevel=2) DeprecationWarning, stacklevel=2)
deprecated_style = True deprecated_style = True
else: else:
# Determine the required page size. # Determine the required page size.
@ -123,10 +123,10 @@ class GenericAPIView(views.APIView):
if not self.allow_empty: if not self.allow_empty:
warnings.warn( warnings.warn(
'The `allow_empty` parameter is due to be deprecated. ' 'The `allow_empty` parameter is deprecated. '
'To use `allow_empty=False` style behavior, You should override ' 'To use `allow_empty=False` style behavior, You should override '
'`get_queryset()` and explicitly raise a 404 on empty querysets.', '`get_queryset()` and explicitly raise a 404 on empty querysets.',
PendingDeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
paginator = self.paginator_class(queryset, page_size, paginator = self.paginator_class(queryset, page_size,
@ -166,10 +166,10 @@ class GenericAPIView(views.APIView):
if not filter_backends and self.filter_backend: if not filter_backends and self.filter_backend:
warnings.warn( warnings.warn(
'The `filter_backend` attribute and `FILTER_BACKEND` setting ' 'The `filter_backend` attribute and `FILTER_BACKEND` setting '
'are due to be deprecated in favor of a `filter_backends` ' 'are deprecated in favor of a `filter_backends` '
'attribute and `DEFAULT_FILTER_BACKENDS` setting, that take ' 'attribute and `DEFAULT_FILTER_BACKENDS` setting, that take '
'a *list* of filter backend classes.', 'a *list* of filter backend classes.',
PendingDeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
filter_backends = [self.filter_backend] filter_backends = [self.filter_backend]
@ -192,8 +192,8 @@ class GenericAPIView(views.APIView):
""" """
if queryset is not None: if queryset is not None:
warnings.warn('The `queryset` parameter to `get_paginate_by()` ' warnings.warn('The `queryset` parameter to `get_paginate_by()` '
'is due to be deprecated.', 'is deprecated.',
PendingDeprecationWarning, stacklevel=2) DeprecationWarning, stacklevel=2)
if self.paginate_by_param: if self.paginate_by_param:
query_params = self.request.QUERY_PARAMS query_params = self.request.QUERY_PARAMS
@ -272,16 +272,16 @@ class GenericAPIView(views.APIView):
filter_kwargs = {self.lookup_field: lookup} filter_kwargs = {self.lookup_field: lookup}
elif pk is not None and self.lookup_field == 'pk': elif pk is not None and self.lookup_field == 'pk':
warnings.warn( warnings.warn(
'The `pk_url_kwarg` attribute is due to be deprecated. ' 'The `pk_url_kwarg` attribute is deprecated. '
'Use the `lookup_field` attribute instead', 'Use the `lookup_field` attribute instead',
PendingDeprecationWarning DeprecationWarning
) )
filter_kwargs = {'pk': pk} filter_kwargs = {'pk': pk}
elif slug is not None and self.lookup_field == 'pk': elif slug is not None and self.lookup_field == 'pk':
warnings.warn( warnings.warn(
'The `slug_url_kwarg` attribute is due to be deprecated. ' 'The `slug_url_kwarg` attribute is deprecated. '
'Use the `lookup_field` attribute instead', 'Use the `lookup_field` attribute instead',
PendingDeprecationWarning DeprecationWarning
) )
filter_kwargs = {self.slug_field: slug} filter_kwargs = {self.slug_field: slug}
else: else:
@ -482,9 +482,9 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
class MultipleObjectAPIView(GenericAPIView): class MultipleObjectAPIView(GenericAPIView):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
warnings.warn( warnings.warn(
'Subclassing `MultipleObjectAPIView` is due to be deprecated. ' 'Subclassing `MultipleObjectAPIView` is deprecated. '
'You should simply subclass `GenericAPIView` instead.', 'You should simply subclass `GenericAPIView` instead.',
PendingDeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
super(MultipleObjectAPIView, self).__init__(*args, **kwargs) super(MultipleObjectAPIView, self).__init__(*args, **kwargs)
@ -492,8 +492,8 @@ class MultipleObjectAPIView(GenericAPIView):
class SingleObjectAPIView(GenericAPIView): class SingleObjectAPIView(GenericAPIView):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
warnings.warn( warnings.warn(
'Subclassing `SingleObjectAPIView` is due to be deprecated. ' 'Subclassing `SingleObjectAPIView` is deprecated. '
'You should simply subclass `GenericAPIView` instead.', 'You should simply subclass `GenericAPIView` instead.',
PendingDeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
super(SingleObjectAPIView, self).__init__(*args, **kwargs) super(SingleObjectAPIView, self).__init__(*args, **kwargs)

View File

@ -24,14 +24,14 @@ def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None)
include = [] include = []
if pk: if pk:
# Pending deprecation # Deprecated
pk_field = obj._meta.pk pk_field = obj._meta.pk
while pk_field.rel: while pk_field.rel:
pk_field = pk_field.rel.to._meta.pk pk_field = pk_field.rel.to._meta.pk
include.append(pk_field.name) include.append(pk_field.name)
if slug_field: if slug_field:
# Pending deprecation # Deprecated
include.append(slug_field) include.append(slug_field)
if lookup_field and lookup_field != 'pk': if lookup_field and lookup_field != 'pk':
@ -77,10 +77,10 @@ class ListModelMixin(object):
# `.allow_empty = False`, to raise 404 errors on empty querysets. # `.allow_empty = False`, to raise 404 errors on empty querysets.
if not self.allow_empty and not self.object_list: if not self.allow_empty and not self.object_list:
warnings.warn( warnings.warn(
'The `allow_empty` parameter is due to be deprecated. ' 'The `allow_empty` parameter is deprecated. '
'To use `allow_empty=False` style behavior, You should override ' 'To use `allow_empty=False` style behavior, You should override '
'`get_queryset()` and explicitly raise a 404 on empty querysets.', '`get_queryset()` and explicitly raise a 404 on empty querysets.',
PendingDeprecationWarning DeprecationWarning
) )
class_name = self.__class__.__name__ class_name = self.__class__.__name__
error_msg = self.empty_error % {'class_name': class_name} error_msg = self.empty_error % {'class_name': class_name}

View File

@ -2,13 +2,10 @@
Provides a set of pluggable permission policies. Provides a set of pluggable permission policies.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import inspect from rest_framework.compat import oauth2_provider_scope, oauth2_constants
import warnings
SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS'] SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']
from rest_framework.compat import oauth2_provider_scope, oauth2_constants
class BasePermission(object): class BasePermission(object):
""" """
@ -25,13 +22,6 @@ class BasePermission(object):
""" """
Return `True` if permission is granted, `False` otherwise. 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 deprecated. '
'Use `has_object_permission()` instead for object permissions.',
DeprecationWarning, stacklevel=2
)
return self.has_permission(request, view, obj)
return True return True

View File

@ -40,14 +40,6 @@ class RelatedField(WritableField):
many = False many = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# 'null' is to be deprecated in favor of 'required'
if 'null' in kwargs:
warnings.warn('The `null` keyword argument is deprecated. '
'Use the `required` keyword argument instead.',
DeprecationWarning, stacklevel=2)
kwargs['required'] = not kwargs.pop('null')
queryset = kwargs.pop('queryset', None) queryset = kwargs.pop('queryset', None)
self.many = kwargs.pop('many', self.many) self.many = kwargs.pop('many', self.many)
if self.many: if self.many:
@ -322,7 +314,7 @@ class HyperlinkedRelatedField(RelatedField):
'incorrect_type': _('Incorrect type. Expected url string, received %s.'), 'incorrect_type': _('Incorrect type. Expected url string, received %s.'),
} }
# These are all pending deprecation # These are all deprecated
pk_url_kwarg = 'pk' pk_url_kwarg = 'pk'
slug_field = 'slug' slug_field = 'slug'
slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden
@ -336,16 +328,16 @@ class HyperlinkedRelatedField(RelatedField):
self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.lookup_field = kwargs.pop('lookup_field', self.lookup_field)
self.format = kwargs.pop('format', None) self.format = kwargs.pop('format', None)
# These are pending deprecation # These are deprecated
if 'pk_url_kwarg' in kwargs: if 'pk_url_kwarg' in kwargs:
msg = 'pk_url_kwarg is pending deprecation. Use lookup_field instead.' msg = 'pk_url_kwarg is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
if 'slug_url_kwarg' in kwargs: if 'slug_url_kwarg' in kwargs:
msg = 'slug_url_kwarg is pending deprecation. Use lookup_field instead.' msg = 'slug_url_kwarg is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
if 'slug_field' in kwargs: if 'slug_field' in kwargs:
msg = 'slug_field is pending deprecation. Use lookup_field instead.' msg = 'slug_field is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
self.pk_url_kwarg = kwargs.pop('pk_url_kwarg', self.pk_url_kwarg) self.pk_url_kwarg = kwargs.pop('pk_url_kwarg', self.pk_url_kwarg)
self.slug_field = kwargs.pop('slug_field', self.slug_field) self.slug_field = kwargs.pop('slug_field', self.slug_field)
@ -388,9 +380,9 @@ class HyperlinkedRelatedField(RelatedField):
# If the lookup succeeds using the default slug params, # If the lookup succeeds using the default slug params,
# then `slug_field` is being used implicitly, and we # then `slug_field` is being used implicitly, and we
# we need to warn about the pending deprecation. # we need to warn about the pending deprecation.
msg = 'Implicit slug field hyperlinked fields are pending deprecation.' \ msg = 'Implicit slug field hyperlinked fields are deprecated.' \
'You should set `lookup_field=slug` on the HyperlinkedRelatedField.' 'You should set `lookup_field=slug` on the HyperlinkedRelatedField.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
return ret return ret
except NoReverseMatch: except NoReverseMatch:
pass pass
@ -424,14 +416,11 @@ class HyperlinkedRelatedField(RelatedField):
request = self.context.get('request', None) request = self.context.get('request', None)
format = self.format or self.context.get('format', None) format = self.format or self.context.get('format', None)
if request is None: assert request is not None, (
msg = ( "`HyperlinkedRelatedField` requires the request in the serializer "
"Using `HyperlinkedRelatedField` without including the request " "context. Add `context={'request': request}` when instantiating "
"in the serializer context is deprecated. " "the serializer."
"Add `context={'request': request}` when instantiating " )
"the serializer."
)
warnings.warn(msg, DeprecationWarning, stacklevel=4)
# If the object has not yet been saved then we cannot hyperlink to it. # If the object has not yet been saved then we cannot hyperlink to it.
if getattr(obj, 'pk', None) is None: if getattr(obj, 'pk', None) is None:
@ -491,7 +480,7 @@ class HyperlinkedIdentityField(Field):
lookup_field = 'pk' lookup_field = 'pk'
read_only = True read_only = True
# These are all pending deprecation # These are all deprecated
pk_url_kwarg = 'pk' pk_url_kwarg = 'pk'
slug_field = 'slug' slug_field = 'slug'
slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden
@ -507,16 +496,16 @@ class HyperlinkedIdentityField(Field):
lookup_field = kwargs.pop('lookup_field', None) lookup_field = kwargs.pop('lookup_field', None)
self.lookup_field = lookup_field or self.lookup_field self.lookup_field = lookup_field or self.lookup_field
# These are pending deprecation # These are deprecated
if 'pk_url_kwarg' in kwargs: if 'pk_url_kwarg' in kwargs:
msg = 'pk_url_kwarg is pending deprecation. Use lookup_field instead.' msg = 'pk_url_kwarg is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
if 'slug_url_kwarg' in kwargs: if 'slug_url_kwarg' in kwargs:
msg = 'slug_url_kwarg is pending deprecation. Use lookup_field instead.' msg = 'slug_url_kwarg is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
if 'slug_field' in kwargs: if 'slug_field' in kwargs:
msg = 'slug_field is pending deprecation. Use lookup_field instead.' msg = 'slug_field is deprecated. Use lookup_field instead.'
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) warnings.warn(msg, DeprecationWarning, stacklevel=2)
self.slug_field = kwargs.pop('slug_field', self.slug_field) self.slug_field = kwargs.pop('slug_field', self.slug_field)
default_slug_kwarg = self.slug_url_kwarg or self.slug_field default_slug_kwarg = self.slug_url_kwarg or self.slug_field
@ -530,11 +519,11 @@ class HyperlinkedIdentityField(Field):
format = self.context.get('format', None) format = self.context.get('format', None)
view_name = self.view_name view_name = self.view_name
if request is None: assert request is not None, (
warnings.warn("Using `HyperlinkedIdentityField` without including the " "`HyperlinkedIdentityField` requires the request in the serializer"
"request in the serializer context is deprecated. " " context. Add `context={'request': request}` when instantiating "
"Add `context={'request': request}` when instantiating the serializer.", "the serializer."
DeprecationWarning, stacklevel=4) )
# By default use whatever format is given for the current context # By default use whatever format is given for the current context
# unless the target is a different type to the source. # unless the target is a different type to the source.
@ -593,41 +582,3 @@ class HyperlinkedIdentityField(Field):
pass pass
raise NoReverseMatch() raise NoReverseMatch()
### Old-style many classes for backwards compat
class ManyRelatedField(RelatedField):
def __init__(self, *args, **kwargs):
warnings.warn('`ManyRelatedField()` is deprecated. '
'Use `RelatedField(many=True)` instead.',
DeprecationWarning, stacklevel=2)
kwargs['many'] = True
super(ManyRelatedField, self).__init__(*args, **kwargs)
class ManyPrimaryKeyRelatedField(PrimaryKeyRelatedField):
def __init__(self, *args, **kwargs):
warnings.warn('`ManyPrimaryKeyRelatedField()` is deprecated. '
'Use `PrimaryKeyRelatedField(many=True)` instead.',
DeprecationWarning, stacklevel=2)
kwargs['many'] = True
super(ManyPrimaryKeyRelatedField, self).__init__(*args, **kwargs)
class ManySlugRelatedField(SlugRelatedField):
def __init__(self, *args, **kwargs):
warnings.warn('`ManySlugRelatedField()` is deprecated. '
'Use `SlugRelatedField(many=True)` instead.',
DeprecationWarning, stacklevel=2)
kwargs['many'] = True
super(ManySlugRelatedField, self).__init__(*args, **kwargs)
class ManyHyperlinkedRelatedField(HyperlinkedRelatedField):
def __init__(self, *args, **kwargs):
warnings.warn('`ManyHyperlinkedRelatedField()` is deprecated. '
'Use `HyperlinkedRelatedField(many=True)` instead.',
DeprecationWarning, stacklevel=2)
kwargs['many'] = True
super(ManyHyperlinkedRelatedField, self).__init__(*args, **kwargs)

View File

@ -15,7 +15,6 @@ import copy
import datetime import datetime
import types import types
from decimal import Decimal from decimal import Decimal
from django.core.paginator import Page
from django.db import models from django.db import models
from django.forms import widgets from django.forms import widgets
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
@ -141,7 +140,7 @@ class BaseSerializer(WritableField):
_dict_class = SortedDictWithMetadata _dict_class = SortedDictWithMetadata
def __init__(self, instance=None, data=None, files=None, def __init__(self, instance=None, data=None, files=None,
context=None, partial=False, many=None, context=None, partial=False, many=False,
allow_add_remove=False, **kwargs): allow_add_remove=False, **kwargs):
super(BaseSerializer, self).__init__(**kwargs) super(BaseSerializer, self).__init__(**kwargs)
self.opts = self._options_class(self.Meta) self.opts = self._options_class(self.Meta)
@ -348,12 +347,7 @@ class BaseSerializer(WritableField):
if value is None: if value is None:
return None return None
if self.many is not None: if self.many:
many = self.many
else:
many = hasattr(value, '__iter__') and not isinstance(value, (Page, dict, six.text_type))
if many:
return [self.to_native(item) for item in value] return [self.to_native(item) for item in value]
return self.to_native(value) return self.to_native(value)
@ -424,16 +418,7 @@ class BaseSerializer(WritableField):
if self._errors is None: if self._errors is None:
data, files = self.init_data, self.init_files data, files = self.init_data, self.init_files
if self.many is not None: if self.many:
many = self.many
else:
many = hasattr(data, '__iter__') and not isinstance(data, (Page, dict, six.text_type))
if many:
warnings.warn('Implict list/queryset serialization is deprecated. '
'Use the `many=True` flag when instantiating the serializer.',
DeprecationWarning, stacklevel=3)
if many:
ret = [] ret = []
errors = [] errors = []
update = self.object is not None update = self.object is not None
@ -486,16 +471,7 @@ class BaseSerializer(WritableField):
if self._data is None: if self._data is None:
obj = self.object obj = self.object
if self.many is not None: if self.many:
many = self.many
else:
many = hasattr(obj, '__iter__') and not isinstance(obj, (Page, dict))
if many:
warnings.warn('Implict list/queryset serialization is deprecated. '
'Use the `many=True` flag when instantiating the serializer.',
DeprecationWarning, stacklevel=2)
if many:
self._data = [self.to_native(item) for item in obj] self._data = [self.to_native(item) for item in obj]
else: else:
self._data = self.to_native(obj) self._data = self.to_native(obj)
@ -617,10 +593,10 @@ class ModelSerializer(Serializer):
if len(inspect.getargspec(self.get_nested_field).args) == 2: if len(inspect.getargspec(self.get_nested_field).args) == 2:
warnings.warn( warnings.warn(
'The `get_nested_field(model_field)` call signature ' 'The `get_nested_field(model_field)` call signature '
'is due to be deprecated. ' 'is deprecated. '
'Use `get_nested_field(model_field, related_model, ' 'Use `get_nested_field(model_field, related_model, '
'to_many) instead', 'to_many) instead',
PendingDeprecationWarning DeprecationWarning
) )
field = self.get_nested_field(model_field) field = self.get_nested_field(model_field)
else: else:
@ -629,10 +605,10 @@ class ModelSerializer(Serializer):
if len(inspect.getargspec(self.get_nested_field).args) == 3: if len(inspect.getargspec(self.get_nested_field).args) == 3:
warnings.warn( warnings.warn(
'The `get_related_field(model_field, to_many)` call ' 'The `get_related_field(model_field, to_many)` call '
'signature is due to be deprecated. ' 'signature is deprecated. '
'Use `get_related_field(model_field, related_model, ' 'Use `get_related_field(model_field, related_model, '
'to_many) instead', 'to_many) instead',
PendingDeprecationWarning DeprecationWarning
) )
field = self.get_related_field(model_field, to_many=to_many) field = self.get_related_field(model_field, to_many=to_many)
else: else:

View File

@ -1268,7 +1268,7 @@ class NestedSerializerContextTests(TestCase):
model = Album model = Album
fields = ("photo_set", "callable") fields = ("photo_set", "callable")
photo_set = PhotoSerializer(source="photo_set") photo_set = PhotoSerializer(source="photo_set", many=True)
callable = serializers.SerializerMethodField("_callable") callable = serializers.SerializerMethodField("_callable")
def _callable(self, instance): def _callable(self, instance):
@ -1280,7 +1280,7 @@ class NestedSerializerContextTests(TestCase):
albums = None albums = None
class AlbumCollectionSerializer(serializers.Serializer): class AlbumCollectionSerializer(serializers.Serializer):
albums = AlbumSerializer(source="albums") albums = AlbumSerializer(source="albums", many=True)
album1 = Album.objects.create(title="album 1") album1 = Album.objects.create(title="album 1")
album2 = Album.objects.create(title="album 2") album2 = Album.objects.create(title="album 2")