validate playlist custom action

This commit is contained in:
Simon 2024-02-17 15:30:11 +01:00
parent c91a31c9e3
commit 4a8e04c0fc
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 14 additions and 6 deletions

View File

@ -525,6 +525,7 @@ class PlaylistApiView(ApiBaseView):
search_base = "ta_playlist/_doc/"
permission_classes = [AdminWriteOnly]
valid_custom_actions = ["create", "remove", "up", "down", "top", "bottom"]
def get(self, request, playlist_id):
# pylint: disable=unused-argument
@ -534,16 +535,23 @@ class PlaylistApiView(ApiBaseView):
def post(self, request, playlist_id):
"""post to custom playlist to add a video to list"""
playlist = YoutubePlaylist(playlist_id)
if not playlist.is_custom_playlist():
message = f"playlist with ID {playlist_id} is not custom"
return Response({"message": message}, status=400)
action = request.data.get("action")
if action not in self.valid_custom_actions:
message = f"invalid action: {action}"
return Response({"message": message}, status=400)
video_id = request.data.get("video_id")
if action == "create":
YoutubePlaylist(playlist_id).add_video_to_playlist(video_id)
playlist.add_video_to_playlist(video_id)
else:
YoutubePlaylist(playlist_id).move_video(
video_id,
action,
UserConfig(request.user.id).get_value("hide_watched"),
)
hide = UserConfig(request.user.id).get_value("hide_watched")
playlist.move_video(video_id, action, hide_watched=hide)
return Response({"success": True}, status=status.HTTP_201_CREATED)
def delete(self, request, playlist_id):