mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 04:31:09 -05:00
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from django.views import View
|
|
from django.shortcuts import render
|
|
from django.http import JsonResponse
|
|
from django.contrib.contenttypes.models import ContentType
|
|
import pghistory
|
|
|
|
def serialize_event(event):
|
|
"""Serialize a history event for JSON response"""
|
|
return {
|
|
'label': event.pgh_label,
|
|
'created_at': event.pgh_created_at.isoformat(),
|
|
'context': event.pgh_context,
|
|
'data': event.pgh_data,
|
|
}
|
|
|
|
class HistoryTimelineView(View):
|
|
"""View for displaying object history timeline"""
|
|
|
|
def get(self, request, content_type_id, object_id):
|
|
# Get content type and object
|
|
content_type = ContentType.objects.get_for_id(content_type_id)
|
|
obj = content_type.get_object_for_this_type(id=object_id)
|
|
|
|
# Get history events
|
|
events = pghistory.models.Event.objects.filter(
|
|
pgh_obj_model=content_type.model_class(),
|
|
pgh_obj_id=object_id
|
|
).order_by('-pgh_created_at')[:25]
|
|
|
|
context = {
|
|
'events': events,
|
|
'content_type': content_type,
|
|
'object': obj,
|
|
}
|
|
|
|
if request.htmx:
|
|
return render(request, "history/partials/history_timeline.html", context)
|
|
|
|
return JsonResponse({
|
|
'history': [serialize_event(e) for e in events]
|
|
}) |