def get_fields_with_instance(cls, doc_cls): if cls.__cached_fields_with_instance is None: cls.__cached_fields_with_instance = {} if doc_cls not in cls.__cached_fields_with_instance: cls.__cached_fields_with_instance[doc_cls] = get_fields( doc_cls, return_instance=True) return cls.__cached_fields_with_instance[doc_cls]
def get_field_names_for_type(cls, of_type=BaseField): """ Return field names per type including subfields The fields of derived types are also returned """ assert issubclass(of_type, BaseField) if cls.__cached_field_names_per_type is None: fields = defaultdict(list) for name, field in get_fields(cls, return_instance=True, subfields=True): fields[type(field)].append(name) for type_ in fields: fields[type_].extend( chain.from_iterable(fields[other_type] for other_type in fields if other_type != type_ and issubclass(other_type, type_))) cls.__cached_field_names_per_type = fields if of_type not in cls.__cached_field_names_per_type: names = list( chain.from_iterable(field_names for type_, field_names in cls.__cached_field_names_per_type.items() if issubclass(type_, of_type))) cls.__cached_field_names_per_type[of_type] = names return cls.__cached_field_names_per_type[of_type]
def collect_embedded_docs(doc_cls, embedded_doc_field_getter): for field, embedded_doc_field in get_fields(cls_, of_type=doc_cls, return_instance=True): embedded_doc_cls = embedded_doc_field_getter( embedded_doc_field).document_type fields.update({ '.'.join((field, subfield)): doc for subfield, doc in PropsMixin._get_fields_with_attr( embedded_doc_cls, attr).items() })
def get_all_fields_with_instance(cls): if cls.__cached_all_fields_with_instance is None: cls.__cached_all_fields_with_instance = get_fields( cls, return_instance=True, subfields=True ) return cls.__cached_all_fields_with_instance
def get_fields(cls): if cls.__cached_fields is None: cls.__cached_fields = get_fields(cls) return cls.__cached_fields