Implement historical tracking using django-pghistory; add middleware for context capture and update model architecture

This commit is contained in:
pacnpal
2025-02-08 21:18:44 -05:00
parent 71b73522ae
commit a148d34cf9
6 changed files with 223 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
## Feature: Unified History Timeline (HTMX Integrated)
### HTMX Template Pattern
```django
{# history/partials/history_timeline.html #}
<div id="history-timeline"
hx-get="{% url 'history:timeline' content_type_id=content_type.id object_id=object.id %}"
hx-trigger="every 30s, historyUpdate from:body">
<div class="space-y-4">
{% for event in events %}
<div class="component-wrapper bg-white p-4 shadow-sm">
<div class="component-header flex items-center gap-2 mb-2">
<span class="text-sm font-medium">{{ event.pgh_label|title }}</span>
<time class="text-xs text-gray-500">{{ event.pgh_created_at|date:"M j, Y H:i" }}</time>
</div>
<div class="component-content text-sm">
{% if event.pgh_context.metadata.user %}
<div class="flex items-center gap-1">
<svg class="w-4 h-4">...</svg>
<span>{{ event.pgh_context.metadata.user }}</span>
</div>
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
```
### View Integration (Class-Based with HTMX)
```python
# history/views.py
class HistoryTimelineView(View):
def get(self, request, content_type_id, object_id):
events = ModelHistory.objects.filter(
pgh_obj_model=content_type_id,
pgh_obj_id=object_id
).order_by('-pgh_created_at')[:25]
if request.htmx:
return render(request, "history/partials/history_timeline.html", {
"events": events
})
return JsonResponse({
'history': [serialize_event(e) for e in events]
})
```
### Event Trigger Pattern
```python
# parks/signals.py
from django.dispatch import Signal
history_updated = Signal()
# In model save/delete handlers:
history_updated.send(sender=Model, instance=instance)