Commit 7af11d3f authored by petitminion's avatar petitminion
Browse files

optimising utils.py with prefect_related

parent dc19381c
Pipeline #15217 failed with stages
in 28 seconds
......@@ -81,19 +81,17 @@ 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)
tracks_id = fw_playlist.playlist_tracks.all().values_list("track_id", flat=True)
plt_tracks = fw_playlist.playlist_tracks.prefetch_related('track')
top = Element("playlist")
top.set("version", "1")
# top.append(Element.fromstring('version="1"'))
title_xspf = SubElement(top, "title")
title_xspf.text = fw_playlist.name
date_xspf = SubElement(top, "date")
date_xspf.text = str(fw_playlist.creation_date)
trackList_xspf = SubElement(top, "trackList")
for track_id in tracks_id:
track = Track.objects.get(id=track_id)
for plt_track in plt_tracks:
track = plt_track.track
track_xspf = SubElement(trackList_xspf, "track")
location_xspf = SubElement(track_xspf, "location")
location_xspf.text = "https://" + track.domain_name + track.listen_url
......
......@@ -21,10 +21,10 @@ def test_get_track_id_from_xspf(factories, tmp_path):
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)
playlist_factory = models.Playlist.objects.get()
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")
\ No newline at end of file
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