mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 18:11:08 -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:
@@ -2,6 +2,8 @@ from django.contrib import admin
|
||||
from django.contrib.admin import AdminSite
|
||||
from django.utils.html import format_html
|
||||
from django.urls import reverse
|
||||
from django.utils.safestring import mark_safe
|
||||
import pghistory
|
||||
from .models import EditSubmission, PhotoSubmission
|
||||
|
||||
class ModerationAdminSite(AdminSite):
|
||||
@@ -75,6 +77,33 @@ class PhotoSubmissionAdmin(admin.ModelAdmin):
|
||||
obj.reject(request.user, obj.notes)
|
||||
super().save_model(request, obj, form, change)
|
||||
|
||||
class HistoryAdmin(admin.ModelAdmin):
|
||||
"""Admin interface for viewing model history events"""
|
||||
list_display = ['pgh_label', 'pgh_created_at', 'get_object_link', 'get_context']
|
||||
list_filter = ['pgh_label', 'pgh_created_at']
|
||||
readonly_fields = ['pgh_label', 'pgh_obj_id', 'pgh_data', 'pgh_context', 'pgh_created_at']
|
||||
date_hierarchy = 'pgh_created_at'
|
||||
|
||||
def get_object_link(self, obj):
|
||||
"""Display a link to the related object if possible"""
|
||||
if obj.pgh_obj and hasattr(obj.pgh_obj, 'get_absolute_url'):
|
||||
url = obj.pgh_obj.get_absolute_url()
|
||||
return format_html('<a href="{}">{}</a>', url, str(obj.pgh_obj))
|
||||
return str(obj.pgh_obj or '')
|
||||
get_object_link.short_description = 'Object'
|
||||
|
||||
def get_context(self, obj):
|
||||
"""Format the context data nicely"""
|
||||
if not obj.pgh_context:
|
||||
return '-'
|
||||
html = ['<table>']
|
||||
for key, value in obj.pgh_context.items():
|
||||
html.append(f'<tr><th>{key}</th><td>{value}</td></tr>')
|
||||
html.append('</table>')
|
||||
return mark_safe(''.join(html))
|
||||
get_context.short_description = 'Context'
|
||||
|
||||
# Register with moderation site only
|
||||
moderation_site.register(EditSubmission, EditSubmissionAdmin)
|
||||
moderation_site.register(PhotoSubmission, PhotoSubmissionAdmin)
|
||||
moderation_site.register(pghistory.models.Event, HistoryAdmin)
|
||||
|
||||
Reference in New Issue
Block a user