Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable log level filtering on the TimelineLogAdmin page #1445

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ django-solo==2.2.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-timeline-logger==3.0.0
django-timeline-logger==4.0.0
# via -r requirements/base.in
django-timezone-field==6.1.0
# via django-celery-beat
Expand Down
2 changes: 1 addition & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ django-solo==2.2.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-timeline-logger==3.0.0
django-timeline-logger==4.0.0
# via
# -c requirements/base.txt
# -r requirements/base.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ django-solo==2.2.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-timeline-logger==3.0.0
django-timeline-logger==4.0.0
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
Expand Down
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
Loading