Skip to content

Commit

Permalink
Get sources list for filter from content_provider table (#5180)
Browse files Browse the repository at this point in the history
  • Loading branch information
krysal authored Nov 25, 2024
1 parent 475b8c6 commit 5531359
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions api/api/admin/media_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
AudioDecision,
AudioDecisionThrough,
AudioReport,
ContentSource,
DeletedAudio,
DeletedImage,
Image,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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),
)

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5531359

Please sign in to comment.