From b24a8203c9462a78882c40399a9e8c2ad05a473a Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 20 Jul 2021 19:06:55 +0100 Subject: [PATCH] Add object type filter to audit log report --- wagtail/admin/views/reports/audit_logging.py | 16 +++++++++++++++- wagtail/core/models/audit_log.py | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/wagtail/admin/views/reports/audit_logging.py b/wagtail/admin/views/reports/audit_logging.py index af6f18a4b9..02d5dad321 100644 --- a/wagtail/admin/views/reports/audit_logging.py +++ b/wagtail/admin/views/reports/audit_logging.py @@ -6,6 +6,7 @@ import django_filters from django import forms from django.contrib.auth import get_user_model +from django.contrib.contenttypes.models import ContentType from django.db.models import IntegerField, Value from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ @@ -25,6 +26,15 @@ def get_users_for_filter(): User = get_user_model() return User.objects.filter(pk__in=user_ids).order_by(User.USERNAME_FIELD) + +def get_content_types_for_filter(): + content_type_ids = set() + for log_model in log_action_registry.get_log_entry_models(): + content_type_ids.update(log_model.objects.all().get_content_type_ids()) + + return ContentType.objects.filter(pk__in=content_type_ids).order_by('model') + + class SiteHistoryReportFilterSet(WagtailFilterSet): action = django_filters.ChoiceFilter(choices=log_action_registry.get_choices) hide_commenting_actions = django_filters.BooleanFilter( @@ -37,6 +47,10 @@ class SiteHistoryReportFilterSet(WagtailFilterSet): user = django_filters.ModelChoiceFilter( field_name='user', queryset=lambda request: get_users_for_filter() ) + object_type = django_filters.ModelChoiceFilter( + label=_('Object type'), + field_name='content_type', queryset=lambda request: get_content_types_for_filter() + ) def filter_hide_commenting_actions(self, queryset, name, value): if value: @@ -47,7 +61,7 @@ class SiteHistoryReportFilterSet(WagtailFilterSet): class Meta: model = PageLogEntry - fields = ['label', 'action', 'user', 'timestamp', 'hide_commenting_actions'] + fields = ['object_type', 'label', 'action', 'user', 'timestamp', 'hide_commenting_actions'] class LogEntriesView(ReportView): diff --git a/wagtail/core/models/audit_log.py b/wagtail/core/models/audit_log.py index e97c6611ac..1d178c9769 100644 --- a/wagtail/core/models/audit_log.py +++ b/wagtail/core/models/audit_log.py @@ -34,6 +34,12 @@ class LogEntryQuerySet(models.QuerySet): User = get_user_model() return User.objects.filter(pk__in=self.get_user_ids()).order_by(User.USERNAME_FIELD) + def get_content_type_ids(self): + """ + Returns a set of IDs of content types with logged actions in this QuerySet + """ + return set(self.order_by().values_list('content_type_id', flat=True).distinct()) + class BaseLogEntryManager(models.Manager): def get_queryset(self):