aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/compat.py
diff options
context:
space:
mode:
authorTom Christie2015-02-06 14:35:06 +0000
committerTom Christie2015-02-06 14:35:06 +0000
commit3dff9a4fe2952cf632ca7f4cd9ecf4221059ca91 (patch)
tree0649d42b20b875e97cb551b987644b61e7860e84 /rest_framework/compat.py
parentc06a82d0531f4cb290baacee196829c770913eaa (diff)
parent1f996128458570a909d13f15c3d739fb12111984 (diff)
downloaddjango-rest-framework-model-serializer-caching.tar.bz2
Resolve merge conflictmodel-serializer-caching
Diffstat (limited to 'rest_framework/compat.py')
-rw-r--r--rest_framework/compat.py35
1 files changed, 31 insertions, 4 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 3c8fb0da..50f37014 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -8,7 +8,7 @@ from __future__ import unicode_literals
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.utils.encoding import force_text
-from django.utils.six.moves.urllib import parse as urlparse
+from django.utils.six.moves.urllib.parse import urlparse as _urlparse
from django.utils import six
import django
import inspect
@@ -18,7 +18,7 @@ def unicode_repr(instance):
# Get the repr of an instance, but ensure it is a unicode string
# on both python 3 (already the case) and 2 (not the case).
if six.PY2:
- repr(instance).decode('utf-8')
+ return repr(instance).decode('utf-8')
return repr(instance)
@@ -38,10 +38,18 @@ def unicode_http_header(value):
return value
+def total_seconds(timedelta):
+ # TimeDelta.total_seconds() is only available in Python 2.7
+ if hasattr(timedelta, 'total_seconds'):
+ return timedelta.total_seconds()
+ else:
+ return (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0)
+
+
# 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.
+# For Django <= 1.6 and Python 2.6 fall back to SortedDict.
try:
from collections import OrderedDict
except ImportError:
@@ -55,6 +63,23 @@ except ImportError:
from django.http import HttpResponse as HttpResponseBase
+# contrib.postgres only supported from 1.8 onwards.
+try:
+ from django.contrib.postgres import fields as postgres_fields
+except ImportError:
+ postgres_fields = None
+
+
+# request only provides `resolver_match` from 1.5 onwards.
+def get_resolver_match(request):
+ try:
+ return request.resolver_match
+ except AttributeError:
+ # Django < 1.5
+ from django.core.urlresolvers import resolve
+ return resolve(request.path_info)
+
+
# django-filter is optional
try:
import django_filters
@@ -177,7 +202,7 @@ except ImportError:
class RequestFactory(DjangoRequestFactory):
def generic(self, method, path,
data='', content_type='application/octet-stream', **extra):
- parsed = urlparse.urlparse(path)
+ parsed = _urlparse(path)
data = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET)
r = {
'PATH_INFO': self._get_path(parsed),
@@ -217,6 +242,8 @@ except ImportError:
if six.PY3:
SHORT_SEPARATORS = (',', ':')
LONG_SEPARATORS = (', ', ': ')
+ INDENT_SEPARATORS = (',', ': ')
else:
SHORT_SEPARATORS = (b',', b':')
LONG_SEPARATORS = (b', ', b': ')
+ INDENT_SEPARATORS = (b',', b': ')