mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 12:11:13 -05:00
weird header stuff
This commit is contained in:
@@ -7,14 +7,20 @@ class HistoryTrackingConfig(AppConfig):
|
||||
name = "history_tracking"
|
||||
|
||||
def ready(self):
|
||||
from django.apps import apps
|
||||
from .mixins import HistoricalChangeMixin
|
||||
from .models import Park
|
||||
|
||||
models_with_history = [Park]
|
||||
|
||||
for model in models_with_history:
|
||||
# Check if mixin is already applied
|
||||
if HistoricalChangeMixin not in model.history.model.__bases__:
|
||||
model.history.model.__bases__ = (
|
||||
HistoricalChangeMixin,
|
||||
) + model.history.model.__bases__
|
||||
# Get the Park model
|
||||
try:
|
||||
Park = apps.get_model('parks', 'Park')
|
||||
ParkArea = apps.get_model('parks', 'ParkArea')
|
||||
|
||||
# Apply mixin to historical models
|
||||
if HistoricalChangeMixin not in Park.history.model.__bases__:
|
||||
Park.history.model.__bases__ = (HistoricalChangeMixin,) + Park.history.model.__bases__
|
||||
|
||||
if HistoricalChangeMixin not in ParkArea.history.model.__bases__:
|
||||
ParkArea.history.model.__bases__ = (HistoricalChangeMixin,) + ParkArea.history.model.__bases__
|
||||
except LookupError:
|
||||
# Models might not be loaded yet
|
||||
pass
|
||||
|
||||
@@ -2,6 +2,17 @@
|
||||
|
||||
|
||||
class HistoricalChangeMixin:
|
||||
@property
|
||||
def prev_record(self):
|
||||
"""Get the previous record for this instance"""
|
||||
try:
|
||||
return type(self).objects.filter(
|
||||
history_date__lt=self.history_date,
|
||||
id=self.id
|
||||
).order_by('-history_date').first()
|
||||
except (AttributeError, TypeError):
|
||||
return None
|
||||
|
||||
@property
|
||||
def diff_against_previous(self):
|
||||
prev_record = self.prev_record
|
||||
@@ -15,9 +26,14 @@ class HistoricalChangeMixin:
|
||||
"history_id",
|
||||
"history_type",
|
||||
"history_user_id",
|
||||
"history_change_reason",
|
||||
"history_type",
|
||||
] and not field.startswith("_"):
|
||||
old_value = getattr(prev_record, field)
|
||||
new_value = getattr(self, field)
|
||||
if old_value != new_value:
|
||||
changes[field] = {"old": old_value, "new": new_value}
|
||||
try:
|
||||
old_value = getattr(prev_record, field)
|
||||
new_value = getattr(self, field)
|
||||
if old_value != new_value:
|
||||
changes[field] = {"old": old_value, "new": new_value}
|
||||
except AttributeError:
|
||||
continue
|
||||
return changes
|
||||
|
||||
@@ -4,14 +4,11 @@ from simple_history.models import HistoricalRecords
|
||||
from .mixins import HistoricalChangeMixin
|
||||
|
||||
|
||||
class Park(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
# ... other fields ...
|
||||
history = HistoricalRecords()
|
||||
class HistoricalModel(models.Model):
|
||||
"""Abstract base class for models with history tracking"""
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@property
|
||||
def _history_model(self):
|
||||
return self.history.model
|
||||
|
||||
|
||||
# Apply the mixin
|
||||
|
||||
Reference in New Issue
Block a user