Skip to content

Commit

Permalink
Allow media types to handle their own criteria for fetching addons
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Dec 19, 2024
1 parent 35b39e5 commit e67edbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions api/api/views/audio_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 16 additions & 2 deletions api/api/views/media_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e67edbc

Please sign in to comment.