1. Luke Plant
  2. django-piston

Commits

Jesper Nøhr  committed 1606671 Merge
  • Participants
  • Parent commits 0407e88, 0fe5edc
  • Branches default

Comments (0)

Files changed (5)

File piston/emitters.py

View file
  • Ignore whitespace
 from __future__ import generators
 
-import types, decimal, types, re, inspect
+import decimal, re, inspect
 
 try:
     # yaml isn't standard with python.  It shouldn't be required if it
                 ret = _model(thing, fields=fields)
             elif isinstance(thing, HttpResponse):
                 raise HttpStatusCode(thing)
-            elif isinstance(thing, types.FunctionType):
+            elif inspect.isfunction(thing):
                 if not inspect.getargspec(thing)[0]:
                     ret = _any(thing())
+            elif hasattr(thing, '__emittable__'):
+                f = thing.__emittable__
+                if inspect.ismethod(f) and len(inspect.getargspec(f)[0]) == 1:
+                    ret = _any(f())
             else:
                 ret = smart_unicode(thing, strings_only=True)
 

File tests/test_project/apps/testapp/handlers.py

View file
  • Ignore whitespace
 from piston.handler import BaseHandler
 from piston.utils import rc, validate
 
-from models import TestModel, ExpressiveTestModel, Comment, InheritedModel
+from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject
 from forms import EchoForm
 from test_project.apps.testapp import signals
 
         else:
             return super(AbstractHandler, self).read(request)
 
+class PlainOldObjectHandler(BaseHandler):
+    allowed_methods = ('GET',)
+    fields = ('type', 'field')
+    model = PlainOldObject
+    
+    def read(self, request):
+        return self.model()
 
 class EchoHandler(BaseHandler):
     allowed_methods = ('GET', )

File tests/test_project/apps/testapp/models.py

View file
  • Ignore whitespace
     some_other = models.CharField(max_length=32, default='something else')
     
     class Meta:
-        db_table = 'testing_abstracts'
+        db_table = 'testing_abstracts'
+
+class PlainOldObject(object):
+    def __emittable__(self):
+        return {'type': 'plain',
+                'field': 'a field'}

File tests/test_project/apps/testapp/tests.py

View file
  • Ignore whitespace
         resp = self.client.get('/api/echo', data)
         self.assertEquals(resp.status_code, 200)
         self.assertEquals(data, simplejson.loads(resp.content))
+
+class PlainOldObject(MainTests):
+    def test_plain_object_serialization(self):
+        resp = self.client.get('/api/popo')
+        self.assertEquals(resp.status_code, 200)
+        self.assertEquals({'type': 'plain', 'field': 'a field'}, simplejson.loads(resp.content))
+        

File tests/test_project/apps/testapp/urls.py

View file
  • Ignore whitespace
 from piston.resource import Resource
 from piston.authentication import HttpBasicAuthentication
 
-from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler
+from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler
 
 auth = HttpBasicAuthentication(realm='TestApplication')
 
 expressive = Resource(handler=ExpressiveHandler, authentication=auth)
 abstract = Resource(handler=AbstractHandler, authentication=auth)
 echo = Resource(handler=EchoHandler)
+popo = Resource(handler=PlainOldObjectHandler)
 
 
 urlpatterns = patterns('',
     url(r'^abstract/(?P<id_>\d+)\.(?P<emitter_format>.+)$', abstract),
 
     url(r'^echo$', echo),
+    
+    url(r'^popo$', popo),
 )