diff --git a/server/vbv_lernwelt/core/admin_utils.py b/server/vbv_lernwelt/core/admin_utils.py new file mode 100644 index 00000000..250ae60b --- /dev/null +++ b/server/vbv_lernwelt/core/admin_utils.py @@ -0,0 +1,19 @@ +import json + +from django.forms import Textarea +from django.utils.html import format_html + + +class PrettyJSONWidget(Textarea): + """ + Custom widget that can pretty print JSONField data. + """ + + def render(self, name, value, attrs=None, renderer=None): + try: + parsed = json.loads(value) + pretty_printed = json.dumps(parsed, indent=4, sort_keys=True) + except (TypeError, ValueError): + pretty_printed = value + html = super().render(name, pretty_printed, attrs=attrs, renderer=renderer) + return format_html("
{0}
", html) diff --git a/server/vbv_lernwelt/course/admin.py b/server/vbv_lernwelt/course/admin.py index c1788250..6405fa12 100644 --- a/server/vbv_lernwelt/course/admin.py +++ b/server/vbv_lernwelt/course/admin.py @@ -1,11 +1,16 @@ from django.contrib import admin +from django.db.models import JSONField +from vbv_lernwelt.core.admin_utils import PrettyJSONWidget from vbv_lernwelt.course.models import CourseSession, CourseSessionUser from vbv_lernwelt.learnpath.models import Circle @admin.register(CourseSession) class CourseSessionAdmin(admin.ModelAdmin): + formfield_overrides = { + JSONField: {"widget": PrettyJSONWidget(attrs={"rows": 16, "cols": 80})}, + } date_hierarchy = "created_at" list_display = [ "title",