From 8657e82aeeb3e3c7f8fa8c4fb9593ecc5f89f7ed Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Fri, 3 Nov 2023 20:04:54 +0100 Subject: [PATCH] Save the data as proper JSON; add viewer to admin --- ingest/admin.py | 12 ++++++++++++ ingest/views.py | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ingest/admin.py b/ingest/admin.py index 02eb883..3ecc069 100644 --- a/ingest/admin.py +++ b/ingest/admin.py @@ -1,8 +1,20 @@ +from django.utils.html import escape, mark_safe from django.contrib import admin +import json + from .models import DecompressedEvent @admin.register(DecompressedEvent) class DecompressedEventAdmin(admin.ModelAdmin): list_display = ["timestamp", "project"] + exclude = ["data"] + + readonly_fields = [ + 'pretty_data', + ] + + def pretty_data(self, obj): + return mark_safe("
" + escape(json.dumps(json.loads(obj.data), indent=2)) + "
") + pretty_data.short_description = "Data" diff --git a/ingest/views.py b/ingest/views.py index 7ec0c30..2fb45fa 100644 --- a/ingest/views.py +++ b/ingest/views.py @@ -1,3 +1,5 @@ +import json # TODO consider faster APIs + from rest_framework import permissions, status from rest_framework.response import Response from rest_framework.views import APIView @@ -19,7 +21,10 @@ class BaseIngestAPIView(APIView): http_method_names = ["post"] def process_event(self, event_data, request, project): - DecompressedEvent.objects.create(project=project, data=event_data) + DecompressedEvent.objects.create( + project=project, + data=json.dumps(event_data), # TODO don't parse-then-print for BaseIngestion + ) class IngestEventAPIView(BaseIngestAPIView):