Permalink
Browse files

Cleaned code.

  • Loading branch information...
1 parent 573cf8e commit 5d05e4c739da29364fc2b03981b608ae66457027 @mitar mitar committed Oct 13, 2012
View
@@ -70,18 +70,6 @@ ReferenceField
person = fields.ReferenceField(to='test_project.test_app.api.resources.PersonResource', attribute='person', full=True)
...
-ReferencedListField
--------------------
-
-If you are using ``ListField`` containing a ``ReferenceField`` in
-MongoEngine document, it should be mapped to ``ReferencedListField``::
-
- from tastypie_mongoengine import fields
-
- class ReferencedListFieldTestResource(resources.MongoEngineResource):
- referencedlist = fields.ReferencedListField(of='test_project.test_app.api.resources.PersonResource', attribute='referencedlist', full=True, null=True)
- ...
-
EmbeddedDocumentField
---------------------
@@ -102,7 +90,7 @@ MongoEngine document, it should be mapped to ``EmbeddedListField``::
from tastypie_mongoengine import fields
class EmbeddedListFieldTestResource(resources.MongoEngineResource):
- embeddedlist = fields.EmbeddedListField(of='test_project.test_app.api.resources.EmbeddedPersonResource', attribute='embeddedlist', full=True)
+ embeddedlist = fields.EmbeddedListField(of='test_project.test_app.api.resources.EmbeddedPersonResource', attribute='embeddedlist', full=True, null=True)
...
``EmbeddedListField`` also exposes its embedded documents as subresources, so
@@ -111,6 +99,18 @@ above could be
``/api/v1/embeddedlistfieldtest/4fb88d7549902817fe000000/embeddedlist/0/``. You
can also manipulate subresources in the same manner as resources themselves.
+ReferencedListField
+-------------------
+
+If you are using ``ListField`` containing a ``ReferenceField`` in
+MongoEngine document, it should be mapped to ``ReferencedListField``::
+
+ from tastypie_mongoengine import fields
+
+ class ReferencedListFieldTestResource(resources.MongoEngineResource):
+ referencedlist = fields.ReferencedListField(of='test_project.test_app.api.resources.PersonResource', attribute='referencedlist', full=True, null=True)
+ ...
+
Polymorphism
============
@@ -202,8 +202,6 @@ class ReferencedListField(ApiNameMixin, fields.ToManyField):
is_m2m = False
def __init__(self, of, attribute, **kwargs):
- self._to_class_with_listresource = None
-
help_text = kwargs.pop('help_text', None)
super(ReferencedListField, self).__init__(to=of, attribute=attribute, **kwargs)
@@ -234,7 +232,11 @@ def build_schema(self):
return data
def dehydrate(self, bundle):
- assert bundle.obj
+ if not bundle.obj or not bundle.obj.pk:
+ if not self.null:
+ raise exceptions.ApiFieldError("The document %r does not have a primary key and can not be used in a ReferencedList context." % bundle.obj)
+
+ return []
@leo-naeka
leo-naeka Mar 11, 2013 Contributor

Hi @mitar,
For which reason do you check for a primary key here ?
With this behaviour, we can't use list of references on embedded documents.

@mitar
mitar Mar 29, 2013 Member

How exactly do you expect a reference to an embedded document to work? Does it even work with ReferenceField?

