From 8f8bb19fff3a6974bdbc86060fe3f5e4539d02b8 Mon Sep 17 00:00:00 2001 From: "Hassan D. M. Sambo" Date: Mon, 15 Jul 2024 15:01:37 -0400 Subject: [PATCH] Bug fix to allow filtering by waiver types from django admin console (#4088) --- backend/audit/admin.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/audit/admin.py b/backend/audit/admin.py index 5073cc9601..3b03609331 100644 --- a/backend/audit/admin.py +++ b/backend/audit/admin.py @@ -14,6 +14,8 @@ validate_auditee_certification_json, validate_auditor_certification_json, ) +from django.contrib.admin import SimpleListFilter +from django.utils.translation import gettext_lazy as _ class SACAdmin(admin.ModelAdmin): @@ -93,6 +95,26 @@ class SubmissionEventAdmin(admin.ModelAdmin): search_fields = ("sac__report_id", "user__username") +class WaiverTypesFilter(SimpleListFilter): + title = _("Waiver Types") + parameter_name = "waiver_types" + + def lookups(self, request, model_admin): + waiver_types = set( + [ + waiver_type + for waiver in SacValidationWaiver.objects.all() + for waiver_type in waiver.waiver_types + ] + ) + return [(waiver_type, waiver_type) for waiver_type in waiver_types] + + def queryset(self, request, queryset): + if self.value(): + return queryset.filter(waiver_types__contains=[self.value()]) + return queryset + + class SacValidationWaiverAdmin(admin.ModelAdmin): form = SacValidationWaiverForm list_display = ( @@ -101,7 +123,7 @@ class SacValidationWaiverAdmin(admin.ModelAdmin): "approver_email", "requester_email", ) - list_filter = ("timestamp", "waiver_types") + list_filter = ("timestamp", WaiverTypesFilter) search_fields = ( "report_id__report_id", "approver_email",