Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Fetch all add-on instances in one DB request (#5289)
Dhruv Bhanushali4a85e1431fea
diff --git a/api/api/models/audio.py b/api/api/models/audio.pyindex 2a0e9621a..02b82263d 100644--- a/api/api/models/audio.py+++ b/api/api/models/audio.py@@ -224,19 +224,6 @@ class Audio(AudioFileMixin, AbstractMedia):def audio_set(self):return getattr(self, "audioset")- def get_waveform(self) -> list[float]:- """- Get the waveform if it exists. Return a blank list otherwise.-- :return: the waveform, if it exists; empty list otherwise- """-- try:- add_on = AudioAddOn.objects.get(audio_identifier=self.identifier)- return add_on.waveform_peaks or []- except AudioAddOn.DoesNotExist:- return []-def get_or_create_waveform(self):add_on, _ = AudioAddOn.objects.get_or_create(audio_identifier=self.identifier)diff --git a/api/api/serializers/audio_serializers.py b/api/api/serializers/audio_serializers.pyindex b65f56c89..18b552ccc 100644--- a/api/api/serializers/audio_serializers.py+++ b/api/api/serializers/audio_serializers.py@@ -179,9 +179,9 @@ class AudioSerializer(AudioHyperlinksSerializer, MediaSerializer):super().__init__(*args, **kwargs)def get_peaks(self, obj) -> list[int]:- if isinstance(obj, Hit):- obj = Audio.objects.get(identifier=obj.identifier)- return obj.get_waveform()+ audio_addon = self.context.get("addons", {}).get(obj.identifier)+ if audio_addon:+ return audio_addon.waveform_peaksdef to_representation(self, instance):# Get the original representationdiff --git a/api/api/views/audio_views.py b/api/api/views/audio_views.pyindex 0adb95f1d..fe795f619 100644--- a/api/api/views/audio_views.py+++ b/api/api/views/audio_views.py@@ -16,6 +16,7 @@ from api.docs.audio_docs import ()from api.docs.audio_docs import thumbnail as thumbnail_docsfrom api.models import Audio+from api.models.audio import AudioAddOnfrom api.serializers.audio_serializers import (AudioReportRequestSerializer,AudioSearchRequestSerializer,@@ -38,6 +39,7 @@ class AudioViewSet(MediaViewSet):"""Viewset for all endpoints pertaining to audio."""model_class = Audio+ addon_model_class = AudioAddOnmedia_type = AUDIO_TYPEquery_serializer_class = AudioSearchRequestSerializerdefault_index = settings.MEDIA_INDEX_MAPPING[AUDIO_TYPE]@@ -47,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 actionsasync 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.pyindex 50e651ee0..d49f386e2 100644--- a/api/api/views/media_views.py+++ b/api/api/views/media_views.py@@ -14,6 +14,7 @@ from api.constants.media_types import MediaTypefrom api.controllers import search_controllerfrom api.controllers.elasticsearch.related import related_mediafrom api.models import ContentSource+from api.models.base import OpenLedgerModelfrom api.models.media import AbstractMediafrom api.serializers import media_serializersfrom api.serializers.source_serializers import SourceSerializer@@ -51,6 +52,7 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):# Populate these in the corresponding subclassmodel_class: type[AbstractMedia] = None+ addon_model_class: type[OpenLedgerModel] = Nonemedia_type: MediaType | None = Nonequery_serializer_class = Nonedefault_index = None@@ -97,7 +99,11 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):req_serializer.is_valid(raise_exception=True)return req_serializer- def get_db_results(self, results):+ def get_db_results(+ self,+ results,+ include_addons=False,+ ) -> tuple[list[AbstractMedia], list[OpenLedgerModel]]:"""Map ES hits to ORM model instances.@@ -107,6 +113,7 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):which is both unique and indexed, so it's quite performant.:param results: the list of ES hits+ :param include_addons: whether to include add-ons with results:return: the corresponding list of ORM model instances"""@@ -121,7 +128,12 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):for result, hit in zip(results, hits):result.fields_matched = getattr(hit.meta, "highlight", None)- return results+ if include_addons and self.addon_model_class:+ addons = list(self.addon_model_class.objects.filter(pk__in=identifiers))+ else:+ addons = []++ return (results, addons)# Standard actions@@ -147,6 +159,20 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):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,@@ -188,9 +214,13 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):except ValueError as e:raise APIException(getattr(e, "message", str(e)))- serializer_context = search_context | self.get_serializer_context()-- results = self.get_db_results(results)+ include_addons = self.include_addons(params)+ results, addons = self.get_db_results(results, include_addons)+ serializer_context = (+ search_context+ | self.get_serializer_context()+ | {"addons": {addon.audio_identifier: addon for addon in addons}}+ )serializer = self.get_serializer(results, many=True, context=serializer_context)return self.get_paginated_response(serializer.data)@@ -231,7 +261,7 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):serializer_context = self.get_serializer_context()- results = self.get_db_results(results)+ results, _ = self.get_db_results(results)serializer = self.get_serializer(results, many=True, context=serializer_context)return self.get_paginated_response(serializer.data)diff --git a/api/test/integration/test_dead_link_filter.py b/api/test/integration/test_dead_link_filter.pyindex ec7a36efa..cd876a0a5 100644--- a/api/test/integration/test_dead_link_filter.py+++ b/api/test/integration/test_dead_link_filter.py@@ -31,6 +31,10 @@ def empty_validation_cache(monkeypatch):_MAKE_HEAD_REQUESTS_MODULE_PATH = "api.utils.check_dead_links._make_head_requests"+def _mock_get_db_results(results, include_addons=False):+ return (results, [])++def _patch_make_head_requests():def _make_head_requests(urls, *args, **kwargs):responses = []@@ -67,7 +71,7 @@ def test_dead_link_filtering(mocked_map, api_client):with patch("api.views.image_views.ImageViewSet.get_db_results") as mock_get_db_result:- mock_get_db_result.side_effect = lambda value: value+ mock_get_db_result.side_effect = _mock_get_db_resultsres_with_dead_links = api_client.get(path,query_params | {"filter_dead": False},@@ -121,7 +125,7 @@ def test_dead_link_filtering_all_dead_links(with patch("api.views.image_views.ImageViewSet.get_db_results") as mock_get_db_result:- mock_get_db_result.side_effect = lambda value: value+ mock_get_db_result.side_effect = _mock_get_db_resultswith patch_link_validation_dead_for_count(page_size / DEAD_LINK_RATIO):response = api_client.get(path,diff --git a/api/test/unit/models/test_audio.py b/api/test/unit/models/test_audio.pyindex 3372c6b0e..80f30bf1b 100644--- a/api/test/unit/models/test_audio.py+++ b/api/test/unit/models/test_audio.py@@ -41,28 +41,3 @@ def test_audio_waveform_caches(generate_peaks_mock, audio_fixture):audio_fixture.delete()assert AudioAddOn.objects.count() == 1--[email protected]("api.models.audio.AudioAddOn.objects.get")-def test_audio_waveform_sent_when_present(get_mock, audio_fixture):- # When ``AudioAddOn.waveform_peaks`` exists, waveform is filled- peaks = [0, 0.25, 0.5, 0.25, 0.1]- get_mock.return_value = mock.Mock(waveform_peaks=peaks)- assert audio_fixture.get_waveform() == peaks--[email protected]("api.models.audio.AudioAddOn.objects.get")-def test_audio_waveform_blank_when_absent(get_mock, audio_fixture):- # When ``AudioAddOn`` does not exist, waveform is blank- get_mock.side_effect = AudioAddOn.DoesNotExist()- assert audio_fixture.get_waveform() == []--[email protected]("api.models.audio.AudioAddOn.objects.get")-def test_audio_waveform_blank_when_none(get_mock, audio_fixture):- # When ``AudioAddOn.waveform_peaks`` is None, waveform is blank- get_mock.return_value = mock.Mock(waveform_peaks=None)- assert audio_fixture.get_waveform() == []diff --git a/api/test/unit/views/test_audio_views.py b/api/test/unit/views/test_audio_views.pynew file mode 100644index 000000000..ef689b30d--- /dev/null+++ b/api/test/unit/views/test_audio_views.py@@ -0,0 +1,39 @@+from unittest.mock import MagicMock, patch++import pytest+import pytest_django.asserts++from test.factory.models import AudioFactory++[email protected]("peaks, query_count", [(True, 2), (False, 1)])+def test_peaks_param_determines_addons(api_client, peaks, query_count):+ num_results = 20++ # Since controller returns a list of ``Hit``s, not model instances, we must+ # set the ``meta`` param on each of them to match the shape of ``Hit``.+ results = AudioFactory.create_batch(size=num_results)+ for result in results:+ result.meta = None++ controller_ret = (+ results,+ 1, # num_pages+ num_results,+ {}, # search_context+ )+ with (+ patch(+ "api.views.media_views.search_controller",+ query_media=MagicMock(return_value=controller_ret),+ ),+ patch(+ "api.serializers.media_serializers.search_controller",+ get_sources=MagicMock(return_value={}),+ ),+ pytest_django.asserts.assertNumQueries(query_count),+ ):+ res = api_client.get(f"/v1/audio/?peaks={peaks}")++ assert res.status_code == 200