cleanup + nicer EmbeddedDocumentList.__match_all and __only_matches

This commit is contained in:
Stefan Wojcik 2016-12-08 18:59:25 -05:00
parent 9a32ff4c42
commit f1f999a570
3 changed files with 14 additions and 12 deletions

View File

@ -211,18 +211,22 @@ class BaseList(list):
class EmbeddedDocumentList(BaseList): class EmbeddedDocumentList(BaseList):
@classmethod @classmethod
def __match_all(cls, i, kwargs): def __match_all(cls, embedded_doc, kwargs):
items = kwargs.items() """Return True if a given embedded doc matches all the filter
return all([ kwargs. If it doesn't return False
getattr(i, k) == v or six.text_type(getattr(i, k)) == v """
for k, v in items for key, expected_value in kwargs.items():
]) doc_val = getattr(embedded_doc, key)
if doc_val != expected_value and six.text_type(doc_val) != expected_value:
return False
return True
@classmethod @classmethod
def __only_matches(cls, obj, kwargs): def __only_matches(cls, embedded_docs, kwargs):
"""Return embedded docs that match the filter kwargs."""
if not kwargs: if not kwargs:
return obj return embedded_docs
return filter(lambda i: cls.__match_all(i, kwargs), obj) return [doc for doc in embedded_docs if cls.__match_all(doc, kwargs)]
def __init__(self, list_items, instance, name): def __init__(self, list_items, instance, name):
super(EmbeddedDocumentList, self).__init__(list_items, instance, name) super(EmbeddedDocumentList, self).__init__(list_items, instance, name)

View File

@ -433,7 +433,7 @@ class Document(BaseDocument):
"""Recursively save any references and generic references on the """Recursively save any references and generic references on the
document. document.
""" """
_refs = kwargs.get('_refs', []) or [] _refs = kwargs.get('_refs') or []
ReferenceField = _import_class('ReferenceField') ReferenceField = _import_class('ReferenceField')
GenericReferenceField = _import_class('GenericReferenceField') GenericReferenceField = _import_class('GenericReferenceField')

View File

@ -506,8 +506,6 @@ class FileTest(unittest.TestCase):
test_file = TestFile.objects.first() test_file = TestFile.objects.first()
test_file.the_file = six.b('HELLO, WORLD!') test_file.the_file = six.b('HELLO, WORLD!')
print('HERE!!!')
print(test_file.the_file)
test_file.save() test_file.save()
test_file = TestFile.objects.first() test_file = TestFile.objects.first()