Skip to content

Commit 91ba271

Browse files
committed
Merge pull request encode#2858 from kazmiruk/version-2.4.x
change SortedDict to OrderedDict + fix list and tuple concatination
2 parents 4a75a9c + fad0848 commit 91ba271

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

rest_framework/compat.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,30 @@ def python_2_unicode_compatible(klass):
265265
klass.__unicode__ = klass.__str__
266266
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
267267
return klass
268+
269+
"""
270+
SortedDict deprecated since django 1.8. There is collections.OrderedDict
271+
which available since python 2.7 and python 3.1. There are no need of other
272+
checks because of django 1.7+ requires python 2.7+
273+
"""
274+
try:
275+
from collections import OrderedDict as SortedDict
276+
except ImportError:
277+
from django.utils.datastructures import SortedDict
278+
279+
"""
280+
GenericForeignKey moves from generic to fields in django 1.9 and in 1.8 shows
281+
deprecation warnings
282+
"""
283+
if django.VERSION >= (1, 8):
284+
from django.contrib.contenttypes.fields import GenericForeignKey
285+
else:
286+
from django.contrib.contenttypes.generic import GenericForeignKey
287+
288+
"""
289+
django.utils.importlib is deprecated since django 1.8
290+
"""
291+
try:
292+
import importlib
293+
except ImportError:
294+
from django.utils import importlib

rest_framework/fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
from django.utils import six, timezone
2323
from django.utils.encoding import is_protected_type
2424
from django.utils.translation import ugettext_lazy as _
25-
from django.utils.datastructures import SortedDict
2625
from django.utils.dateparse import parse_date, parse_datetime, parse_time
2726
from rest_framework import ISO_8601
2827
from rest_framework.compat import (
29-
BytesIO, smart_text,
28+
BytesIO, smart_text, SortedDict,
3029
force_text, is_non_str_iterable
3130
)
3231
from rest_framework.settings import api_settings

rest_framework/routers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from django.conf.urls import patterns, url
2121
from django.core.exceptions import ImproperlyConfigured
2222
from django.core.urlresolvers import NoReverseMatch
23-
from django.utils.datastructures import SortedDict
2423
from rest_framework import views
24+
from rest_framework.compat import SortedDict
2525
from rest_framework.response import Response
2626
from rest_framework.reverse import reverse
2727
from rest_framework.urlpatterns import format_suffix_patterns

rest_framework/serializers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
import inspect
1717
import types
1818
from decimal import Decimal
19-
from django.contrib.contenttypes.generic import GenericForeignKey
2019
from django.core.paginator import Page
2120
from django.db import models
2221
from django.forms import widgets
2322
from django.utils import six
24-
from django.utils.datastructures import SortedDict
2523
from django.utils.functional import cached_property
2624
from django.core.exceptions import ObjectDoesNotExist
25+
from rest_framework.compat import SortedDict, GenericForeignKey
2726
from rest_framework.settings import api_settings
2827

2928

@@ -110,9 +109,17 @@ class SortedDictWithMetadata(SortedDict):
110109
"""
111110
A sorted dict-like object, that can have additional properties attached.
112111
"""
112+
def __reduce__(self):
113+
"""
114+
Used by pickle (e.g., caching) if OrderedDict is used instead of SortedDict
115+
Overriden to remove the metadata from the dict, since it shouldn't be
116+
pickle and may in some instances be unpickleable.
117+
"""
118+
return self.__class__, (SortedDict(self), )
119+
113120
def __getstate__(self):
114121
"""
115-
Used by pickle (e.g., caching).
122+
Used by pickle (e.g., caching) in SortedDict
116123
Overriden to remove the metadata from the dict, since it shouldn't be
117124
pickle and may in some instances be unpickleable.
118125
"""

rest_framework/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"""
2020
from __future__ import unicode_literals
2121
from django.conf import settings
22-
from django.utils import importlib, six
22+
from django.utils import six
2323
from rest_framework import ISO_8601
24+
from rest_framework.compat import importlib
2425

2526

2627
USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None)

rest_framework/utils/encoders.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
from __future__ import unicode_literals
55
from django.utils import timezone
66
from django.db.models.query import QuerySet
7-
from django.utils.datastructures import SortedDict
87
from django.utils.functional import Promise
9-
from rest_framework.compat import force_text
8+
from rest_framework.compat import force_text, SortedDict
109
from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata
1110
import datetime
1211
import decimal

rest_framework/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
from django.core.exceptions import PermissionDenied
77
from django.http import Http404
8-
from django.utils.datastructures import SortedDict
98
from django.views.decorators.csrf import csrf_exempt
109
from rest_framework import status, exceptions
11-
from rest_framework.compat import smart_text, HttpResponseBase, View
10+
from rest_framework.compat import smart_text, HttpResponseBase, SortedDict, View
1211
from rest_framework.request import Request
1312
from rest_framework.response import Response
1413
from rest_framework.settings import api_settings

tests/test_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from django.core import validators
1111
from django.db import models
1212
from django.test import TestCase
13-
from django.utils.datastructures import SortedDict
1413
from rest_framework import serializers
14+
from rest_framework.compat import SortedDict
1515
from tests.models import RESTFrameworkModel
1616

1717

0 commit comments

Comments
 (0)