1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
From: Andi Albrecht
Subject: Use _base_content_is_iter or _is_string
Correctly use _base_content_is_iter or _is_string on HttpResponse depending on
Django version.
Origin: upstream, https://bitbucket.org/jespern/django-piston/changeset/3a0d021dd042
--- python-django-piston-0.2.3.orig/piston/resource.py 2011-11-01 09:52:13.000000000 -0400
+++ python-django-piston-0.2.3/piston/resource.py 2012-06-27 12:43:41.424489361 -0400
@@ -1,5 +1,6 @@
import sys, inspect
+import django
from django.http import (HttpResponse, Http404, HttpResponseNotAllowed,
HttpResponseForbidden, HttpResponseServerError)
from django.views.debug import ExceptionReporter
@@ -181,13 +182,15 @@
# If we're looking at a response object which contains non-string
# content, then assume we should use the emitter to format that
# content
- if isinstance(result, HttpResponse) and not result._is_string:
+ if self._use_emitter(result):
status_code = result.status_code
- # Note: We can't use result.content here because that method attempts
- # to convert the content into a string which we don't want.
- # when _is_string is False _container is the raw data
+ # Note: We can't use result.content here because that
+ # method attempts to convert the content into a string
+ # which we don't want. when
+ # _is_string/_base_content_is_iter is False _container is
+ # the raw data
result = result._container
-
+
srl = emitter(result, typemapper, handler, fields, anonymous)
try:
@@ -212,6 +215,16 @@
return e.response
@staticmethod
+ def _use_emitter(result):
+ """True iff result is a HttpResponse and contains non-string content."""
+ if not isinstance(result, HttpResponse):
+ return False
+ elif django.VERSION >= (1, 4):
+ return not result._base_content_is_iter
+ else:
+ return result._is_string
+
+ @staticmethod
def cleanup_request(request):
"""
Removes `oauth_` keys from various dicts on the
--- python-django-piston-0.2.3.orig/piston/utils.py 2011-11-01 09:52:13.000000000 -0400
+++ python-django-piston-0.2.3/piston/utils.py 2012-06-27 12:43:41.424489361 -0400
@@ -1,4 +1,6 @@
import time
+
+import django
from django.http import HttpResponseNotAllowed, HttpResponseForbidden, HttpResponse, HttpResponseBadRequest
from django.core.urlresolvers import reverse
from django.core.cache import cache
@@ -50,24 +52,30 @@
class HttpResponseWrapper(HttpResponse):
"""
- Wrap HttpResponse and make sure that the internal _is_string
- flag is updated when the _set_content method (via the content
- property) is called
+ Wrap HttpResponse and make sure that the internal
+ _is_string/_base_content_is_iter flag is updated when the
+ _set_content method (via the content property) is called
"""
def _set_content(self, content):
"""
- Set the _container and _is_string properties based on the
- type of the value parameter. This logic is in the construtor
- for HttpResponse, but doesn't get repeated when setting
- HttpResponse.content although this bug report (feature request)
- suggests that it should: http://code.djangoproject.com/ticket/9403
+ Set the _container and _is_string /
+ _base_content_is_iter properties based on the type of
+ the value parameter. This logic is in the construtor
+ for HttpResponse, but doesn't get repeated when
+ setting HttpResponse.content although this bug report
+ (feature request) suggests that it should:
+ http://code.djangoproject.com/ticket/9403
"""
+ is_string = False
if not isinstance(content, basestring) and hasattr(content, '__iter__'):
self._container = content
- self._is_string = False
else:
self._container = [content]
- self._is_string = True
+ is_string = True
+ if django.VERSION >= (1, 4):
+ self._base_content_is_iter = not is_string
+ else:
+ self._is_string = is_string
content = property(HttpResponse._get_content, _set_content)
|