From 9cb713513ce70118819e4debd255e43acbb7cb91 Mon Sep 17 00:00:00 2001 From: Krystle Salazar Date: Fri, 22 Nov 2024 17:33:42 -0400 Subject: [PATCH] Get sources list for filter from content_provider table --- api/api/admin/media_report.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/api/api/admin/media_report.py b/api/api/admin/media_report.py index 0f0a8848a40..c6cc79b1869 100644 --- a/api/api/admin/media_report.py +++ b/api/api/admin/media_report.py @@ -26,6 +26,7 @@ AudioDecision, AudioDecisionThrough, AudioReport, + ContentSource, DeletedAudio, DeletedImage, Image, @@ -170,6 +171,30 @@ def _get_default_ordering(self): return [] +def get_source_filter(media_type: str): + class SourceFilter(admin.SimpleListFilter): + """ + Custom filter for source field in admin list view to prevent Django + from performing expensive `DISTINCT` query on media tables. + """ + + title = "source" + parameter_name = "source" + + def lookups(self, request, model_admin): + return ContentSource.objects.filter(media_type=media_type).values_list( + "source_identifier", "source_name" + ) + + def queryset(self, request, queryset): + value = self.value() + if value is not None: + queryset = queryset.filter(source=value) + return queryset + + return SourceFilter + + def get_pending_record_filter(media_type: str): class PendingRecordCountFilter(admin.SimpleListFilter): title = "pending record count" @@ -528,8 +553,7 @@ def has_sensitive_text(self, obj): def get_list_filter(self, request): return ( - "source", - "provider", + get_source_filter(self.media_type), get_pending_record_filter(self.media_type), ) @@ -866,10 +890,6 @@ def is_pending(self, obj): "is_pending", "media_id", # used because ``media_obj`` does not render a link ) - list_filter = ( - "reason", - ("decision", admin.EmptyFieldListFilter), # ~is_pending - ) search_fields = ("description", *_production_deferred("media_obj__identifier")) @admin.display(description="Media obj")