Merge pull request #1060 from touilleMan/GenericReferenceField-choices
Fix GenericReferenceField choices parameter
This commit is contained in:
commit
9c8ceb6b4e
@ -165,26 +165,29 @@ class BaseField(object):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _validate(self, value, **kwargs):
|
def _validate_choices(self, value):
|
||||||
Document = _import_class('Document')
|
Document = _import_class('Document')
|
||||||
EmbeddedDocument = _import_class('EmbeddedDocument')
|
EmbeddedDocument = _import_class('EmbeddedDocument')
|
||||||
|
|
||||||
|
choice_list = self.choices
|
||||||
|
if isinstance(choice_list[0], (list, tuple)):
|
||||||
|
choice_list = [k for k, _ in choice_list]
|
||||||
|
|
||||||
|
# Choices which are other types of Documents
|
||||||
|
if isinstance(value, (Document, EmbeddedDocument)):
|
||||||
|
if not any(isinstance(value, c) for c in choice_list):
|
||||||
|
self.error(
|
||||||
|
'Value must be instance of %s' % unicode(choice_list)
|
||||||
|
)
|
||||||
|
# Choices which are types other than Documents
|
||||||
|
elif value not in choice_list:
|
||||||
|
self.error('Value must be one of %s' % unicode(choice_list))
|
||||||
|
|
||||||
|
|
||||||
|
def _validate(self, value, **kwargs):
|
||||||
# Check the Choices Constraint
|
# Check the Choices Constraint
|
||||||
if self.choices:
|
if self.choices:
|
||||||
|
self._validate_choices(value)
|
||||||
choice_list = self.choices
|
|
||||||
if isinstance(self.choices[0], (list, tuple)):
|
|
||||||
choice_list = [k for k, v in self.choices]
|
|
||||||
|
|
||||||
# Choices which are other types of Documents
|
|
||||||
if isinstance(value, (Document, EmbeddedDocument)):
|
|
||||||
if not any(isinstance(value, c) for c in choice_list):
|
|
||||||
self.error(
|
|
||||||
'Value must be instance of %s' % unicode(choice_list)
|
|
||||||
)
|
|
||||||
# Choices which are types other than Documents
|
|
||||||
elif value not in choice_list:
|
|
||||||
self.error('Value must be one of %s' % unicode(choice_list))
|
|
||||||
|
|
||||||
# check validation argument
|
# check validation argument
|
||||||
if self.validation is not None:
|
if self.validation is not None:
|
||||||
@ -308,7 +311,7 @@ class ComplexBaseField(BaseField):
|
|||||||
value_dict[k] = self.to_python(v)
|
value_dict[k] = self.to_python(v)
|
||||||
|
|
||||||
if is_list: # Convert back to a list
|
if is_list: # Convert back to a list
|
||||||
return [v for k, v in sorted(value_dict.items(),
|
return [v for _, v in sorted(value_dict.items(),
|
||||||
key=operator.itemgetter(0))]
|
key=operator.itemgetter(0))]
|
||||||
return value_dict
|
return value_dict
|
||||||
|
|
||||||
@ -375,7 +378,7 @@ class ComplexBaseField(BaseField):
|
|||||||
value_dict[k] = self.to_mongo(v)
|
value_dict[k] = self.to_mongo(v)
|
||||||
|
|
||||||
if is_list: # Convert back to a list
|
if is_list: # Convert back to a list
|
||||||
return [v for k, v in sorted(value_dict.items(),
|
return [v for _, v in sorted(value_dict.items(),
|
||||||
key=operator.itemgetter(0))]
|
key=operator.itemgetter(0))]
|
||||||
return value_dict
|
return value_dict
|
||||||
|
|
||||||
|
@ -1140,6 +1140,30 @@ class GenericReferenceField(BaseField):
|
|||||||
.. versionadded:: 0.3
|
.. versionadded:: 0.3
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
choices = kwargs.pop('choices', None)
|
||||||
|
super(GenericReferenceField, self).__init__(*args, **kwargs)
|
||||||
|
self.choices = []
|
||||||
|
# Keep the choices as a list of allowed Document class names
|
||||||
|
if choices:
|
||||||
|
for choice in choices:
|
||||||
|
if isinstance(choice, basestring):
|
||||||
|
self.choices.append(choice)
|
||||||
|
elif isinstance(choice, type) and issubclass(choice, Document):
|
||||||
|
self.choices.append(choice._class_name)
|
||||||
|
else:
|
||||||
|
self.error('Invalid choices provided: must be a list of'
|
||||||
|
'Document subclasses and/or basestrings')
|
||||||
|
|
||||||
|
def _validate_choices(self, value):
|
||||||
|
if isinstance(value, dict):
|
||||||
|
# If the field has not been dereferenced, it is still a dict
|
||||||
|
# of class and DBRef
|
||||||
|
value = value.get('_cls')
|
||||||
|
elif isinstance(value, Document):
|
||||||
|
value = value._class_name
|
||||||
|
super(GenericReferenceField, self)._validate_choices(value)
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
if instance is None:
|
if instance is None:
|
||||||
return self
|
return self
|
||||||
|
@ -2396,6 +2396,62 @@ class FieldTest(unittest.TestCase):
|
|||||||
bm = Bookmark.objects.first()
|
bm = Bookmark.objects.first()
|
||||||
self.assertEqual(bm.bookmark_object, post_1)
|
self.assertEqual(bm.bookmark_object, post_1)
|
||||||
|
|
||||||
|
def test_generic_reference_string_choices(self):
|
||||||
|
"""Ensure that a GenericReferenceField can handle choices as strings
|
||||||
|
"""
|
||||||
|
class Link(Document):
|
||||||
|
title = StringField()
|
||||||
|
|
||||||
|
class Post(Document):
|
||||||
|
title = StringField()
|
||||||
|
|
||||||
|
class Bookmark(Document):
|
||||||
|
bookmark_object = GenericReferenceField(choices=('Post', Link))
|
||||||
|
|
||||||
|
Link.drop_collection()
|
||||||
|
Post.drop_collection()
|
||||||
|
Bookmark.drop_collection()
|
||||||
|
|
||||||
|
link_1 = Link(title="Pitchfork")
|
||||||
|
link_1.save()
|
||||||
|
|
||||||
|
post_1 = Post(title="Behind the Scenes of the Pavement Reunion")
|
||||||
|
post_1.save()
|
||||||
|
|
||||||
|
bm = Bookmark(bookmark_object=link_1)
|
||||||
|
bm.save()
|
||||||
|
|
||||||
|
bm = Bookmark(bookmark_object=post_1)
|
||||||
|
bm.save()
|
||||||
|
|
||||||
|
bm = Bookmark(bookmark_object=bm)
|
||||||
|
self.assertRaises(ValidationError, bm.validate)
|
||||||
|
|
||||||
|
def test_generic_reference_choices_no_dereference(self):
|
||||||
|
"""Ensure that a GenericReferenceField can handle choices on
|
||||||
|
non-derefenreced (i.e. DBRef) elements
|
||||||
|
"""
|
||||||
|
class Post(Document):
|
||||||
|
title = StringField()
|
||||||
|
|
||||||
|
class Bookmark(Document):
|
||||||
|
bookmark_object = GenericReferenceField(choices=(Post, ))
|
||||||
|
other_field = StringField()
|
||||||
|
|
||||||
|
Post.drop_collection()
|
||||||
|
Bookmark.drop_collection()
|
||||||
|
|
||||||
|
post_1 = Post(title="Behind the Scenes of the Pavement Reunion")
|
||||||
|
post_1.save()
|
||||||
|
|
||||||
|
bm = Bookmark(bookmark_object=post_1)
|
||||||
|
bm.save()
|
||||||
|
|
||||||
|
bm = Bookmark.objects.get(id=bm.id)
|
||||||
|
# bookmark_object is now a DBRef
|
||||||
|
bm.other_field = 'dummy_change'
|
||||||
|
bm.save()
|
||||||
|
|
||||||
def test_generic_reference_list_choices(self):
|
def test_generic_reference_list_choices(self):
|
||||||
"""Ensure that a ListField properly dereferences generic references and
|
"""Ensure that a ListField properly dereferences generic references and
|
||||||
respects choices.
|
respects choices.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user