From e67edbc8553ce01300f9b46101619e591833f61c Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Thu, 19 Dec 2024 22:53:53 +0400 Subject: [PATCH] Allow media types to handle their own criteria for fetching addons --- api/api/views/audio_views.py | 3 +++ api/api/views/media_views.py | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/api/api/views/audio_views.py b/api/api/views/audio_views.py index 6056f9d42d9..fe795f61908 100644 --- a/api/api/views/audio_views.py +++ b/api/api/views/audio_views.py @@ -49,6 +49,9 @@ class AudioViewSet(MediaViewSet): def get_queryset(self): return super().get_queryset().select_related("sensitive_audio", "audioset") + def include_addons(self, serializer): + return serializer.validated_data.get("peaks") + # Extra actions async def get_image_proxy_media_info(self) -> image_proxy.MediaInfo: diff --git a/api/api/views/media_views.py b/api/api/views/media_views.py index 72728e3608b..d49f386e206 100644 --- a/api/api/views/media_views.py +++ b/api/api/views/media_views.py @@ -159,6 +159,20 @@ def _validate_source(self, source): detail=f"Invalid source '{source}'. Valid sources are: {valid_string}.", ) + def include_addons(self, serializer): + """ + Whether to include objects of the addon model when mapping hits to + objects of the media model. + + If the media type has an addon model, this method should be overridden + in the subclass to return ``True`` based on serializer input. + + :param serializer: the validated serializer instance + :return: whether to include addon model objects + """ + + return False + def get_media_results( self, request, @@ -200,8 +214,8 @@ def get_media_results( except ValueError as e: raise APIException(getattr(e, "message", str(e))) - peaks = params.validated_data.get("peaks") - results, addons = self.get_db_results(results, include_addons=peaks) + include_addons = self.include_addons(params) + results, addons = self.get_db_results(results, include_addons) serializer_context = ( search_context | self.get_serializer_context()