mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 13:04:03 +03:00
Drop usage of SortedDict. Closes #2027.
This commit is contained in:
parent
9923a6ce90
commit
4e001dbb7a
|
@ -26,6 +26,16 @@ except ImportError:
|
||||||
from django.utils.encoding import force_unicode as force_text
|
from django.utils.encoding import force_unicode as force_text
|
||||||
|
|
||||||
|
|
||||||
|
# OrderedDict only available in Python 2.7.
|
||||||
|
# This will always be the case in Django 1.7 and above, as these versions
|
||||||
|
# no longer support Python 2.6.
|
||||||
|
# For Django <= 1.6 and Python 2.6 fall back to OrderedDict.
|
||||||
|
try:
|
||||||
|
from collections import OrderedDict
|
||||||
|
except:
|
||||||
|
from django.utils.datastructures import SortedDict as OrderedDict
|
||||||
|
|
||||||
|
|
||||||
# HttpResponseBase only exists from 1.5 onwards
|
# HttpResponseBase only exists from 1.5 onwards
|
||||||
try:
|
try:
|
||||||
from django.http.response import HttpResponseBase
|
from django.http.response import HttpResponseBase
|
||||||
|
|
|
@ -4,14 +4,13 @@ from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
from django.core.validators import RegexValidator
|
from django.core.validators import RegexValidator
|
||||||
from django.forms import ImageField as DjangoImageField
|
from django.forms import ImageField as DjangoImageField
|
||||||
from django.utils import six, timezone
|
from django.utils import six, timezone
|
||||||
from django.utils.datastructures import SortedDict
|
|
||||||
from django.utils.dateparse import parse_date, parse_datetime, parse_time
|
from django.utils.dateparse import parse_date, parse_datetime, parse_time
|
||||||
from django.utils.encoding import is_protected_type
|
from django.utils.encoding import is_protected_type
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework import ISO_8601
|
from rest_framework import ISO_8601
|
||||||
from rest_framework.compat import (
|
from rest_framework.compat import (
|
||||||
smart_text, EmailValidator, MinValueValidator, MaxValueValidator,
|
smart_text, EmailValidator, MinValueValidator, MaxValueValidator,
|
||||||
MinLengthValidator, MaxLengthValidator, URLValidator
|
MinLengthValidator, MaxLengthValidator, URLValidator, OrderedDict
|
||||||
)
|
)
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
|
@ -917,9 +916,9 @@ class ChoiceField(Field):
|
||||||
for item in choices
|
for item in choices
|
||||||
]
|
]
|
||||||
if all(pairs):
|
if all(pairs):
|
||||||
self.choices = SortedDict([(key, display_value) for key, display_value in choices])
|
self.choices = OrderedDict([(key, display_value) for key, display_value in choices])
|
||||||
else:
|
else:
|
||||||
self.choices = SortedDict([(item, item) for item in choices])
|
self.choices = OrderedDict([(item, item) for item in choices])
|
||||||
|
|
||||||
# Map the string representation of choices to the underlying value.
|
# Map the string representation of choices to the underlying value.
|
||||||
# Allows us to deal with eg. integer choices while supporting either
|
# Allows us to deal with eg. integer choices while supporting either
|
||||||
|
|
|
@ -10,9 +10,8 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils.datastructures import SortedDict
|
|
||||||
from rest_framework import exceptions, serializers
|
from rest_framework import exceptions, serializers
|
||||||
from rest_framework.compat import force_text
|
from rest_framework.compat import force_text, OrderedDict
|
||||||
from rest_framework.request import clone_request
|
from rest_framework.request import clone_request
|
||||||
from rest_framework.utils.field_mapping import ClassLookupDict
|
from rest_framework.utils.field_mapping import ClassLookupDict
|
||||||
|
|
||||||
|
@ -54,7 +53,7 @@ class SimpleMetadata(BaseMetadata):
|
||||||
})
|
})
|
||||||
|
|
||||||
def determine_metadata(self, request, view):
|
def determine_metadata(self, request, view):
|
||||||
metadata = SortedDict()
|
metadata = OrderedDict()
|
||||||
metadata['name'] = view.get_view_name()
|
metadata['name'] = view.get_view_name()
|
||||||
metadata['description'] = view.get_view_description()
|
metadata['description'] = view.get_view_description()
|
||||||
metadata['renders'] = [renderer.media_type for renderer in view.renderer_classes]
|
metadata['renders'] = [renderer.media_type for renderer in view.renderer_classes]
|
||||||
|
@ -97,7 +96,7 @@ class SimpleMetadata(BaseMetadata):
|
||||||
Given an instance of a serializer, return a dictionary of metadata
|
Given an instance of a serializer, return a dictionary of metadata
|
||||||
about its fields.
|
about its fields.
|
||||||
"""
|
"""
|
||||||
return SortedDict([
|
return OrderedDict([
|
||||||
(field_name, self.get_field_info(field))
|
(field_name, self.get_field_info(field))
|
||||||
for field_name, field in serializer.fields.items()
|
for field_name, field in serializer.fields.items()
|
||||||
])
|
])
|
||||||
|
@ -107,7 +106,7 @@ class SimpleMetadata(BaseMetadata):
|
||||||
Given an instance of a serializer field, return a dictionary
|
Given an instance of a serializer field, return a dictionary
|
||||||
of metadata about it.
|
of metadata about it.
|
||||||
"""
|
"""
|
||||||
field_info = SortedDict()
|
field_info = OrderedDict()
|
||||||
field_info['type'] = self.label_lookup[field]
|
field_info['type'] = self.label_lookup[field]
|
||||||
field_info['required'] = getattr(field, 'required', False)
|
field_info['required'] = getattr(field, 'required', False)
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,8 @@ from collections import namedtuple
|
||||||
from django.conf.urls import patterns, url
|
from django.conf.urls import patterns, url
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.urlresolvers import NoReverseMatch
|
from django.core.urlresolvers import NoReverseMatch
|
||||||
from django.utils.datastructures import SortedDict
|
|
||||||
from rest_framework import views
|
from rest_framework import views
|
||||||
|
from rest_framework.compat import OrderedDict
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
from rest_framework.urlpatterns import format_suffix_patterns
|
from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
|
@ -278,7 +278,7 @@ class DefaultRouter(SimpleRouter):
|
||||||
"""
|
"""
|
||||||
Return a view to use as the API root.
|
Return a view to use as the API root.
|
||||||
"""
|
"""
|
||||||
api_root_dict = SortedDict()
|
api_root_dict = OrderedDict()
|
||||||
list_name = self.routes[0].name
|
list_name = self.routes[0].name
|
||||||
for prefix, viewset, basename in self.registry:
|
for prefix, viewset, basename in self.registry:
|
||||||
api_root_dict[prefix] = list_name.format(basename=basename)
|
api_root_dict[prefix] = list_name.format(basename=basename)
|
||||||
|
@ -287,7 +287,7 @@ class DefaultRouter(SimpleRouter):
|
||||||
_ignore_model_permissions = True
|
_ignore_model_permissions = True
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
ret = SortedDict()
|
ret = OrderedDict()
|
||||||
for key, url_name in api_root_dict.items():
|
for key, url_name in api_root_dict.items():
|
||||||
try:
|
try:
|
||||||
ret[key] = reverse(
|
ret[key] = reverse(
|
||||||
|
|
|
@ -14,8 +14,8 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.fields import FieldDoesNotExist
|
from django.db.models.fields import FieldDoesNotExist
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.datastructures import SortedDict
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from rest_framework.compat import OrderedDict
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.fields import empty, set_value, Field, SkipField
|
from rest_framework.fields import empty, set_value, Field, SkipField
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
|
@ -159,7 +159,7 @@ class BaseSerializer(Field):
|
||||||
# Serializer & ListSerializer classes
|
# Serializer & ListSerializer classes
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
|
|
||||||
class ReturnDict(SortedDict):
|
class ReturnDict(OrderedDict):
|
||||||
"""
|
"""
|
||||||
Return object from `serialier.data` for the `Serializer` class.
|
Return object from `serialier.data` for the `Serializer` class.
|
||||||
Includes a backlink to the serializer instance for renderers
|
Includes a backlink to the serializer instance for renderers
|
||||||
|
@ -235,7 +235,7 @@ class BindingDict(object):
|
||||||
"""
|
"""
|
||||||
def __init__(self, serializer):
|
def __init__(self, serializer):
|
||||||
self.serializer = serializer
|
self.serializer = serializer
|
||||||
self.fields = SortedDict()
|
self.fields = OrderedDict()
|
||||||
|
|
||||||
def __setitem__(self, key, field):
|
def __setitem__(self, key, field):
|
||||||
self.fields[key] = field
|
self.fields[key] = field
|
||||||
|
@ -280,7 +280,7 @@ class SerializerMetaclass(type):
|
||||||
if hasattr(base, '_declared_fields'):
|
if hasattr(base, '_declared_fields'):
|
||||||
fields = list(base._declared_fields.items()) + fields
|
fields = list(base._declared_fields.items()) + fields
|
||||||
|
|
||||||
return SortedDict(fields)
|
return OrderedDict(fields)
|
||||||
|
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
attrs['_declared_fields'] = cls._get_declared_fields(bases, attrs)
|
attrs['_declared_fields'] = cls._get_declared_fields(bases, attrs)
|
||||||
|
@ -679,7 +679,7 @@ class ModelSerializer(Serializer):
|
||||||
def get_fields(self):
|
def get_fields(self):
|
||||||
declared_fields = copy.deepcopy(self._declared_fields)
|
declared_fields = copy.deepcopy(self._declared_fields)
|
||||||
|
|
||||||
ret = SortedDict()
|
ret = OrderedDict()
|
||||||
model = getattr(self.Meta, 'model')
|
model = getattr(self.Meta, 'model')
|
||||||
fields = getattr(self.Meta, 'fields', None)
|
fields = getattr(self.Meta, 'fields', None)
|
||||||
exclude = getattr(self.Meta, 'exclude', None)
|
exclude = getattr(self.Meta, 'exclude', None)
|
||||||
|
|
|
@ -4,9 +4,8 @@ Helper classes for parsers.
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
from django.utils import six, timezone
|
from django.utils import six, timezone
|
||||||
from django.utils.datastructures import SortedDict
|
|
||||||
from django.utils.functional import Promise
|
from django.utils.functional import Promise
|
||||||
from rest_framework.compat import force_text
|
from rest_framework.compat import force_text, OrderedDict
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import types
|
import types
|
||||||
|
@ -68,7 +67,7 @@ else:
|
||||||
class SafeDumper(yaml.SafeDumper):
|
class SafeDumper(yaml.SafeDumper):
|
||||||
"""
|
"""
|
||||||
Handles decimals as strings.
|
Handles decimals as strings.
|
||||||
Handles SortedDicts as usual dicts, but preserves field order, rather
|
Handles OrderedDicts as usual dicts, but preserves field order, rather
|
||||||
than the usual behaviour of sorting the keys.
|
than the usual behaviour of sorting the keys.
|
||||||
"""
|
"""
|
||||||
def represent_decimal(self, data):
|
def represent_decimal(self, data):
|
||||||
|
@ -82,7 +81,7 @@ else:
|
||||||
best_style = True
|
best_style = True
|
||||||
if hasattr(mapping, 'items'):
|
if hasattr(mapping, 'items'):
|
||||||
mapping = list(mapping.items())
|
mapping = list(mapping.items())
|
||||||
if not isinstance(mapping, SortedDict):
|
if not isinstance(mapping, OrderedDict):
|
||||||
mapping.sort()
|
mapping.sort()
|
||||||
for item_key, item_value in mapping:
|
for item_key, item_value in mapping:
|
||||||
node_key = self.represent_data(item_key)
|
node_key = self.represent_data(item_key)
|
||||||
|
@ -104,7 +103,7 @@ else:
|
||||||
SafeDumper.represent_decimal
|
SafeDumper.represent_decimal
|
||||||
)
|
)
|
||||||
SafeDumper.add_representer(
|
SafeDumper.add_representer(
|
||||||
SortedDict,
|
OrderedDict,
|
||||||
yaml.representer.SafeRepresenter.represent_dict
|
yaml.representer.SafeRepresenter.represent_dict
|
||||||
)
|
)
|
||||||
# SafeDumper.add_representer(
|
# SafeDumper.add_representer(
|
||||||
|
@ -112,7 +111,7 @@ else:
|
||||||
# yaml.representer.SafeRepresenter.represent_dict
|
# yaml.representer.SafeRepresenter.represent_dict
|
||||||
# )
|
# )
|
||||||
# SafeDumper.add_representer(
|
# SafeDumper.add_representer(
|
||||||
# SortedDictWithMetadata,
|
# OrderedDictWithMetadata,
|
||||||
# yaml.representer.SafeRepresenter.represent_dict
|
# yaml.representer.SafeRepresenter.represent_dict
|
||||||
# )
|
# )
|
||||||
SafeDumper.add_representer(
|
SafeDumper.add_representer(
|
||||||
|
|
|
@ -8,7 +8,7 @@ Usage: `get_field_info(model)` returns a `FieldInfo` instance.
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.datastructures import SortedDict
|
from rest_framework.compat import OrderedDict
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,12 +63,12 @@ def get_field_info(model):
|
||||||
pk = pk.rel.to._meta.pk
|
pk = pk.rel.to._meta.pk
|
||||||
|
|
||||||
# Deal with regular fields.
|
# Deal with regular fields.
|
||||||
fields = SortedDict()
|
fields = OrderedDict()
|
||||||
for field in [field for field in opts.fields if field.serialize and not field.rel]:
|
for field in [field for field in opts.fields if field.serialize and not field.rel]:
|
||||||
fields[field.name] = field
|
fields[field.name] = field
|
||||||
|
|
||||||
# Deal with forward relationships.
|
# Deal with forward relationships.
|
||||||
forward_relations = SortedDict()
|
forward_relations = OrderedDict()
|
||||||
for field in [field for field in opts.fields if field.serialize and field.rel]:
|
for field in [field for field in opts.fields if field.serialize and field.rel]:
|
||||||
forward_relations[field.name] = RelationInfo(
|
forward_relations[field.name] = RelationInfo(
|
||||||
model_field=field,
|
model_field=field,
|
||||||
|
@ -89,7 +89,7 @@ def get_field_info(model):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Deal with reverse relationships.
|
# Deal with reverse relationships.
|
||||||
reverse_relations = SortedDict()
|
reverse_relations = OrderedDict()
|
||||||
for relation in opts.get_all_related_objects():
|
for relation in opts.get_all_related_objects():
|
||||||
accessor_name = relation.get_accessor_name()
|
accessor_name = relation.get_accessor_name()
|
||||||
reverse_relations[accessor_name] = RelationInfo(
|
reverse_relations[accessor_name] = RelationInfo(
|
||||||
|
@ -114,14 +114,14 @@ def get_field_info(model):
|
||||||
|
|
||||||
# Shortcut that merges both regular fields and the pk,
|
# Shortcut that merges both regular fields and the pk,
|
||||||
# for simplifying regular field lookup.
|
# for simplifying regular field lookup.
|
||||||
fields_and_pk = SortedDict()
|
fields_and_pk = OrderedDict()
|
||||||
fields_and_pk['pk'] = pk
|
fields_and_pk['pk'] = pk
|
||||||
fields_and_pk[pk.name] = pk
|
fields_and_pk[pk.name] = pk
|
||||||
fields_and_pk.update(fields)
|
fields_and_pk.update(fields)
|
||||||
|
|
||||||
# Shortcut that merges both forward and reverse relationships
|
# Shortcut that merges both forward and reverse relationships
|
||||||
|
|
||||||
relations = SortedDict(
|
relations = OrderedDict(
|
||||||
list(forward_relations.items()) +
|
list(forward_relations.items()) +
|
||||||
list(reverse_relations.items())
|
list(reverse_relations.items())
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user