From 2f8ee0579d3b665d480161a05a2686e365ad7c82 Mon Sep 17 00:00:00 2001 From: Sidney Richards Date: Tue, 15 Oct 2024 11:59:18 +0200 Subject: [PATCH] Allow filtering by log level in TimelineLogLevel admin --- src/open_inwoner/utils/admin.py | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/open_inwoner/utils/admin.py b/src/open_inwoner/utils/admin.py index 92b5fb9d6b..b744a8a375 100644 --- a/src/open_inwoner/utils/admin.py +++ b/src/open_inwoner/utils/admin.py @@ -4,6 +4,7 @@ from django.contrib import admin from django.contrib.admin.models import ADDITION, CHANGE, DELETION from django.contrib.contenttypes.models import ContentType +from django.db.models import Q from django.shortcuts import get_object_or_404, redirect from django.urls import NoReverseMatch, path, reverse from django.utils.html import escape, format_html @@ -62,6 +63,32 @@ def queryset(self, request, queryset): return queryset +class TimelineLogLevelFilter(admin.SimpleListFilter): + title = _("log level") + parameter_name = "log_level" + + def lookups(self, request, model_admin): + return [(v, v) for v in logging.getLevelNamesMapping().keys()] + [ + ("unknown", _("Unknown")) + ] + + def queryset(self, request, queryset): + if not (value := self.value()): + return queryset + + if value == "unknown": + return queryset.filter( + ~Q(extra_data__has_key="log_level") + | Q(extra_data__log_level__isnull=True) + ) + + try: + log_level = logging.getLevelNamesMapping()[value] + return queryset.filter(extra_data__log_level=log_level) + except KeyError: + return queryset + + class CustomTimelineLogAdmin(ExportMixin, TimelineLogAdmin): show_full_result_count = False fields = ["content_type", "timestamp", "extra_data", "user"] @@ -74,7 +101,12 @@ class CustomTimelineLogAdmin(ExportMixin, TimelineLogAdmin): "get_action_flag", "message", ] - list_filter = ["timestamp", LogActionListFilter, ContentTypeUsedListFilter] + list_filter = [ + "timestamp", + TimelineLogLevelFilter, + LogActionListFilter, + ContentTypeUsedListFilter, + ] list_select_related = ["content_type"] search_fields = [ "user__email",