Commit 5555382f authored by petitminion's avatar petitminion
Browse files

add generate_xspf_from_playlist to playlist serializer

parent 5f8c01c0
Pipeline #15638 canceled with stages
in 3 minutes and 17 seconds
from xml.etree.ElementTree import Element
from rest_framework import serializers
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.music.models import Track
from funkwhale_api.music.serializers import TrackSerializer
from funkwhale_api.playlists import utils
from funkwhale_api.playlists.models import Playlist
from funkwhale_api.users.serializers import UserBasicSerializer
from . import models
......@@ -106,6 +110,22 @@ class PlaylistSerializer(serializers.ModelSerializer):
full_urls.append(url)
return full_urls
def generate_xspf_from_playlist(playlist_id):
"""
This returns a string containing playlist data in xspf format
"""
fw_playlist = Playlist.objects.get(id=playlist_id)
plt_tracks = fw_playlist.playlist_tracks.prefetch_related("track")
xspf_playlist = Element("playlist")
xspf_tracklist = utils.write_xspf_headers(
xspf_playlist, fw_playlist.name, str(fw_playlist.creation_date)
)
for plt_track in plt_tracks:
track = plt_track.track
utils.write_xspf_track_data(track, xspf_tracklist)
return utils.prettify(xspf_playlist)
class PlaylistAddManySerializer(serializers.Serializer):
tracks = serializers.PrimaryKeyRelatedField(
......
......@@ -11,7 +11,6 @@ from defusedxml import minidom
from django.core.exceptions import ObjectDoesNotExist
from funkwhale_api.music.models import Album, Artist, Track
from funkwhale_api.playlists.models import Playlist
logger = logging.getLogger(__name__)
......@@ -81,23 +80,6 @@ def get_track_id_from_xspf(xspf_file):
return track_list
def generate_xspf_from_playlist(playlist_id):
"""
This returns a string containing playlist data in xspf format
"""
fw_playlist = Playlist.objects.get(id=playlist_id)
plt_tracks = fw_playlist.playlist_tracks.prefetch_related("track")
xspf_playlist = Element("playlist")
xspf_tracklist = write_xspf_headers(
xspf_playlist, fw_playlist.name, str(fw_playlist.creation_date)
)
for plt_track in plt_tracks:
track = plt_track.track
write_xspf_track_data(track, xspf_tracklist)
return prettify(xspf_playlist)
def generate_xspf_from_tracks_ids(tracks_ids):
"""
This returns a string containing playlist data in xspf format. It's used for test purposes.
......
from defusedxml import ElementTree as etree
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.playlists import serializers
from funkwhale_api.playlists import models, serializers
from funkwhale_api.users import serializers as users_serializers
......@@ -60,3 +62,14 @@ def test_playlist_serializer(factories, to_api_date):
serializer = serializers.PlaylistSerializer(playlist)
assert serializer.data == expected
def test_generate_xspf_from_playlist(factories):
playlist = factories["playlists.PlaylistTrack"]()
playlist_factory = models.Playlist.objects.get()
xspf_test = serializers.PlaylistSerializer.generate_xspf_from_playlist(playlist.id)
tree = etree.fromstring(xspf_test)
track1 = playlist_factory.playlist_tracks.get(id=1)
track1_name = track1.track
assert playlist_factory.name == tree.findtext("./title")
assert track1_name.title == tree.findtext("./trackList/track/title")
import os
from defusedxml import ElementTree as etree
from funkwhale_api.playlists import models, utils
def test_get_track_id_from_xspf(factories, tmp_path):
track1 = factories["music.Track"]()
track2 = factories["music.Track"]()
tracks_ids = [track1.id, track2.id]
xspf_content = utils.generate_xspf_from_tracks_ids(tracks_ids)
f = open("test.xspf", "w")
f.write(xspf_content)
f.close()
xspf_file = "test.xspf"
expected = [track1.id, track2.id]
assert utils.get_track_id_from_xspf(xspf_file) == expected
os.remove("test.xspf")
def test_generate_xspf_from_playlist(factories):
playlist = factories["playlists.PlaylistTrack"]()
playlist_factory = models.Playlist.objects.get()
xspf_test = utils.generate_xspf_from_playlist(playlist.id)
tree = etree.fromstring(xspf_test)
track1 = playlist_factory.playlist_tracks.get(id=1)
track1_name = track1.track
assert playlist_factory.name == tree.findtext("./title")
assert track1_name.title == tree.findtext("./trackList/track/title")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment