aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/compat.py
diff options
context:
space:
mode:
authorTom Christie2013-07-04 05:50:04 -0700
committerTom Christie2013-07-04 05:50:04 -0700
commit99794773cf6b865b5b860b35db31dea92968c605 (patch)
tree157b09d6b19ee5583d6d32123b3671c1c75adbc9 /rest_framework/compat.py
parenta890116ab31e57af3bd1382c1f17259fa368f988 (diff)
parent7398464b397d37dbcfda13eb6142039fed3e9a19 (diff)
downloaddjango-rest-framework-99794773cf6b865b5b860b35db31dea92968c605.tar.bz2
Merge pull request #962 from tomchristie/test-client
APIClient and APIRequestFactory
Diffstat (limited to 'rest_framework/compat.py')
-rw-r--r--rest_framework/compat.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index cb122846..6f7447ad 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -8,6 +8,7 @@ from __future__ import unicode_literals
import django
from django.core.exceptions import ImproperlyConfigured
+from django.conf import settings
# Try to import six from Django, fallback to included `six`.
try:
@@ -83,7 +84,6 @@ def get_concrete_model(model_cls):
# Django 1.5 add support for custom auth user model
if django.VERSION >= (1, 5):
- from django.conf import settings
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
else:
AUTH_USER_MODEL = 'auth.User'
@@ -436,6 +436,42 @@ except ImportError:
return force_text(url)
+# RequestFactory only provide `generic` from 1.5 onwards
+
+from django.test.client import RequestFactory as DjangoRequestFactory
+from django.test.client import FakePayload
+try:
+ # In 1.5 the test client uses force_bytes
+ from django.utils.encoding import force_bytes_or_smart_bytes
+except ImportError:
+ # In 1.3 and 1.4 the test client just uses smart_str
+ from django.utils.encoding import smart_str as force_bytes_or_smart_bytes
+
+
+class RequestFactory(DjangoRequestFactory):
+ def generic(self, method, path,
+ data='', content_type='application/octet-stream', **extra):
+ parsed = urlparse.urlparse(path)
+ data = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET)
+ r = {
+ 'PATH_INFO': self._get_path(parsed),
+ 'QUERY_STRING': force_text(parsed[4]),
+ 'REQUEST_METHOD': str(method),
+ }
+ if data:
+ r.update({
+ 'CONTENT_LENGTH': len(data),
+ 'CONTENT_TYPE': str(content_type),
+ 'wsgi.input': FakePayload(data),
+ })
+ elif django.VERSION <= (1, 4):
+ # For 1.3 we need an empty WSGI payload
+ r.update({
+ 'wsgi.input': FakePayload('')
+ })
+ r.update(extra)
+ return self.request(**r)
+
# Markdown is optional
try:
import markdown