diff options
| author | Tom Christie | 2013-08-19 20:58:28 +0100 |
|---|---|---|
| committer | Tom Christie | 2013-08-19 20:58:28 +0100 |
| commit | 28e44efe25b5373f0f46357e4e26f7cb0482efa6 (patch) | |
| tree | 9dd36c65ade4b801cfb7e93be7123fc5a5fb69e4 /rest_framework/compat.py | |
| parent | 9e4e2c60f75f596d3f9e32deaab23bf98fc8ef0f (diff) | |
| parent | 34d65119fc1c200b76a8af7213a92d6b279bd478 (diff) | |
| download | django-rest-framework-28e44efe25b5373f0f46357e4e26f7cb0482efa6.tar.bz2 | |
Merge branch 'master' into 2.4.0
Diffstat (limited to 'rest_framework/compat.py')
| -rw-r--r-- | rest_framework/compat.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 161fffa8..baee3a9c 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 |
