Fetch all references and convert to their document objects
(self, doc_type=None)
| 162 | return reference_map |
| 163 | |
| 164 | def _fetch_objects(self, doc_type=None): |
| 165 | """Fetch all references and convert to their document objects""" |
| 166 | object_map = {} |
| 167 | for collection, dbrefs in self.reference_map.items(): |
| 168 | # we use getattr instead of hasattr because hasattr swallows any exception under python2 |
| 169 | # so it could hide nasty things without raising exceptions (cfr bug #1688)) |
| 170 | ref_document_cls_exists = getattr(collection, "objects", None) is not None |
| 171 | |
| 172 | if ref_document_cls_exists: |
| 173 | col_name = collection._get_collection_name() |
| 174 | refs = [ |
| 175 | dbref for dbref in dbrefs if (col_name, dbref) not in object_map |
| 176 | ] |
| 177 | references = collection.objects.in_bulk(refs) |
| 178 | for key, doc in references.items(): |
| 179 | object_map[(col_name, key)] = doc |
| 180 | else: # Generic reference: use the refs data to convert to document |
| 181 | if isinstance(doc_type, (ListField, DictField, MapField)): |
| 182 | continue |
| 183 | |
| 184 | refs = [ |
| 185 | dbref for dbref in dbrefs if (collection, dbref) not in object_map |
| 186 | ] |
| 187 | |
| 188 | if doc_type: |
| 189 | references = doc_type._get_db()[collection].find( |
| 190 | {"_id": {"$in": refs}} |
| 191 | ) |
| 192 | for ref in references: |
| 193 | doc = doc_type._from_son(ref) |
| 194 | object_map[(collection, doc.id)] = doc |
| 195 | else: |
| 196 | references = get_db()[collection].find({"_id": {"$in": refs}}) |
| 197 | for ref in references: |
| 198 | if "_cls" in ref: |
| 199 | doc = get_document(ref["_cls"])._from_son(ref) |
| 200 | elif doc_type is None: |
| 201 | doc = get_document( |
| 202 | "".join(x.capitalize() for x in collection.split("_")) |
| 203 | )._from_son(ref) |
| 204 | else: |
| 205 | doc = doc_type._from_son(ref) |
| 206 | object_map[(collection, doc.id)] = doc |
| 207 | return object_map |
| 208 | |
| 209 | def _attach_objects(self, items, depth=0, instance=None, name=None): |
| 210 | """ |
no test coverage detected