Refactor model imports and update admin classes to use pghistory for historical tracking; replace HistoricalModel with TrackedModel in relevant models

This commit is contained in:
pacnpal
2025-02-09 11:20:40 -05:00
parent 7ecf43f1a4
commit b7f6c60682
24 changed files with 1729 additions and 137 deletions

View File

@@ -2,18 +2,7 @@ from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
import pghistory
@pghistory.track()
class HistoricalModel(models.Model):
"""
Abstract base model that provides universal history tracking via django-pghistory.
"""
class Meta:
abstract = True
def save(self, *args, **kwargs):
return super().save(*args, **kwargs)
from history_tracking.models import TrackedModel
class SlugHistory(models.Model):
"""
@@ -38,11 +27,9 @@ class SlugHistory(models.Model):
def __str__(self):
return f"Old slug '{self.old_slug}' for {self.content_object}"
@pghistory.track()
class SluggedModel(HistoricalModel):
class SluggedModel(TrackedModel):
"""
Abstract base model that provides slug functionality with history tracking.
Inherits from HistoricalModel to get universal history tracking.
"""
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
@@ -69,7 +56,6 @@ class SluggedModel(HistoricalModel):
if not self.slug:
self.slug = slugify(self.name)
# Call HistoricalModel's save to ensure history tracking
super().save(*args, **kwargs)
def get_id_field_name(self):
@@ -91,7 +77,18 @@ class SluggedModel(HistoricalModel):
# Try to get by current slug first
return cls.objects.get(slug=slug), False
except cls.DoesNotExist:
# Try to find in slug history
# Check pghistory first
history_model = cls.get_history_model()
history_entry = (
history_model.objects.filter(slug=slug)
.order_by('-pgh_created_at')
.first()
)
if history_entry:
return cls.objects.get(id=history_entry.pgh_obj_id), True
# Try to find in manual slug history as fallback
history = SlugHistory.objects.filter(
content_type=ContentType.objects.get_for_model(cls),
old_slug=slug