Skip to content

Commit 8fb4c73

Browse files
authored
Add playlist id to video results (#1802)
1 parent e4e7b29 commit 8fb4c73

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

frontends/api/src/generated/v1/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6610,6 +6610,12 @@ export interface VideoResource {
66106610
* @memberof VideoResource
66116611
*/
66126612
video: Video
6613+
/**
6614+
* Get the playlist id(s) the video belongs to
6615+
* @type {Array<string>}
6616+
* @memberof VideoResource
6617+
*/
6618+
playlists: Array<string>
66136619
/**
66146620
*
66156621
* @type {string}

learning_resources/serializers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,16 @@ class VideoResourceSerializer(LearningResourceBaseSerializer):
639639

640640
video = VideoSerializer(read_only=True)
641641

642+
playlists = serializers.SerializerMethodField()
643+
644+
def get_playlists(self, instance) -> list[str]:
645+
"""Get the playlist id(s) the video belongs to"""
646+
return list(
647+
instance.parents.filter(
648+
relation_type=constants.LearningResourceRelationTypes.PLAYLIST_VIDEOS.value
649+
).values_list("parent__id", flat=True)
650+
)
651+
642652

643653
class VideoPlaylistResourceSerializer(LearningResourceBaseSerializer):
644654
"""Serializer for video playlist resources"""

learning_resources/views_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,29 @@ def test_list_video_endpoint(client, url, params):
826826
)
827827

828828

829+
@pytest.mark.parametrize(
830+
("url", "params"),
831+
[
832+
("lr:v1:videos_api-list", ""),
833+
("lr:v1:learning_resources_api-list", "resource_type=video"),
834+
],
835+
)
836+
def test_list_video_endpoint_returns_playlists(client, url, params):
837+
"""Test video endpoint returns playlist ids"""
838+
839+
playlist = VideoPlaylistFactory.create().learning_resource
840+
videos = VideoFactory.create_batch(2)
841+
playlist.resources.set(
842+
[video.learning_resource for video in videos],
843+
through_defaults={
844+
"relation_type": LearningResourceRelationTypes.PLAYLIST_VIDEOS
845+
},
846+
)
847+
resp = client.get(f"{reverse(url)}?{params}")
848+
for item in resp.data["results"]:
849+
assert playlist.id in item["playlists"]
850+
851+
829852
@pytest.mark.parametrize(
830853
"url", ["lr:v1:videos_api-detail", "lr:v1:learning_resources_api-detail"]
831854
)

openapi/specs/v1.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13303,6 +13303,12 @@ components:
1330313303
allOf:
1330413304
- $ref: '#/components/schemas/Video'
1330513305
readOnly: true
13306+
playlists:
13307+
type: array
13308+
items:
13309+
type: string
13310+
description: Get the playlist id(s) the video belongs to
13311+
readOnly: true
1330613312
readable_id:
1330713313
type: string
1330813314
maxLength: 512
@@ -13376,6 +13382,7 @@ components:
1337613382
- offered_by
1337713383
- pace
1337813384
- platform
13385+
- playlists
1337913386
- position
1338013387
- prices
1338113388
- professional

0 commit comments

Comments
 (0)