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

Added two list-filters to TimelineLog admin for better usability #512

Merged
merged 2 commits into from
Mar 2, 2023
Merged
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
44 changes: 43 additions & 1 deletion src/open_inwoner/utils/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from django.contrib.admin.models import ADDITION, CHANGE, DELETION
from django.contrib.contenttypes.models import ContentType
from django.urls import NoReverseMatch, reverse
from django.utils.html import escape, format_html
from django.utils.translation import gettext as _
Expand All @@ -10,6 +11,47 @@
from timeline_logger.models import TimelineLog
from timeline_logger.resources import TimelineLogResource

from open_inwoner.utils.logentry import LOG_ACTIONS


class LogActionListFilter(admin.SimpleListFilter):
title = _("Actie")
parameter_name = "log_action"

def lookups(self, request, model_admin):
return list(LOG_ACTIONS.values())

def queryset(self, request, queryset):
v = self.value()
if v:
try:
v = int(v)
except ValueError:
pass
else:
queryset = queryset.filter(extra_data__action_flag__0=v)
return queryset


class ContentTypeUsedListFilter(admin.SimpleListFilter):
title = _("content type")
parameter_name = "ct"

def lookups(self, request, model_admin):
qs = model_admin.get_queryset(request)
content_types = ContentType.objects.filter(
id__in=qs.values_list("content_type", flat=True).distinct()
)
return [("none", "None")] + [(ct.id, str(ct)) for ct in content_types]

def queryset(self, request, queryset):
v = self.value()
if v:
if v == "none":
v = None
queryset = queryset.filter(content_type=v)
return queryset


class CustomTimelineLogAdmin(ExportMixin, TimelineLogAdmin):
show_full_result_count = False
Expand All @@ -23,7 +65,7 @@ class CustomTimelineLogAdmin(ExportMixin, TimelineLogAdmin):
"get_action_flag",
"message",
]
list_filter = ["timestamp", "content_type"]
list_filter = ["timestamp", LogActionListFilter, ContentTypeUsedListFilter]
list_select_related = ["content_type"]
search_fields = [
"user__email",
Expand Down