mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 10:11:09 -05:00
Add history tracking functionality using django-pghistory; implement views, templates, and middleware for event serialization and context management
This commit is contained in:
14
core/middleware.py
Normal file
14
core/middleware.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import pghistory
|
||||
|
||||
def setup_pghistory_context():
|
||||
"""
|
||||
Set up pghistory context middleware to track request information.
|
||||
This function configures what contextual information is stored
|
||||
with each history record.
|
||||
"""
|
||||
pghistory.context(lambda request: {
|
||||
'user': str(request.user) if request.user.is_authenticated else None,
|
||||
'ip': request.META.get('REMOTE_ADDR'),
|
||||
'user_agent': request.META.get('HTTP_USER_AGENT'),
|
||||
'session_key': request.session.session_key
|
||||
})
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user