Commits

Jesper Nøhr committed 2de9bcc

importing patches from bmcmurray, fixes #42 and #43, adjusting testcases to expect indented json

Comments (0)

Files changed (4)

piston/emitters.py

             elif isinstance(thing, Model):
                 ret = _model(thing, fields=fields)
             elif isinstance(thing, HttpResponse):
-                raise HttpStatusCode(thing.content, code=thing.status_code)
+                raise HttpStatusCode(thing)
             elif isinstance(thing, types.FunctionType):
                 if not inspect.getargspec(thing)[0]:
                     ret = _any(thing())
     """
     def render(self, request):
         cb = request.GET.get('callback')
-        seria = simplejson.dumps(self.construct(), cls=DateTimeAwareJSONEncoder)
+        seria = simplejson.dumps(self.construct(), cls=DateTimeAwareJSONEncoder, ensure_ascii=False, indent=4)
 
         # Callback
         if cb:

piston/resource.py

                 
             result.content = format_error(msg)
         except HttpStatusCode, e:
-            result = e
+            #result = e ## why is this being passed on and not just dealt with now?
+            return e.response
         except Exception, e:
             """
             On errors (like code errors), we'd like to be able to
 
             return resp
         except HttpStatusCode, e:
-            return HttpResponse(e.msg, status=e.code)
+            return e.response
 
     @staticmethod
     def cleanup_request(request):
         self.form = form
 
 class HttpStatusCode(Exception):
-    def __init__(self, msg, code=200):
-        self.msg = msg
-        self.code = code
+    def __init__(self, response):
+        self.response = response
 
 def validate(v_form, operation='POST'):
     @decorator

tests/test_project/apps/testapp/tests.py

         result = self.client.get('/api/abstract.json',
                 HTTP_AUTHORIZATION=self.auth_string).content
                 
-        expected = '[{"id": 1, "some_other": "something else", "some_field": "something here"}, {"id": 2, "some_other": "something else", "some_field": "something here"}]'
+        expected = """[
+    {
+        "id": 1, 
+        "some_other": "something else", 
+        "some_field": "something here"
+    }, 
+    {
+        "id": 2, 
+        "some_other": "something else", 
+        "some_field": "something here"
+    }
+]"""
         
         self.assertEquals(result, expected)
 
     def test_specific_id(self):
         ids = (1, 2)
-        be = '{"id": %d, "some_other": "something else", "some_field": "something here"}'
+        be = """{
+    "id": %d, 
+    "some_other": "something else", 
+    "some_field": "something here"
+}"""
         
         for id_ in ids:
             result = self.client.get('/api/abstract/%d.json' % id_,
                     HTTP_AUTHORIZATION=self.auth_string).content
                     
             expected = be % id_
-                    
+            
             self.assertEquals(result, expected)
 
 class IncomingExpressiveTests(MainTests):
                                       'comments': [ { 'content': 'test1' },
                                                     { 'content': 'test2' } ] })
     
-        expected = '[{"content": "bar", "comments": [], "title": "foo"}, {"content": "bar2", "comments": [], "title": "foo2"}]'
+        expected = """[
+    {
+        "content": "bar", 
+        "comments": [], 
+        "title": "foo"
+    }, 
+    {
+        "content": "bar2", 
+        "comments": [], 
+        "title": "foo2"
+    }
+]"""
     
-        self.assertEquals(self.client.get('/api/expressive.json', 
-            HTTP_AUTHORIZATION=self.auth_string).content, expected)
+        result = self.client.get('/api/expressive.json',
+            HTTP_AUTHORIZATION=self.auth_string).content
+
+        self.assertEquals(result, expected)
         
         resp = self.client.post('/api/expressive.json', outgoing, content_type='application/json',
             HTTP_AUTHORIZATION=self.auth_string)
             
         self.assertEquals(resp.status_code, 201)
         
-        expected = '[{"content": "bar", "comments": [], "title": "foo"}, {"content": "bar2", "comments": [], "title": "foo2"}, {"content": "test", "comments": [{"content": "test1"}, {"content": "test2"}], "title": "test"}]'
+        expected = """[
+    {
+        "content": "bar", 
+        "comments": [], 
+        "title": "foo"
+    }, 
+    {
+        "content": "bar2", 
+        "comments": [], 
+        "title": "foo2"
+    }, 
+    {
+        "content": "test", 
+        "comments": [
+            {
+                "content": "test1"
+            }, 
+            {
+                "content": "test2"
+            }
+        ], 
+        "title": "test"
+    }
+]"""
         
-        self.assertEquals(self.client.get('/api/expressive.json', 
-            HTTP_AUTHORIZATION=self.auth_string).content, expected)
+        result = self.client.get('/api/expressive.json', 
+            HTTP_AUTHORIZATION=self.auth_string).content
+            
+        self.assertEquals(result, expected)
         
     def test_incoming_yaml(self):
         if not yaml: