Commits

dfde...@gmail.com  committed 0694841

[mq]: issue-58

  • Participants
  • Parent commits f440f89

Comments (0)

Files changed (4)

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

 from piston.handler import BaseHandler
 from piston.utils import rc, validate
 
-from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject
+from models import TestModel, ExpressiveTestModel, Comment, InheritedModel, PlainOldObject, Issue58Model
 from forms import EchoForm
 from test_project.apps.testapp import signals
 
     @validate(EchoForm, 'GET')
     def read(self, request):
         return {'msg': request.GET['msg']}
+
+class Issue58Handler(BaseHandler):
+    model = Issue58Model
+
+    def read(self, request):
+        return Issue58Model.objects.all()
+                
+    def create(self, request):
+        if request.content_type:
+            data = request.data
+            em = self.model(read=data['read'], create=data['create'])
+            em.save()
+            return rc.CREATED
+        else:
+            super(Issue58Model, self).create(request)

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

 class PlainOldObject(object):
     def __emittable__(self):
         return {'type': 'plain',
-                'field': 'a field'}
+                'field': 'a field'}
+
+class Issue58Model(models.Model):
+    read = models.BooleanField(default=False)
+    create = models.CharField(max_length=1, blank=True, null=True)

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

 
 import urllib, base64
 
-from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel
+from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel, Issue58Model
 from test_project.apps.testapp import signals
 
 class MainTests(TestCase):
         resp = self.client.get('/api/popo')
         self.assertEquals(resp.status_code, 200)
         self.assertEquals({'type': 'plain', 'field': 'a field'}, simplejson.loads(resp.content))
+
+class Issue58ModelTests(MainTests):
+    """
+    This testcase addresses #58 in django-piston where if a model
+    has one of the ['read','update','delete','create'] defined
+    it make piston crash with a `TypeError`
+    """
+    def init_delegate(self):
+        m1 = Issue58Model(read=True,create='t') 
+        m1.save()
+        m2 = Issue58Model(read=False,create='f')
+        m2.save()
+
+    def test_incoming_json(self):
+        outgoing = simplejson.dumps({ 'read': True, 'create': 'T'})
+
+        expected = """[
+    {
+        "read": true, 
+        "create": "t"
+    }, 
+    {
+        "read": false, 
+        "create": "f"
+    }
+]"""
+
+        # test GET
+        result = self.client.get('/api/issue58.json',
+                                HTTP_AUTHORIZATION=self.auth_string).content
+        self.assertEquals(result, expected)
+
+        # test POST
+        resp = self.client.post('/api/issue58.json', outgoing, content_type='application/json',
+                                HTTP_AUTHORIZATION=self.auth_string)
+            
+        self.assertEquals(resp.status_code, 201)

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

 from piston.resource import Resource
 from piston.authentication import HttpBasicAuthentication
 
-from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler
+from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler, PlainOldObjectHandler, Issue58Handler
 
 auth = HttpBasicAuthentication(realm='TestApplication')
 
 abstract = Resource(handler=AbstractHandler, authentication=auth)
 echo = Resource(handler=EchoHandler)
 popo = Resource(handler=PlainOldObjectHandler)
-
+issue58 = Resource(handler=Issue58Handler)
 
 urlpatterns = patterns('',
     url(r'^entries/$', entries),
     url(r'^entries\.(?P<emitter_format>.+)', entries),
     url(r'^entry-(?P<pk>.+)\.(?P<emitter_format>.+)', entries),
 
+    url(r'^issue58\.(?P<emitter_format>.+)$', issue58),
+
     url(r'^expressive\.(?P<emitter_format>.+)$', expressive),
 
     url(r'^abstract\.(?P<emitter_format>.+)$', abstract),