Commits

Horst Gutmann  committed 236bfcb

Made a minimal change to utils.coerce_put_post to support file-uploads

Previously request.FILES got not populated.

  • Participants
  • Parent commits dc4d627

Comments (0)

Files changed (4)

File piston/utils.py

         request._load_post_and_files()
         request.method = "PUT"
         request.PUT = request.POST
-        del request._post
 
 class Mimer(object):
     TYPES = dict()

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

 
 from models import TestModel, ExpressiveTestModel, Comment, InheritedModel
 from forms import EchoForm
+from test_project.apps.testapp import signals
 
 
 class EntryHandler(BaseHandler):
     model = TestModel
-    allowed_methods = ['GET']
+    allowed_methods = ['GET', 'PUT', 'POST']
 
     def read(self, request, pk=None):
+        signals.entry_request_started.send(sender=self, request=request)
         if pk is not None:
             return TestModel.objects.get(pk=int(pk))
         paginator = Paginator(TestModel.objects.all(), 25)
         return paginator.page(int(request.GET.get('page', 1))).object_list
 
+    def update(self, request, pk):
+        signals.entry_request_started.send(sender=self, request=request)
+
+    def create(self, request):
+        signals.entry_request_started.send(sender=self, request=request)
+
 class ExpressiveHandler(BaseHandler):
     model = ExpressiveTestModel
     fields = ('title', 'content', ('comments', ('content',)))
         else:
             super(ExpressiveTestModel, self).create(request)
             
-            
 class AbstractHandler(BaseHandler):
     fields = ('id', 'some_other', 'some_field')
     model = InheritedModel

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

+import django.dispatch
+
+entry_request_started = django.dispatch.Signal(providing_args=['request'])

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

 import base64
 
 from test_project.apps.testapp.models import TestModel, ExpressiveTestModel, Comment, InheritedModel
+from test_project.apps.testapp import signals
 
 class MainTests(TestCase):
     def setUp(self):
         self.assertEquals(self.client.get('/api/expressive.yaml', 
             HTTP_AUTHORIZATION=self.auth_string).content, expected)
 
+class Issue36RegressionTests(MainTests):
+    """
+    This testcase addresses #36 in django-piston where request.FILES is passed
+    empty to the handler if the request.method is PUT.
+    """
+    def fetch_request(self, sender, request, *args, **kwargs):
+        self.request = request
+
+    def setUp(self):
+        super(self.__class__, self).setUp()
+        self.data = TestModel()
+        self.data.save()
+        # Register to the WSGIRequest signals to get the latest generated
+        # request object.
+        signals.entry_request_started.connect(self.fetch_request)
+
+    def tearDown(self):
+        super(self.__class__, self).tearDown()
+        self.data.delete()
+        signals.entry_request_started.disconnect(self.fetch_request)
+    
+    def test_simple(self):
+        # First try it with POST to see if it works there
+        if True:
+            fp = open(__file__, 'r')
+            try:
+                response = self.client.post('/api/entries.xml',
+                        {'file':fp}, HTTP_AUTHORIZATION=self.auth_string)
+                self.assertEquals(1, len(self.request.FILES), 'request.FILES on POST is empty when it should contain 1 file')
+            finally:
+                fp.close()
+
+        if not hasattr(self.client, 'put'):
+            import warnings
+            warnings.warn('Issue36RegressionTest partially requires Django 1.1 or newer. Skipped.')
+            return
+
+        # ... and then with PUT
+        fp = open(__file__, 'r')
+        try:
+            response = self.client.put('/api/entry-%d.xml' % self.data.pk,
+                    {'file': fp}, HTTP_AUTHORIZATION=self.auth_string)
+            self.assertEquals(1, len(self.request.FILES), 'request.FILES on PUT is empty when it should contain 1 file')
+        finally:
+            fp.close()
 
 class ValidationTest(MainTests):
     def test_basic_validation_fails(self):
         resp = self.client.get('/api/echo', data)
         self.assertEquals(resp.status_code, 200)
         self.assertEquals(data, simplejson.loads(resp.content))
-