def to_representation(self, value): assert isinstance(value, (Document, DBRef)) if isinstance(value, Document): doc_id = value.id doc_cls = value.__class__.__name__ if isinstance(value, DBRef): # hard case doc_id = value.id doc_collection = value.collection class_match = [k for k, v in _document_registry.items() if v._get_collection_name() == doc_collection] if len(class_match) != 1: self.fail('unmapped_collection', collection=doc_collection) doc_cls = class_match[0] return {'_cls': doc_cls, '_id': self.pk_field.to_representation(doc_id)}
def get_model_or_document(app_label, model): if has_rel_db(): return get_model(app_label, model, only_installed=False) else: # mongoengine's document registry is case sensitive # while all models are stored in lowercase in the # content types. So we can't use get_document. model = str(model).lower() possible_docs = [v for k, v in _document_registry.items() if k.lower() == model] if len(possible_docs) == 1: return possible_docs[0] if len(possible_docs) > 1: for doc in possible_docs: module = sys.modules[doc.__module__] doc_app_label = model_module.__name__.split('.')[-2] if doc_app_label.lower() == app_label.lower(): return doc return None
def build_modcol(self): """ Models and collections """ r = [] # Models r += [{ "id": m._meta.db_table, "label": "%s.%s" % (m._meta.app_label, m.__name__), "table": m._meta.db_table, } for m in apps.get_models()] # Collections r += [{ "id": c._get_collection_name(), "label": "%s.%s" % (c.__module__.split(".")[1], n), "collection": c._get_collection_name(), } for n, c in _document_registry.items() if c._get_collection_name()] return list(sorted(r, key=lambda x: x["label"]))
def get_model_or_document(app_label, model): if has_rel_db(): return get_model(app_label, model, only_installed=False) else: # mongoengine's document registry is case sensitive # while all models are stored in lowercase in the # content types. So we can't use get_document. model = str(model).lower() possible_docs = [ v for k, v in _document_registry.items() if k.lower() == model ] if len(possible_docs) == 1: return possible_docs[0] if len(possible_docs) > 1: for doc in possible_docs: module = sys.modules[doc.__module__] doc_app_label = module.__name__.split('.')[-2] if doc_app_label.lower() == app_label.lower(): return doc return None