aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'rest_framework/compat.py')
-rw-r--r--rest_framework/compat.py76
1 files changed, 47 insertions, 29 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 7664c400..5508f6c0 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -1,8 +1,23 @@
"""
-The :mod:`compat` module provides support for backwards compatibility with older versions of django/python.
+The `compat` module provides support for backwards compatibility with older
+versions of django/python, and compatibility wrappers around optional packages.
"""
+# flake8: noqa
import django
+# location of patterns, url, include changes in 1.4 onwards
+try:
+ from django.conf.urls import patterns, url, include
+except:
+ from django.conf.urls.defaults import patterns, url, include
+
+# django-filter is optional
+try:
+ import django_filters
+except:
+ django_filters = None
+
+
# cStringIO only if it's available, otherwise StringIO
try:
import cStringIO as StringIO
@@ -10,6 +25,16 @@ except ImportError:
import StringIO
+# Try to import PIL in either of the two ways it can end up installed.
+try:
+ from PIL import Image
+except ImportError:
+ try:
+ import Image
+ except ImportError:
+ Image = None
+
+
def get_concrete_model(model_cls):
try:
return model_cls._meta.concrete_model
@@ -18,6 +43,20 @@ def get_concrete_model(model_cls):
return model_cls
+# Django 1.5 add support for custom auth user model
+if django.VERSION >= (1, 5):
+ from django.conf import settings
+ if hasattr(settings, 'AUTH_USER_MODEL'):
+ User = settings.AUTH_USER_MODEL
+ else:
+ from django.contrib.auth.models import User
+else:
+ try:
+ from django.contrib.auth.models import User
+ except ImportError:
+ raise ImportError(u"User model is not to be found.")
+
+
# First implementation of Django class-based views did not include head method
# in base View class - https://code.djangoproject.com/ticket/15668
if django.VERSION >= (1, 4):
@@ -57,6 +96,12 @@ else:
update_wrapper(view, cls.dispatch, assigned=())
return view
+# Taken from @markotibold's attempt at supporting PATCH.
+# https://github.com/markotibold/django-rest-framework/tree/patch
+http_method_names = set(View.http_method_names)
+http_method_names.add('patch')
+View.http_method_names = list(http_method_names) # PATCH method is not implemented by Django
+
# PUT, DELETE do not require CSRF until 1.4. They should. Make it better.
if django.VERSION >= (1, 4):
from django.middleware.csrf import CsrfViewMiddleware
@@ -331,7 +376,7 @@ try:
"""
extensions = ['headerid(level=2)']
- safe_mode = False,
+ safe_mode = False
md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
return md.convert(text)
@@ -346,33 +391,6 @@ except ImportError:
yaml = None
-import unittest
-try:
- import unittest.skip
-except ImportError: # python < 2.7
- from unittest import TestCase
- import functools
-
- def skip(reason):
- # Pasted from py27/lib/unittest/case.py
- """
- Unconditionally skip a test.
- """
- def decorator(test_item):
- if not (isinstance(test_item, type) and issubclass(test_item, TestCase)):
- @functools.wraps(test_item)
- def skip_wrapper(*args, **kwargs):
- pass
- test_item = skip_wrapper
-
- test_item.__unittest_skip__ = True
- test_item.__unittest_skip_why__ = reason
- return test_item
- return decorator
-
- unittest.skip = skip
-
-
# xml.etree.parse only throws ParseError for python >= 2.7
try:
from xml.etree import ParseError as ETParseError