Permalink
Browse files

posting nested data now functions and passes test

  • Loading branch information...
1 parent 1857181 commit 97f91939cbfbefb8779680aba24b86ec3da386ed @dhatch committed Jul 6, 2011
Showing with 53 additions and 4 deletions.
  1. +3 −3 tastypie/fields.py
  2. +2 −0 tastypie/resources.py
  3. +48 −1 tests/related_resource/tests.py
View
@@ -5,7 +5,7 @@
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.utils import datetime_safe, importlib
from tastypie.bundle import Bundle
-from tastypie.exceptions import ApiFieldError, NotFound
+from tastypie.exceptions import ApiFieldError, NotFound, BadRequest
from tastypie.utils import dict_strip_unicode_keys
class NOT_PROVIDED:
@@ -568,7 +568,7 @@ def build_related_resource(self, value, request=None, resource_type=None):
# if the api is used in this way.
if not resource_type and isinstance(self.to, dict):
if self.contenttype_field:
- raise ApiFieldError("You must set the %s field when setting a GenericForeignKey in this way" % (self.contenttype_field.instance_name))
+ raise BadRequest("You must set the %s field when setting a GenericForeignKey in this way" % (self.contenttype_field.instance_name))
# Try to hydrate the data provided.
value = dict_strip_unicode_keys(value)
@@ -645,7 +645,7 @@ def hydrate(self, bundle):
# find out the class of model we're looking at from this field
related_content_type = self.contenttype_field.hydrate(bundle)
if related_content_type:
- resource_type = self.to[related_content_type.model_class()]
+ resource_type = self.to[related_content_type.obj.model_class()]
return self.build_related_resource(value, request=bundle.request,
resource_type=resource_type)
@@ -1815,6 +1815,8 @@ def __init__(self, *args, **kwargs):
from django.contrib.contenttypes.models import ContentType
self.Meta.queryset = ContentType.objects.all()
self.Meta.object_class = self.Meta.queryset.model
+ self._meta.queryset = ContentType.objects.all()
+ self._meta.object_class = self.Meta.queryset.model
super(ContentTypeResource,self).__init__(*args, **kwargs)
class Meta:
@@ -8,6 +8,8 @@
ExtraDataResource, GenericTagResource
from related_resource.api.urls import api
from related_resource.models import Category, Tag, Taggable, TaggableTag, ExtraData, GenericTag
+from django.contrib.contenttypes.models import ContentType
+from tastypie.resources import ContentTypeResource
settings.DEBUG = True
@@ -273,10 +275,55 @@ def test_post_by_uri(self):
self.assertEqual(data['content_object'],
TaggableResource().get_resource_uri(new_taggable))
+ def test_post_by_data_requires_content_type(self):
+ """Make sure 400 (BadRequest) is the response if an attempt is made to post with data
+ for the GenericForeignKey without providing a content_type
+ """
+
+ request = MockRequest()
+ request.GET = {'format': 'json'}
+ request.method = 'POST'
+ request.raw_post_data = '{"name": "Photoshop", "content_object": %s}' % '{"name": "Design"}'
+
+ resource = api.canonical_resource_for('generictag')
+ resp = resource.wrap_view('dispatch_list')(request)
+ self.assertTrue(resp.status_code, 400)
+
def test_post_by_data(self):
"""Create a new GenericTag item using a POST request.
content_type must be set on the new object and the serialized
data for the GenericForeignKey will be included in the POST
"""
-
+ new_category = Category(name="Design")
+ self.assertEqual(new_category.name, "Design")
+
+ request = MockRequest()
+ request.GET = {'format': 'json'}
+ request.method = 'POST'
+ request.raw_post_data = (
+ '{"name": "Photoshop", "content_type": "%s", "content_object": {"name": "Design"}}'
+ % (ContentTypeResource().get_resource_uri(
+ ContentType.objects.get_for_model(Category))))
+
+ resource = api.canonical_resource_for('generictag')
+
+ resp = resource.wrap_view('dispatch_list')(request)
+ print resp
+ self.assertEqual(resp.status_code, 201)
+
+ # get newly created object via headers.locaion
+ self.assertTrue(resp.has_header('location'))
+ location = resp['location']
+
+ resp = self.client.get(location, data={"format": "json"})
+ self.assertEqual(resp.status_code, 200)
+ data = json.loads(resp.content)
+ self.assertEqual(data['name'], 'Photoshop')
+ self.assertTrue(data['content_object'])
+ resp = self.client.get(data['content_object'], data={'format': 'json'})
+ self.assertEqual(resp.status_code, 200)
+ data = json.loads(resp.content)
+ self.assertEqual(data['name'], new_category.name)
+
+ # make a

0 comments on commit 97f9193

Please sign in to comment.