Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions aggify/aggify.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
convert_match_query,
check_field_already_exists,
get_db_field,
copy_class,
)

AggifyType = TypeVar("AggifyType", bound=Callable[..., "Aggify"])
Expand Down Expand Up @@ -56,9 +57,7 @@ def __init__(self, base_model: Type[Document]):
base_model: The base model class.
"""
# Create a separate copy of the main class for safety and flexibility
self.base_model = type(
"Aggify_base_model", base_model.__bases__, dict(base_model.__dict__)
)
self.base_model = copy_class(base_model)
self.pipelines: List[Dict[str, Union[dict, Any]]] = []
self.start = None
self.stop = None
Expand Down Expand Up @@ -656,7 +655,7 @@ def lookup(
self.pipelines.append(lookup_stage)

# Add this new field to base model fields, which we can use it in the next stages.
self.base_model._fields[as_name] = from_collection # noqa
self.base_model._fields[as_name] = copy_class(from_collection) # noqa

return self

Expand Down
19 changes: 19 additions & 0 deletions aggify/utilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ def get_nested_field_model(model: CollectionType, field: str) -> CollectionType:
if model._fields[field].__dict__.get("__module__"): # noqa
return model
return model._fields[field].__dict__["document_type_obj"] # noqa


def copy_class(original_class):
"""
Copies a class, creating a new class with the same bases and attributes.

Parameters:
original_class (class): The class to be copied.

Returns:
class: A new class with the same bases and attributes as the original class.
"""
# Create a new class with the same name, bases, and attributes
copied_class = type(
"Aggify" + original_class.__name__,
original_class.__bases__,
dict(original_class.__dict__),
)
return copied_class