fixed a bunch of things, hopefully didn't break things

This commit is contained in:
pacnpal
2024-11-05 21:51:02 +00:00
parent 2e8a725933
commit eb5d2acab5
30 changed files with 944 additions and 569 deletions

View File

@@ -1,14 +1,39 @@
# history_tracking/models.py
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from simple_history.models import HistoricalRecords
from .mixins import HistoricalChangeMixin
from typing import Any, Type, TypeVar, cast
T = TypeVar('T', bound=models.Model)
class HistoricalModel(models.Model):
"""Abstract base class for models with history tracking"""
history: HistoricalRecords = HistoricalRecords(inherit=True)
class Meta:
abstract = True
@property
def _history_model(self):
return self.history.model
def _history_model(self) -> Type[T]:
"""Get the history model class"""
return cast(Type[T], self.history.model) # type: ignore
class HistoricalSlug(models.Model):
"""Track historical slugs for models"""
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
slug = models.SlugField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('content_type', 'slug')
indexes = [
models.Index(fields=['content_type', 'object_id']),
models.Index(fields=['slug']),
]
def __str__(self) -> str:
return f"{self.content_type} - {self.object_id} - {self.slug}"