the_m2ms = None
@@ -254,7 +256,6 @@ def dehydrate(self, bundle):
for m2m in the_m2ms:
m2m_resource = self.get_related_resource(m2m)
m2m_bundle = tastypie_bundle.Bundle(obj=m2m, request=bundle.request)
- m2m_resource.get_resource_uri(m2m_bundle)
self.m2m_resources.append(m2m_resource)
m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource))
@@ -51,7 +51,7 @@ class EmbeddedListFieldTest(mongoengine.Document):
class ReferencedListFieldTest(mongoengine.Document):
referencedlist = mongoengine.ListField(mongoengine.ReferenceField(Person))
-
+
def save(self, *args, **kwargs):
for person in self.referencedlist:
if not getattr(person, 'id', None):
@@ -722,14 +722,18 @@ def test_embeddedlist(self):
self.assertEqual(len(response['embeddedlist']), 2)
def test_referencedlist(self):
- # Create a Person to reference
- response = self.c.post(self.resourceListURI('person'), '{"name": "Person 0"}', content_type='application/json')
+ response = self.c.get(self.resourceListURI('referencedlistfieldtest'))
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ self.assertEqual(len(response['objects']), 0)
+
+ response = self.c.post(self.resourceListURI('person'), '{"name": "Person 1"}', content_type='application/json')
self.assertEqual(response.status_code, 201)
- person_0_uri = response['location'][response['location'].index('/api'):]
+ person1_uri = response['location']
- # Testing POST
- response = self.c.post(self.resourceListURI('referencedlistfieldtest'), '{"referencedlist": ["' + person_0_uri + '", {"name": "Person 1"}, {"name": "Person 2", "optional": "Optional"}]}', content_type='application/json')
+ response = self.c.post(self.resourceListURI('referencedlistfieldtest'), '{"referencedlist": ["' + self.fullURItoAbsoluteURI(person1_uri) + '", {"name": "Person 2"}, {"name": "Person 3", "optional": "Optional"}]}', content_type='application/json')
self.assertEqual(response.status_code, 201)
mainresource_uri = response['location']
@@ -739,35 +743,40 @@ def test_referencedlist(self):
response = json.loads(response.content)
self.assertEqual(len(response['referencedlist']), 3)
- for obj in response['referencedlist']:
- self.assertEqual(obj['resource_type'], 'person')
- self.assertEqual(obj['resource_uri'], '/%s/' % '/'.join(['api', self.api_name, obj['resource_type'], obj['id']]))
+ for i, person in enumerate(response['referencedlist']):
+ self.assertEqual(person['resource_type'], 'person')
+ self.assertEqual(person['name'], 'Person %d' % (i + 1))
- person_1, person_2 = response['referencedlist'][1:]
+ self.assertEqual(response['referencedlist'][2]['optional'], 'Optional')
- # Testing PUT
- response = self.c.put(person_2['resource_uri'], '{"name": "Person 2", "optional": "Foobar PUT"}', content_type='application/json')
+ person2, person3 = response['referencedlist'][1:]
+
+ response = self.c.put(person2['resource_uri'], '{"name": "Person 2", "optional": "Foobar PUT"}', content_type='application/json')
self.assertEqual(response.status_code, 204)
- response = self.c.put(mainresource_uri, '{"referencedlist": ["' + person_0_uri + '", "' + person_1['resource_uri'] + '"]}', content_type='application/json')
+ response = self.c.get(person2['resource_uri'])
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ self.assertEqual(response['name'], 'Person 2')
+ self.assertEqual(response['optional'], 'Foobar PUT')
+
+ response = self.c.put(mainresource_uri, '{"referencedlist": ["' + person2['resource_uri'] + '", "' + person3['resource_uri'] + '"]}', content_type='application/json')
self.assertEqual(response.status_code, 204)
response = self.c.get(mainresource_uri)
self.assertEqual(response.status_code, 200)
response = json.loads(response.content)
self.assertEqual(len(response['referencedlist']), 2)
- for idx, obj in enumerate(response['referencedlist']):
- self.assertEqual(response['referencedlist'][idx]['name'], 'Person %i' % idx)
+ for i, person in enumerate(response['referencedlist']):
+ self.assertEqual(person['name'], 'Person %i' % (i + 2))
- # Checks that referenced object still exists as it should be explicitely deleted.
- response = self.c.get(person_2['resource_uri'])
+ response = self.c.get(person1_uri)
self.assertEqual(response.status_code, 200)
response = json.loads(response.content)
- self.assertEqual(response['name'], 'Person 2')
- self.assertEqual(response['optional'], 'Foobar PUT')
- self.assertEqual(response['resource_uri'], person_2['resource_uri'])
+ self.assertEqual(response['name'], 'Person 1')
def test_polymorphic_schema(self):
person_schema_uri = self.resourceListURI('person') + 'schema/'

0 comments on commit 5d05e4c

Please sign in to comment.