diff options
| author | Tom Christie | 2013-06-28 17:17:39 +0100 | 
|---|---|---|
| committer | Tom Christie | 2013-06-28 17:17:39 +0100 | 
| commit | 7224b20d58ceee22abc987980ab646ab8cb2d8dc (patch) | |
| tree | acbe0ee1eb19e22d561ca93e7fdab18f3a1e53ff /rest_framework/compat.py | |
| parent | 4ee9cdc7aff30fc3f45e78292da77b5989bb0e23 (diff) | |
| download | django-rest-framework-7224b20d58ceee22abc987980ab646ab8cb2d8dc.tar.bz2 | |
Added APIRequestFactory
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 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 | 
