From d8b7453260cec9fc1ec0e4b5b41eded5f086b44a Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 15 Jan 2013 14:06:55 -0500 Subject: [PATCH] Test playlist importing, bugfixes for import NOTE: This reverts songs to use the ID as PK - breaks IntegerListField if not done this way. --- archiver/playlist.py | 16 ++++++++++------ archiver/song.py | 2 +- archiver/tests.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/archiver/playlist.py b/archiver/playlist.py index 4eb292c..fae7ea1 100644 --- a/archiver/playlist.py +++ b/archiver/playlist.py @@ -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 diff --git a/archiver/song.py b/archiver/song.py index e1ec7ae..935352f 100644 --- a/archiver/song.py +++ b/archiver/song.py @@ -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): diff --git a/archiver/tests.py b/archiver/tests.py index 663142e..a3adb03 100644 --- a/archiver/tests.py +++ b/archiver/tests.py @@ -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)