Test playlist importing, bugfixes for import

NOTE: This reverts songs to use the ID as PK - breaks IntegerListField
if not done this way.
master
Bradlee Speice 2013-01-15 14:06:55 -05:00
parent 8125942901
commit d8b7453260
3 changed files with 53 additions and 7 deletions

View File

@ -179,6 +179,7 @@ class Playlist (models.Model):
#Figure out what format we're in
if playlist_string[0:7] == "#EXTM3U":
#Import m3u format playlist
print playlist_string
#Expected format is "#EXTINF:" followed by the song url on the next line.
line_iterator = playlist_string.split("\n").__iter__()
@ -186,16 +187,18 @@ class Playlist (models.Model):
#In case we end iteration early
try:
for line in line_iterator:
if line[0:8] != "#EXTINF:":
song_url = line.next() #Consume the next line
if line[0:8] == "#EXTINF:":
song_url = line_iterator.next() #Consume the next line
try:
song = Song.objects.get(url = song_url)
self.song_list.append(song.id)
self.append(song)
except ObjectDoesNotExist:
#The URL of our song could not be found
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist."
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist.")
continue
#Silently end processing
except StopIteration:
@ -212,11 +215,12 @@ class Playlist (models.Model):
song_url = file_line[5:]
try:
song = Song.objects.get(url = song_url)
self.song_list.append(song.id)
self.append(song)
except ObjectDoesNotExist:
#The URL of our song could not be found
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist."
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist.")
continue
return True

View File

@ -47,7 +47,7 @@ class Song (models.Model):
bit_rate = models.IntegerField(default = _default_bit_rate)
duration = models.IntegerField(default = _default_bit_rate)
echonest_song_id = models.CharField(max_length = 64, default = _default_echonest_song_id)
url = models.CharField(max_length = 255, primary_key = True)
url = models.CharField(max_length = 255)
file_hash = melodia_settings.HASH_RESULT_DB_TYPE
def populate_metadata(self, use_echonest = False):

View File

@ -81,8 +81,50 @@ class PlaylistExportTest(TestCase):
a_playlist.save()
for song in new_archive.songs.all():
song.save()
a_playlist.append(song)
a_playlist.save()
playlist_string = a_playlist.export()
class PlaylistImportTest(TestCase):
def test_playlist_import(self):
"Test that we can import a playlist."
#----------------------------------------------------------------------------
#- Re-using code from scanning and export
from archiver.archive import Archive
from archiver.playlist import Playlist
import os
from archiver.archive import Archive
from Melodia.settings import PROJECT_FOLDER
TEST_DATA_FOLDER = os.path.join(PROJECT_FOLDER, "test_data")
new_archive = Archive(root_folder = TEST_DATA_FOLDER)
#We must save the archive before we can start adding songs to it
new_archive.save()
new_archive.scan()
new_archive.save()
#Resume playlist testing code
a_playlist = Playlist()
a_playlist.name = "Testing..."
a_playlist.save()
for song in new_archive.songs.all():
song.save()
a_playlist.append(song)
a_playlist.save()
playlist_string = a_playlist.export()
#----------------------------------------------------------------------------
another_playlist = Playlist()
another_playlist.name = "Testing 2..."
another_playlist.save()
another_playlist._import(playlist_string)
print len(another_playlist.song_list)