(mcs, name, bases, attrs)
| 24 | |
| 25 | # TODO lower complexity of this method |
| 26 | def __new__(mcs, name, bases, attrs): |
| 27 | flattened_bases = mcs._get_bases(bases) |
| 28 | super_new = super().__new__ |
| 29 | |
| 30 | # If a base class just call super |
| 31 | metaclass = attrs.get("my_metaclass") |
| 32 | if metaclass and issubclass(metaclass, DocumentMetaclass): |
| 33 | return super_new(mcs, name, bases, attrs) |
| 34 | |
| 35 | attrs["_is_document"] = attrs.get("_is_document", False) |
| 36 | attrs["_cached_reference_fields"] = [] |
| 37 | |
| 38 | # EmbeddedDocuments could have meta data for inheritance |
| 39 | if "meta" in attrs: |
| 40 | attrs["_meta"] = attrs.pop("meta") |
| 41 | |
| 42 | # EmbeddedDocuments should inherit meta data |
| 43 | if "_meta" not in attrs: |
| 44 | meta = MetaDict() |
| 45 | for base in flattened_bases[::-1]: |
| 46 | # Add any mixin metadata from plain objects |
| 47 | if hasattr(base, "meta"): |
| 48 | meta.merge(base.meta) |
| 49 | elif hasattr(base, "_meta"): |
| 50 | meta.merge(base._meta) |
| 51 | attrs["_meta"] = meta |
| 52 | attrs["_meta"][ |
| 53 | "abstract" |
| 54 | ] = False # 789: EmbeddedDocument shouldn't inherit abstract |
| 55 | |
| 56 | # If allow_inheritance is True, add a "_cls" string field to the attrs |
| 57 | if attrs["_meta"].get("allow_inheritance"): |
| 58 | StringField = _import_class("StringField") |
| 59 | attrs["_cls"] = StringField() |
| 60 | |
| 61 | # Handle document Fields |
| 62 | |
| 63 | # Merge all fields from subclasses |
| 64 | doc_fields = {} |
| 65 | for base in flattened_bases[::-1]: |
| 66 | if hasattr(base, "_fields"): |
| 67 | doc_fields.update(base._fields) |
| 68 | |
| 69 | # Standard object mixin - merge in any Fields |
| 70 | if not hasattr(base, "_meta"): |
| 71 | base_fields = {} |
| 72 | for attr_name, attr_value in base.__dict__.items(): |
| 73 | if not isinstance(attr_value, BaseField): |
| 74 | continue |
| 75 | attr_value.name = attr_name |
| 76 | if not attr_value.db_field: |
| 77 | attr_value.db_field = attr_name |
| 78 | base_fields[attr_name] = attr_value |
| 79 | |
| 80 | doc_fields.update(base_fields) |
| 81 | |
| 82 | # Discover any document fields |
| 83 | field_names = {} |
nothing calls this directly
no test coverage detected