Add history tracking functionality using django-pghistory; implement views, templates, and middleware for event serialization and context management

This commit is contained in:
pacnpal
2025-02-09 09:52:19 -05:00
parent a148d34cf9
commit 7ecf43f1a4
14 changed files with 210 additions and 7 deletions

View File

@@ -2,6 +2,18 @@ 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)
class SlugHistory(models.Model):
"""
@@ -26,9 +38,11 @@ class SlugHistory(models.Model):
def __str__(self):
return f"Old slug '{self.old_slug}' for {self.content_object}"
class SluggedModel(models.Model):
@pghistory.track()
class SluggedModel(HistoricalModel):
"""
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)
@@ -55,6 +69,7 @@ class SluggedModel(models.Model):
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):