Commits

Benjamin Pollack  committed 0fe5edc Merge
  • Participants
  • Parent commits 44b2601, 7be2fee

Comments (0)

Files changed (4)

File piston/emitters.py

 except ImportError:
     yaml = None
 
+# Fallback since `any` isn't in Python <2.5
+try:
+    any
+except NameError:
+    def any(iterable):
+        for element in iterable:
+            if element:
+                return True
+        return False
+
 from django.db.models.query import QuerySet
 from django.db.models import Model, permalink
 from django.utils import simplejson
             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 inspect.isfunction(thing):
                 if not inspect.getargspec(thing)[0]:
                     ret = _any(thing())
                 met_fields = self.method_fields(handler, get_fields)
 
                 for f in data._meta.local_fields:
-                    if f.serialize and f.attname not in met_fields:
+                    if f.serialize and not any([ p in met_fields for p in [ f.attname, f.name ]]):
                         if not f.rel:
                             if f.attname in get_fields:
                                 ret[f.attname] = _any(v(f))
                     else:                    
                         maybe = getattr(data, maybe_field, None)
                         if maybe:
-                            if isinstance(maybe, (int, basestring)):
-                                ret[maybe_field] = _any(maybe)
-                            elif callable(maybe):
+                            if callable(maybe):
                                 if len(inspect.getargspec(maybe)[0]) == 1:
                                     ret[maybe_field] = _any(maybe())
+                            else:
+                                ret[maybe_field] = _any(maybe)
                         else:
                             handler_f = getattr(handler or self.handler, maybe_field, None)
 
     """
     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:

File 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):

File piston/utils.py

         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

File 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: