Skip to content

Commit

Permalink
Basic support for PUT requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
af committed Nov 25, 2011
1 parent ea710de commit c598356
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
24 changes: 24 additions & 0 deletions djangbone/tests.py
Expand Up @@ -111,3 +111,27 @@ def test_post(self):
response = self.writable_view(request)
self.assertEqual(response.status_code, 200)
self.assert_(User.objects.get(username='post_test'))

def test_put(self):
request = self.factory.put('/users/1')
response = self.view(request, id='1')
self.assertEqual(response.status_code, 405) # "Method not supported" if no edit_form_class specified

# PUT is also not supported for collections (when no id is provided):
request = self.factory.put('/users')
response = self.writable_view(request)
self.assertEqual(response.status_code, 405)

# If no JSON in PUT body, return HTTP 400:
response = self.writable_view(request, id='1')
self.assertEqual(response.status_code, 400)

# Raise 404 if an object with the given id doesn't exist:
request = self.factory.put('/users/27', '{"username": "put_test"}', content_type='application/json')
self.assertRaises(Http404, lambda: self.writable_view(request, id='27'))

# If the object exists and an edit_form_class is supplied, it actually does something:
request = self.factory.put('/users/1', '{"username": "put_test"}', content_type='application/json')
response = self.writable_view(request, id='1')
self.assertEqual(response.status_code, 200)
self.assertEqual(User.objects.get(id=1).username, 'put_test')
42 changes: 31 additions & 11 deletions djangbone/views.py
@@ -1,6 +1,7 @@
from datetime import datetime
import json #FIXME: fallback to simplejson if json not available

from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse, Http404
from django.views.generic import View

Expand Down Expand Up @@ -60,20 +61,39 @@ def post(self, request, *args, **kwargs):
Backbone.js will send the new object's attributes as json in the request body,
so use json.loads() to parse it, rather than looking at request.POST.
"""
if self.add_form_class != None:
try:
request_dict = json.loads(request.raw_post_data)
except ValueError:
return HttpResponse('Invalid POST JSON', status=400)
form = self.add_form_class(request_dict)
if form.is_valid():
new_object = form.save()
return HttpResponse('OK') #FIXME: send json of new model
else:
if self.add_form_class == None:
return HttpResponse('POST not supported', status=405)
try:
request_dict = json.loads(request.raw_post_data)
except ValueError:
return HttpResponse('Invalid POST JSON', status=400)
form = self.add_form_class(request_dict)
if form.is_valid():
new_object = form.save()
return HttpResponse('OK') #FIXME: send json of new model

def put(self, request, *args, **kwargs):
pass
"""
Handle a PUT request by editing an existing model.
This view will only do something if BackboneView.edit_form_class is specified
by the subclass. This should be a ModelForm corresponding to the model used by
base_queryset.
"""
if self.edit_form_class == None or not kwargs.has_key('id'):
return HttpResponse('PUT not supported', status=405)
try:
# Just like with POST requests, Backbone will send the object's data as json:
request_dict = json.loads(request.raw_post_data)
instance = self.base_queryset.get(id=kwargs['id'])
except ValueError:
return HttpResponse('Invalid PUT JSON', status=400)
except ObjectDoesNotExist:
raise Http404
form = self.edit_form_class(request_dict, instance=instance)
if form.is_valid():
item = form.save()
return HttpResponse('TODO: proper PUT output')

def delete(self, request, *args, **kwargs):
pass
Expand Down

0 comments on commit c598356

Please sign in to comment.