mirror of
https://git.vectorsigma.ru/public/tubearchivist.git
synced 2026-08-04 21:39:49 +00:00
add playlist sort order toggle, #171
This commit is contained in:
@@ -28,6 +28,7 @@ class PlaylistSerializer(serializers.Serializer):
|
||||
playlist_last_refresh = serializers.CharField()
|
||||
playlist_name = serializers.CharField()
|
||||
playlist_subscribed = serializers.BooleanField()
|
||||
playlist_sort_order = serializers.ChoiceField(choices=["top", "bottom"])
|
||||
playlist_thumbnail = serializers.CharField()
|
||||
playlist_type = serializers.ChoiceField(choices=["regular", "custom"])
|
||||
_index = serializers.CharField(required=False)
|
||||
@@ -68,7 +69,10 @@ class PlaylistBulkAddSerializer(serializers.Serializer):
|
||||
class PlaylistSingleUpdate(serializers.Serializer):
|
||||
"""update state of single playlist"""
|
||||
|
||||
playlist_subscribed = serializers.BooleanField()
|
||||
playlist_subscribed = serializers.BooleanField(required=False)
|
||||
playlist_sort_order = serializers.ChoiceField(
|
||||
choices=["top", "bottom"], required=False
|
||||
)
|
||||
|
||||
|
||||
class PlaylistListCustomPostSerializer(serializers.Serializer):
|
||||
|
||||
@@ -35,11 +35,17 @@ class YoutubePlaylist(YouTubeItem):
|
||||
self.get_from_es()
|
||||
if self.json_data:
|
||||
subscribed = self.json_data.get("playlist_subscribed")
|
||||
playlist_sort_order = self.json_data.get("playlist_sort_order")
|
||||
else:
|
||||
subscribed = False
|
||||
playlist_sort_order = "top"
|
||||
|
||||
playlist_items = "::1" if playlist_sort_order == "top" else "::-1"
|
||||
|
||||
if scrape or not self.json_data:
|
||||
self.get_from_youtube()
|
||||
self.get_from_youtube(
|
||||
obs_overwrite={"playlist_items": playlist_items}
|
||||
)
|
||||
if not self.youtube_meta:
|
||||
self.json_data = False
|
||||
return
|
||||
@@ -48,8 +54,13 @@ class YoutubePlaylist(YouTubeItem):
|
||||
self._ensure_channel()
|
||||
ids_found = self.get_local_vids()
|
||||
self.get_entries(ids_found)
|
||||
self.json_data["playlist_entries"] = self.all_members
|
||||
self.json_data["playlist_subscribed"] = subscribed
|
||||
self.json_data.update(
|
||||
{
|
||||
"playlist_entries": self.all_members,
|
||||
"playlist_subscribed": subscribed,
|
||||
"playlist_sort_order": playlist_sort_order,
|
||||
}
|
||||
)
|
||||
|
||||
def process_youtube_meta(self):
|
||||
"""extract relevant fields from youtube"""
|
||||
@@ -190,6 +201,15 @@ class YoutubePlaylist(YouTubeItem):
|
||||
self.get_playlist_art()
|
||||
return True
|
||||
|
||||
def change_sort_order(self, new_sort_order):
|
||||
"""update sort order of playlist"""
|
||||
playlist = YoutubePlaylist(self.youtube_id)
|
||||
playlist.build_json()
|
||||
playlist.json_data["playlist_sort_order"] = new_sort_order
|
||||
playlist.upload_to_es()
|
||||
|
||||
return playlist.json_data
|
||||
|
||||
def build_nav(self, youtube_id):
|
||||
"""find next and previous in playlist of a given youtube_id"""
|
||||
cache_root = EnvironmentSettings().get_cache_root()
|
||||
|
||||
@@ -240,9 +240,24 @@ class PlaylistApiView(ApiBaseView):
|
||||
error = ErrorResponseSerializer({"error": "playlist not found"})
|
||||
return Response(error.data, status=404)
|
||||
|
||||
subscribed = validated_data["playlist_subscribed"]
|
||||
playlist_sub = PlaylistSubscription()
|
||||
json_data = playlist_sub.change_subscribe(playlist_id, subscribed)
|
||||
subscribed = validated_data.get("playlist_subscribed")
|
||||
sort_order = validated_data.get("playlist_sort_order")
|
||||
|
||||
json_data = None
|
||||
if subscribed is not None:
|
||||
playlist_sub = PlaylistSubscription()
|
||||
json_data = playlist_sub.change_subscribe(playlist_id, subscribed)
|
||||
|
||||
if sort_order:
|
||||
json_data = YoutubePlaylist(playlist_id).change_sort_order(
|
||||
new_sort_order=sort_order
|
||||
)
|
||||
|
||||
if not json_data:
|
||||
error = ErrorResponseSerializer(
|
||||
{"error": "expect playlist_subscribed or playlist_sort_order"}
|
||||
)
|
||||
return Response(error.data, status=400)
|
||||
|
||||
response_serializer = PlaylistSerializer(json_data)
|
||||
return Response(response_serializer.data)
|
||||
|
||||
Reference in New Issue
Block a user