Skip to content

Commit

Permalink
Allow filtering by log level in TimelineLogLevel admin
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Oct 15, 2024
1 parent 63d98be commit 2f8ee05
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/open_inwoner/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand All @@ -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",
Expand Down

0 comments on commit 2f8ee05

Please sign in to comment.