Skip to content

Commit

Permalink
Check for nested resources which are currently not supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Jun 16, 2012
1 parent e7e7d58 commit e6e982b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 1 addition & 4 deletions tastypie_mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ def build_schema(self):
return data

def dehydrate(self, bundle):
if not bundle.obj:
if not self.null:
raise exceptions.ApiFieldError("The document %r does not have a primary key and can not be in a ToMany context." % bundle.obj)
return []
assert bundle.obj

the_m2ms = None

Expand Down
14 changes: 11 additions & 3 deletions tastypie_mongoengine/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ def __new__(self, name, bases, attrs):

for field_name in field_names:
if field_name == 'resource_uri':
# Delete resource_uri from fields if this is mongoengine.EmbeddedDocument
if meta and issubclass(meta.object_class, mongoengine.EmbeddedDocument):
if hasattr(new_class, '_parent'):
if new_class._parent._meta.object_class and issubclass(new_class._parent._meta.object_class, mongoengine.EmbeddedDocument):
# TODO: We do not support yet nested resources
# If parent is embedded document, then also this one do not have its own resource_uri
del(new_class.base_fields[field_name])
elif new_class._meta.object_class and issubclass(new_class._meta.object_class, mongoengine.EmbeddedDocument):
# Embedded documents which are not in lists (do not have _parent) do not have their own resource_uri
del(new_class.base_fields[field_name])
if field_name in new_class.declared_fields:
continue
Expand Down Expand Up @@ -721,7 +726,10 @@ def get_resource_uri(self, bundle_or_obj):
'index': obj.pk,
}

if hasattr(obj, 'parent') and hasattr(obj.parent, 'pk'):
if hasattr(obj, 'parent'):
# pk could not exist in the case of nested resources, but we should not come here in this
# case as we should remove resource_uri from fields in MongoEngineModelDeclarativeMetaclass
# TODO: Support nested resources
kwargs['pk'] = obj.parent.pk
else:
kwargs['pk'] = self.instance.pk
Expand Down
6 changes: 5 additions & 1 deletion tests/test_project/test_app/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ def test_nested_lists_field(self):
self.assertEqual(response.status_code, 200)
response = json.loads(response.content)

#self.assertEqual(response['posts'][0]['comments'][0]['content'], 'Embedded comment 1.1')
self.assertEqual(response['posts'][0]['comments'][0]['content'], 'Embedded comment 1.1')
self.assertEqual(response['posts'][1]['comments'][0]['content'], 'Embedded comment 2.1')

response = self.c.get(board_uri + 'posts/', {'order_by': 'title'})
Expand Down Expand Up @@ -1110,6 +1110,7 @@ def test_embedded_in_embedded_doc(self):
}
}
"""

response = self.c.post(self.resourceListURI('embeddedlistinembeddeddoctest'), post, content_type='application/json')
self.assertEqual(response.status_code, 201)

Expand All @@ -1118,4 +1119,7 @@ def test_embedded_in_embedded_doc(self):
response = self.c.get(post_uri)
self.assertEqual(response.status_code, 200)
response = json.loads(response.content)

self.assertEqual(len(response['post']['comments']), 2)
self.assertTrue('resource_uri' not in response['post'])
self.assertTrue('resource_uri' not in response['post']['comments'][0])

0 comments on commit e6e982b

Please sign in to comment.