def delete(self): """ Delete all the models in the query set. Models delegate to this :return: """ # Search the model, and all embedded models for ImageFields image_paths = [] docs_by_cls = defaultdict(list) for doc in self.values(): cls_name = doc.get('_cls', self._model._mongometa.object_name) docs_by_cls[cls_name].append(doc) while len(docs_by_cls) > 0: cls_name, docs = docs_by_cls.popitem() model = get_document(cls_name) # Find all the Image fields on this model fields = model._mongometa.get_fields() image_fields = [ field.attname for field in fields if isinstance(field, ImageField) ] # Get the values of those fields from the documents image_paths.extend(doc[name] for name in image_fields for doc in docs if name in doc) # Look in embedded documents as well for field in fields: if isinstance(field, pymodm.fields.EmbeddedDocumentField): for doc in docs: if field.attname in doc: inner_doc = doc[field.attname] cls_name = inner_doc.get( '_cls', field.related_model._mongometa.object_name) docs_by_cls[cls_name].append(inner_doc) elif isinstance(field, pymodm.fields.EmbeddedDocumentListField): for doc in docs: if field.attname in doc: for inner_doc in doc[field.attname]: cls_name = inner_doc.get( '_cls', field.related_model._mongometa.object_name) docs_by_cls[cls_name].append(inner_doc) # Delete all the images at those paths if len(image_paths) > 0: with arvet.database.image_manager.get() as image_manager: for path in image_paths: image_manager.remove_image(path) # Proceed with the rest of the delete super(ImageQuerySet, self).delete()
def related_model(self): if not self.__related_model: MongoModelBase = _import('pymodm.base.models.MongoModelBase') if isinstance(self.__model, string_types): self.__related_model = get_document(self.__model) # 'issubclass' complains if first argument is not a class. elif (isinstance(self.__model, type) and issubclass(self.__model, MongoModelBase)): self.__related_model = self.__model return self.__related_model
def from_document(cls, document): """Construct an instance of this class from the given document. :parameters: - `document`: A Python dictionary describing a MongoDB document. Keys within the document must be named according to each model field's `mongo_name` attribute, rather than the field's Python name. """ dct = validate_mapping('document', document) cls_name = dct.get('_cls') if cls_name is not None: cls = get_document(cls_name) inst = cls() inst._set_attributes(dct) return inst
def from_document(cls, document): """Construct an instance of this class from the given document. :parameters: - `document`: A Python dictionary describing a MongoDB document. Keys within the document must be named according to each model field's `mongo_name` attribute, rather than the field's Python name. """ dct = validate_mapping('document', document) doc_cls = cls cls_name = dct.get('_cls') if cls_name is not None: doc_cls = get_document(cls_name) if not issubclass(doc_cls, cls): raise TypeError('A document\'s _cls field must be ' 'a subclass of the %s, but %s is not.' % (doc_cls, cls)) inst = doc_cls() inst._set_attributes(dct) return inst