MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / EmbeddedDocumentList

Class EmbeddedDocumentList

mongoengine/base/datastructures.py:192–352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

190
191
192class EmbeddedDocumentList(BaseList):
193 @classmethod
194 def __match_all(cls, embedded_doc, kwargs):
195 """Return True if a given embedded doc matches all the filter
196 kwargs. If it doesn't return False.
197 """
198 for key, expected_value in kwargs.items():
199 doc_val = getattr(embedded_doc, key)
200 if doc_val != expected_value and str(doc_val) != expected_value:
201 return False
202 return True
203
204 @classmethod
205 def __only_matches(cls, embedded_docs, kwargs):
206 """Return embedded docs that match the filter kwargs."""
207 if not kwargs:
208 return embedded_docs
209 return [doc for doc in embedded_docs if cls.__match_all(doc, kwargs)]
210
211 def filter(self, **kwargs):
212 """
213 Filters the list by only including embedded documents with the
214 given keyword arguments.
215
216 This method only supports simple comparison (e.g. .filter(name='John Doe'))
217 and does not support operators like __gte, __lte, __icontains like queryset.filter does
218
219 :param kwargs: The keyword arguments corresponding to the fields to
220 filter on. *Multiple arguments are treated as if they are ANDed
221 together.*
222 :return: A new ``EmbeddedDocumentList`` containing the matching
223 embedded documents.
224
225 Raises ``AttributeError`` if a given keyword is not a valid field for
226 the embedded document class.
227 """
228 values = self.__only_matches(self, kwargs)
229 return EmbeddedDocumentList(values, self._instance, self._name)
230
231 def exclude(self, **kwargs):
232 """
233 Filters the list by excluding embedded documents with the given
234 keyword arguments.
235
236 :param kwargs: The keyword arguments corresponding to the fields to
237 exclude on. *Multiple arguments are treated as if they are ANDed
238 together.*
239 :return: A new ``EmbeddedDocumentList`` containing the non-matching
240 embedded documents.
241
242 Raises ``AttributeError`` if a given keyword is not a valid field for
243 the embedded document class.
244 """
245 exclude = self.__only_matches(self, kwargs)
246 values = [item for item in self if item not in exclude]
247 return EmbeddedDocumentList(values, self._instance, self._name)
248
249 def count(self):

Callers 5

_reloadMethod · 0.90
__get__Method · 0.90
filterMethod · 0.85
excludeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected