You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.9 KiB
143 lines
4.9 KiB
from django.db import models
|
|
from django.utils.timezone import now
|
|
from typing import List, Set
|
|
from django.conf import settings
|
|
|
|
class BaseModel(models.Model):
|
|
creation_date = models.DateTimeField(default=now, editable=False)
|
|
last_update = models.DateTimeField(default=now)
|
|
last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def get_parent_reference(self):
|
|
"""Return a tuple: model_name, model_id"""
|
|
return None, None
|
|
|
|
def get_children_by_model(self):
|
|
"""
|
|
Returns a dictionary where:
|
|
- keys are the model names
|
|
- values are QuerySets of related objects
|
|
"""
|
|
children = self._meta.get_fields(include_parents=False, include_hidden=False)
|
|
related_objects = {}
|
|
|
|
for child in children:
|
|
if (child.one_to_many or child.one_to_one) and child.auto_created:
|
|
model_name = child.related_model.__name__
|
|
# print(f'>>> add children for {model_name}')
|
|
related_objects[model_name] = getattr(self, child.name).all()
|
|
|
|
return related_objects
|
|
|
|
def get_parents_by_model(self):
|
|
"""
|
|
Returns a dictionary where:
|
|
- keys are the model names
|
|
- values are the parent model instances
|
|
"""
|
|
parents = {}
|
|
|
|
# Get all fields including parent links
|
|
fields = self._meta.get_fields(include_parents=True, include_hidden=True)
|
|
|
|
for field in fields:
|
|
# Check if the field is a parent link (OneToOne field that points to a parent model)
|
|
if isinstance(field, models.OneToOneRel) and field.parent_link:
|
|
model_name = field.related_model.__name__
|
|
# Get the parent instance using the related name
|
|
parent_instance = getattr(self, field.get_accessor_name())
|
|
if parent_instance:
|
|
parents[model_name] = parent_instance
|
|
|
|
# Also check for direct foreign key relationships that might represent parent relationships
|
|
elif isinstance(field, models.ForeignKey):
|
|
model_name = field.related_model.__name__
|
|
parent_instance = getattr(self, field.name)
|
|
if parent_instance:
|
|
parents[model_name] = parent_instance
|
|
|
|
return parents
|
|
|
|
|
|
def get_recursive_children(self, processed_objects: Set = None) -> List:
|
|
"""
|
|
Recursively get all children objects through the hierarchy
|
|
"""
|
|
if processed_objects is None:
|
|
processed_objects = set()
|
|
|
|
# Skip if we've already processed this object to avoid infinite recursion
|
|
if self.pk in processed_objects:
|
|
return []
|
|
|
|
processed_objects.add(self.pk)
|
|
children = []
|
|
|
|
# Get immediate children
|
|
children_by_model = self.get_children_by_model()
|
|
for queryset in children_by_model.values():
|
|
for child in queryset:
|
|
children.append(child)
|
|
# Recursively get children of children
|
|
if isinstance(child, BaseModel):
|
|
children.extend(child.get_recursive_children(processed_objects))
|
|
|
|
return children
|
|
|
|
def get_recursive_parents(self, processed_objects: Set = None) -> List:
|
|
"""
|
|
Recursively get all parent objects through the hierarchy
|
|
"""
|
|
if processed_objects is None:
|
|
processed_objects = set()
|
|
|
|
# Skip if we've already processed this object to avoid infinite recursion
|
|
if self.pk in processed_objects:
|
|
return []
|
|
|
|
processed_objects.add(self.pk)
|
|
parents = []
|
|
|
|
# Get immediate parents
|
|
parents_by_model = self.get_parents_by_model()
|
|
for parent in parents_by_model.values():
|
|
parents.append(parent)
|
|
# Recursively get parents of parents
|
|
if isinstance(parent, BaseModel):
|
|
parents.extend(parent.get_recursive_parents(processed_objects))
|
|
|
|
return parents
|
|
|
|
def related_instances(self):
|
|
"""
|
|
Get all related instances (both children and parents) recursively
|
|
"""
|
|
instances = []
|
|
processed_objects = set()
|
|
instances.extend(self.get_recursive_children(processed_objects))
|
|
|
|
processed_objects = set()
|
|
instances.extend(self.get_recursive_parents(processed_objects))
|
|
|
|
return instances
|
|
|
|
# def related_instances(self):
|
|
# instances = []
|
|
# children_by_model = self.get_children_by_model()
|
|
# all_children = [item for sublist in children_by_model.values() for item in sublist]
|
|
# instances.extend(all_children)
|
|
|
|
# parents_by_model = self.get_parents_by_model()
|
|
# instances.extend(parents_by_model.values())
|
|
|
|
# return instances
|
|
|
|
|
|
class SideStoreModel(BaseModel):
|
|
store_id = models.CharField(max_length=100)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|