Package: python-django-piston
Version: 0.2.3-1
Severity: serious
Tags: upstream patch

Ubuntu has cherry-picked two upstream Django 1.4 compatibility patches
for python-django-piston, but they were never forwarded to Debian:

https://bitbucket.org/jespern/django-piston/changeset/7c90898072ce
https://bitbucket.org/jespern/django-piston/changeset/3a0d021dd042

Upstream hasn't released a version including these patches, yet, so I
propose we cherry-pick them too.

SR

-- System Information:
Debian Release: wheezy/sid
  APT prefers testing
  APT policy: (900, 'testing'), (800, 'unstable'), (1, 'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.2.0-2-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_ZA.UTF-8, LC_CTYPE=en_ZA.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
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)            
 
From: Andi Albrecht
Subject: Piston now supports Django 1.4
 Support for Django 1.4
Origin: upstream, https://bitbucket.org/jespern/django-piston/changeset/7c90898072ce
Bug: https://bitbucket.org/jespern/django-piston/issue/214/django-14-compatibility

--- python-django-piston-0.2.3.orig/piston/resource.py	2012-06-27 12:43:54.384489691 -0400
+++ python-django-piston-0.2.3/piston/resource.py	2012-06-27 12:45:00.316491380 -0400
@@ -73,7 +73,7 @@
         `Resource` subclass.
         """
         resp = rc.BAD_REQUEST
-        resp.write(' '+str(e.form.errors))
+        resp.write(u' '+unicode(e.form.errors))
         return resp
 
     @property
@@ -220,9 +220,9 @@
         if not isinstance(result, HttpResponse):
             return False
         elif django.VERSION >= (1, 4):
-            return not result._base_content_is_iter
+            return result._base_content_is_iter
         else:
-            return result._is_string
+            return not result._is_string
 
     @staticmethod
     def cleanup_request(request):
--- python-django-piston-0.2.3.orig/piston/tests.py	2011-11-01 09:52:13.000000000 -0400
+++ python-django-piston-0.2.3/piston/tests.py	2012-06-27 12:45:00.320491380 -0400
@@ -1,4 +1,5 @@
 # Django imports
+import django
 from django.core import mail
 from django.contrib.auth.models import User
 from django.conf import settings
@@ -100,7 +101,8 @@
          response = resource(request, emitter_format='json')
 
          self.assertEquals(201, response.status_code)
-         self.assertTrue(response._is_string, "Expected response content to be a string")
+         is_string = (not response._base_content_is_iter) if django.VERSION >= (1,4) else response._is_string
+         self.assert_(is_string, "Expected response content to be a string")
 
          # compare the original data dict with the json response 
          # converted to a dict
--- python-django-piston-0.2.3.orig/tests/test_project/settings.py	2011-11-01 09:52:13.000000000 -0400
+++ python-django-piston-0.2.3/tests/test_project/settings.py	2012-06-27 12:45:00.320491380 -0400
@@ -1,5 +1,12 @@
 import os
 DEBUG = True
+DATABASES = {
+    'default':
+        {
+        'ENGINE': 'django.db.backends.sqlite3',
+        'NAME': '/tmp/piston.db'
+    }
+}
 DATABASE_ENGINE = 'sqlite3'
 DATABASE_NAME = '/tmp/piston.db'
 INSTALLED_APPS = (
_______________________________________________
Python-modules-team mailing list
Python-modules-team@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/python-modules-team

Reply via email to