Skip to content

Commit

Permalink
Add playlist id to video results (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanbady authored Nov 8, 2024
1 parent e4e7b29 commit 8fb4c73
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions frontends/api/src/generated/v1/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6610,6 +6610,12 @@ export interface VideoResource {
* @memberof VideoResource
*/
video: Video
/**
* Get the playlist id(s) the video belongs to
* @type {Array<string>}
* @memberof VideoResource
*/
playlists: Array<string>
/**
*
* @type {string}
Expand Down
10 changes: 10 additions & 0 deletions learning_resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
23 changes: 23 additions & 0 deletions learning_resources/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
Expand Down
7 changes: 7 additions & 0 deletions openapi/specs/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -13376,6 +13382,7 @@ components:
- offered_by
- pace
- platform
- playlists
- position
- prices
- professional
Expand Down

0 comments on commit 8fb4c73

Please sign in to comment.