Merge branch 'SortedListField' of git://github.com/joshourisman/mongoengine
This commit is contained in:
commit
2b08ca7c99
@ -1,6 +1,7 @@
|
|||||||
from base import BaseField, ObjectIdField, ValidationError, get_document
|
from base import BaseField, ObjectIdField, ValidationError, get_document
|
||||||
from document import Document, EmbeddedDocument
|
from document import Document, EmbeddedDocument
|
||||||
from connection import _get_db
|
from connection import _get_db
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import pymongo
|
import pymongo
|
||||||
@ -12,7 +13,7 @@ __all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
|
|||||||
'DateTimeField', 'EmbeddedDocumentField', 'ListField', 'DictField',
|
'DateTimeField', 'EmbeddedDocumentField', 'ListField', 'DictField',
|
||||||
'ObjectIdField', 'ReferenceField', 'ValidationError',
|
'ObjectIdField', 'ReferenceField', 'ValidationError',
|
||||||
'DecimalField', 'URLField', 'GenericReferenceField',
|
'DecimalField', 'URLField', 'GenericReferenceField',
|
||||||
'BinaryField']
|
'BinaryField', 'SortedListField']
|
||||||
|
|
||||||
RECURSIVE_REFERENCE_CONSTANT = 'self'
|
RECURSIVE_REFERENCE_CONSTANT = 'self'
|
||||||
|
|
||||||
@ -310,6 +311,23 @@ class ListField(BaseField):
|
|||||||
def lookup_member(self, member_name):
|
def lookup_member(self, member_name):
|
||||||
return self.field.lookup_member(member_name)
|
return self.field.lookup_member(member_name)
|
||||||
|
|
||||||
|
class SortedListField(ListField):
|
||||||
|
"""A ListField that sorts the contents of its list before writing to
|
||||||
|
the database in order to ensure that a sorted list is always
|
||||||
|
retrieved.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_ordering = None
|
||||||
|
|
||||||
|
def __init__(self, field, **kwargs):
|
||||||
|
if 'ordering' in kwargs.keys():
|
||||||
|
self._ordering = kwargs.pop('ordering')
|
||||||
|
super(SortedListField, self).__init__(field, **kwargs)
|
||||||
|
|
||||||
|
def to_mongo(self, value):
|
||||||
|
if self._ordering is not None:
|
||||||
|
return sorted([self.field.to_mongo(item) for item in value], key=itemgetter(self._ordering))
|
||||||
|
return sorted([self.field.to_mongo(item) for item in value])
|
||||||
|
|
||||||
class DictField(BaseField):
|
class DictField(BaseField):
|
||||||
"""A dictionary field that wraps a standard Python dictionary. This is
|
"""A dictionary field that wraps a standard Python dictionary. This is
|
||||||
|
@ -218,6 +218,37 @@ class FieldTest(unittest.TestCase):
|
|||||||
post.comments = 'yay'
|
post.comments = 'yay'
|
||||||
self.assertRaises(ValidationError, post.validate)
|
self.assertRaises(ValidationError, post.validate)
|
||||||
|
|
||||||
|
def test_sorted_list_sorting(self):
|
||||||
|
"""Ensure that a sorted list field properly sorts values.
|
||||||
|
"""
|
||||||
|
class Comment(EmbeddedDocument):
|
||||||
|
order = IntField()
|
||||||
|
content = StringField()
|
||||||
|
|
||||||
|
class BlogPost(Document):
|
||||||
|
content = StringField()
|
||||||
|
comments = SortedListField(EmbeddedDocumentField(Comment), ordering='order')
|
||||||
|
tags = SortedListField(StringField())
|
||||||
|
|
||||||
|
post = BlogPost(content='Went for a walk today...')
|
||||||
|
post.save()
|
||||||
|
|
||||||
|
post.tags = ['leisure', 'fun']
|
||||||
|
post.save()
|
||||||
|
post.reload()
|
||||||
|
self.assertEqual(post.tags, ['fun', 'leisure'])
|
||||||
|
|
||||||
|
comment1 = Comment(content='Good for you', order=1)
|
||||||
|
comment2 = Comment(content='Yay.', order=0)
|
||||||
|
comments = [comment1, comment2]
|
||||||
|
post.comments = comments
|
||||||
|
post.save()
|
||||||
|
post.reload()
|
||||||
|
self.assertEqual(post.comments[0].content, comment2.content)
|
||||||
|
self.assertEqual(post.comments[1].content, comment1.content)
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
def test_dict_validation(self):
|
def test_dict_validation(self):
|
||||||
"""Ensure that dict types work as expected.
|
"""Ensure that dict types work as expected.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user