diff --git a/frontends/api/src/generated/v1/api.ts b/frontends/api/src/generated/v1/api.ts index fef8b9c57f..99e09952af 100644 --- a/frontends/api/src/generated/v1/api.ts +++ b/frontends/api/src/generated/v1/api.ts @@ -6610,6 +6610,12 @@ export interface VideoResource { * @memberof VideoResource */ video: Video + /** + * Get the playlist id(s) the video belongs to + * @type {Array} + * @memberof VideoResource + */ + playlists: Array /** * * @type {string} diff --git a/learning_resources/serializers.py b/learning_resources/serializers.py index e04e3fc7c7..a3ca7b3750 100644 --- a/learning_resources/serializers.py +++ b/learning_resources/serializers.py @@ -639,6 +639,16 @@ class VideoResourceSerializer(LearningResourceBaseSerializer): video = VideoSerializer(read_only=True) + playlists = serializers.SerializerMethodField() + + def get_playlists(self, instance) -> list[str]: + """Get the playlist id(s) the video belongs to""" + return list( + instance.parents.filter( + relation_type=constants.LearningResourceRelationTypes.PLAYLIST_VIDEOS.value + ).values_list("parent__id", flat=True) + ) + class VideoPlaylistResourceSerializer(LearningResourceBaseSerializer): """Serializer for video playlist resources""" diff --git a/learning_resources/views_test.py b/learning_resources/views_test.py index e6bed815ab..9dbeccc57f 100644 --- a/learning_resources/views_test.py +++ b/learning_resources/views_test.py @@ -826,6 +826,29 @@ def test_list_video_endpoint(client, url, params): ) +@pytest.mark.parametrize( + ("url", "params"), + [ + ("lr:v1:videos_api-list", ""), + ("lr:v1:learning_resources_api-list", "resource_type=video"), + ], +) +def test_list_video_endpoint_returns_playlists(client, url, params): + """Test video endpoint returns playlist ids""" + + playlist = VideoPlaylistFactory.create().learning_resource + videos = VideoFactory.create_batch(2) + playlist.resources.set( + [video.learning_resource for video in videos], + through_defaults={ + "relation_type": LearningResourceRelationTypes.PLAYLIST_VIDEOS + }, + ) + resp = client.get(f"{reverse(url)}?{params}") + for item in resp.data["results"]: + assert playlist.id in item["playlists"] + + @pytest.mark.parametrize( "url", ["lr:v1:videos_api-detail", "lr:v1:learning_resources_api-detail"] ) diff --git a/openapi/specs/v1.yaml b/openapi/specs/v1.yaml index dd0650931c..120ae561ee 100644 --- a/openapi/specs/v1.yaml +++ b/openapi/specs/v1.yaml @@ -13303,6 +13303,12 @@ components: allOf: - $ref: '#/components/schemas/Video' readOnly: true + playlists: + type: array + items: + type: string + description: Get the playlist id(s) the video belongs to + readOnly: true readable_id: type: string maxLength: 512 @@ -13376,6 +13382,7 @@ components: - offered_by - pace - platform + - playlists - position - prices - professional