mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 03:11:11 -05:00
Refactor comments app to use mixins for comment functionality; update admin interfaces and add historical model fixes
This commit is contained in:
61
history_tracking/custom_history.py
Normal file
61
history_tracking/custom_history.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from django.db import models
|
||||
from simple_history.models import HistoricalRecords
|
||||
from django.contrib.contenttypes.fields import GenericRelation
|
||||
from django.utils.timezone import now
|
||||
|
||||
class CustomHistoricalRecords(HistoricalRecords):
|
||||
"""Custom historical records that properly handle generic relations."""
|
||||
|
||||
def copy_fields(self, model):
|
||||
"""
|
||||
Copy fields from the model to the historical record model,
|
||||
excluding GenericRelation fields.
|
||||
"""
|
||||
fields = {}
|
||||
for field in model._meta.concrete_fields:
|
||||
if not isinstance(field, GenericRelation) and field.name not in [
|
||||
'comments', 'comment_threads', 'photos', 'reviews'
|
||||
]:
|
||||
fields[field.name] = field.clone()
|
||||
return fields
|
||||
|
||||
def create_history_model(self, model, inherited):
|
||||
"""
|
||||
Override to ensure we don't create duplicate auto fields.
|
||||
"""
|
||||
attrs = {
|
||||
'__module__': model.__module__,
|
||||
'_history_excluded_fields': ['comments', 'comment_threads', 'photos', 'reviews'],
|
||||
}
|
||||
|
||||
app_module = '%s.models' % model._meta.app_label
|
||||
|
||||
if inherited:
|
||||
# inherited use models.AutoField instead of models.IntegerField
|
||||
attrs.update({
|
||||
'id': models.AutoField(primary_key=True),
|
||||
'history_id': models.AutoField(primary_key=True),
|
||||
'history_date': models.DateTimeField(default=now),
|
||||
'history_change_reason': models.CharField(max_length=100, null=True),
|
||||
'history_type': models.CharField(max_length=1, choices=(
|
||||
('+', 'Created'),
|
||||
('~', 'Changed'),
|
||||
('-', 'Deleted'),
|
||||
)),
|
||||
'history_user': models.ForeignKey(
|
||||
'accounts.User',
|
||||
null=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='+'
|
||||
),
|
||||
})
|
||||
|
||||
# Convert field to point to historical model
|
||||
fields = self.copy_fields(model)
|
||||
attrs.update(fields)
|
||||
|
||||
return type(
|
||||
str('Historical%s' % model._meta.object_name),
|
||||
(models.Model,),
|
||||
attrs
|
||||
)
|
||||
Reference in New Issue
Block a user