Refactor comments app to use mixins for comment functionality; update admin interfaces and add historical model fixes

This commit is contained in:
pacnpal
2025-02-08 16:33:55 -05:00
parent 75f5b07129
commit 03f9df4bab
21 changed files with 548 additions and 280 deletions

View File

@@ -1,10 +1,13 @@
from django.db import models
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.db.models.fields.related import RelatedField
from django.contrib.auth import get_user_model
from simple_history.models import HistoricalRecords
from .mixins import HistoricalChangeMixin
from typing import Any, Type, TypeVar, cast, Optional
from .historical_fields import HistoricalFieldsMixin
from typing import Any, Type, TypeVar, cast, Optional, List
from django.db.models import QuerySet
from django.core.exceptions import ValidationError
from django.utils import timezone
@@ -13,13 +16,28 @@ T = TypeVar('T', bound=models.Model)
User = get_user_model()
class HistoricalModel(models.Model):
class HistoricalModel(models.Model, HistoricalFieldsMixin):
"""Abstract base class for models with history tracking"""
id = models.BigAutoField(primary_key=True)
history: HistoricalRecords = HistoricalRecords(
@classmethod
def __init_subclass__(cls, **kwargs):
"""Initialize subclass with proper configuration."""
super().__init_subclass__(**kwargs)
# Mark historical models
if cls.__name__.startswith('Historical'):
cls._is_historical_model = True
# Remove any inherited generic relations
for field in list(cls._meta.private_fields):
if isinstance(field, GenericRelation):
cls._meta.private_fields.remove(field)
else:
cls._is_historical_model = False
history = HistoricalRecords(
inherit=True,
bases=(HistoricalChangeMixin,),
excluded_fields=['comments', 'photos', 'reviews'] # Exclude all generic relations
bases=[HistoricalChangeMixin],
excluded_fields=['comments', 'comment_threads', 'photos', 'reviews'],
use_base_model_db=True # Use base model's db
)
class Meta: