2012-12-26 18:37:14 -05:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from song import Song
|
|
|
|
from listfield import IntegerListField
|
|
|
|
|
|
|
|
"""
|
|
|
|
Playlist model
|
2013-01-09 20:48:28 -05:00
|
|
|
Each playlist is a high-level ordering of songs. There really isn't much to a playlist - just its name, and the songs inside it.
|
|
|
|
However, we need to have a way to guarantee song order, in addition to re-ordering. A ManyToMany field can't do this.
|
|
|
|
As such, a custom IntegerListField is implemented - it takes a python list of ints, converts it to a text field in the DB,
|
|
|
|
and then back to a python list. This way, we can guarantee order, and have a song appear multiple times.
|
|
|
|
The IntegerListField itself uses the ID of each song as the int in a list. For example, a list of:
|
|
|
|
[1, 3, 5, 17]
|
|
|
|
|
|
|
|
Means that the playlist is made up of four songs. The order of the playlist is the song with index 1, 3, 5, and 17.
|
|
|
|
Additionally, the ManyToMany field is included to make sure we don't use the global Songs manager - it just seems hackish.
|
2012-12-26 18:37:14 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
class Playlist (models.Model):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
The Playlist class defines the playlist model and its operations.
|
2013-01-09 21:03:37 -05:00
|
|
|
Currently supported are add, move, and remove operations, as well as exporting to
|
2013-01-10 09:01:33 -05:00
|
|
|
multiple formats.
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
name = models.CharField(max_length = 255)
|
2013-01-09 21:03:37 -05:00
|
|
|
song_list = IntegerListField()
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
#This is a bit of a backup field, since technically the song PK's are in
|
|
|
|
#the song_order field. However, it might be useful to just get a reference
|
|
|
|
#to the songs in this playlist. Additionally, it's kind of hackish to reference
|
|
|
|
#the global Song manager, rather than something that is specific for this playlist.
|
|
|
|
songs = models.ManyToManyField(Song)
|
|
|
|
|
|
|
|
def _populate_songs(self):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
Make sure that the 'songs' relation is up-to-date.
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
#This operation works by getting the ID's for all songs currently in the playlist,
|
|
|
|
#calculates what we need to add, what we need to remove, and then does it.
|
2013-01-09 21:03:37 -05:00
|
|
|
#As much work as is possible is done on the python side to avoid the DB at all costs.
|
2012-12-26 18:37:14 -05:00
|
|
|
current_song_ids = [song.id for song in self.songs.all()]
|
|
|
|
current_song_ids_set = set(current_song_ids)
|
|
|
|
|
2013-01-09 21:03:37 -05:00
|
|
|
new_song_ids_set = set(self.song_list)
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
remove_set = current_song_ids_set.difference(new_song_ids_set)
|
|
|
|
add_set = new_song_ids_set.difference(current_song_ids_set)
|
|
|
|
|
|
|
|
for song_id in remove_set:
|
|
|
|
song = self.songs.get(id = song_id)
|
|
|
|
song.remove()
|
|
|
|
|
|
|
|
for song_id in add_set:
|
|
|
|
song = Song.objects.get(id = song_id) #Using the global Songs manager is unavoidable for this one
|
|
|
|
self.songs.add(song)
|
|
|
|
|
|
|
|
def insert(self, position, new_song):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
Insert a new song into the playlist at a specific position.
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
if not isinstance(new_song, Song):
|
|
|
|
#Not given a song reference, raise an error
|
|
|
|
raise ValidationError("Not given a song reference to insert.")
|
|
|
|
|
|
|
|
self.songs.add(new_song)
|
|
|
|
|
2013-01-09 21:03:37 -05:00
|
|
|
self.song_list.insert(position, new_song.id)
|
2012-12-26 18:37:14 -05:00
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
self._populate_songs()
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
def append(self, new_song):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
Add a new song to the end of the playlist.
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
if not isinstance(new_song, Song):
|
|
|
|
#Not given a song reference, raise an error
|
|
|
|
raise ValidationError("Not given a song reference to insert.")
|
|
|
|
|
|
|
|
self.songs.add(new_song)
|
|
|
|
|
2013-01-09 21:03:37 -05:00
|
|
|
self.song_list.append(new_song.id)
|
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
self._populate_songs()
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
def move(self, original_position, new_position):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
Move a song from one position to another
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
if original_position == new_position:
|
|
|
|
return
|
|
|
|
|
2013-01-09 21:03:37 -05:00
|
|
|
song_id = self.song_list[original_position]
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
#This is actually a bit more complicated than it first appears, since the index we're moving to may actually change
|
|
|
|
#when we remove an item.
|
|
|
|
if new_position < original_position:
|
2013-01-09 21:03:37 -05:00
|
|
|
del self.song_list[original_position]
|
|
|
|
self.song_list.insert(new_position, song_id)
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
else:
|
2013-01-09 21:03:37 -05:00
|
|
|
del self.song_list[original_position]
|
|
|
|
self.song_list.insert(new_position - 1, song_id) #Account for the list indices shifting down.
|
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
self._populate_songs()
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
def remove(self, position):
|
2013-01-09 21:03:37 -05:00
|
|
|
if position > len(self.song_list):
|
2012-12-26 18:37:14 -05:00
|
|
|
return False
|
|
|
|
|
2013-01-09 21:03:37 -05:00
|
|
|
del self.song_list[position]
|
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
self._populate_songs()
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
def export(self, playlist_type = "m3u"):
|
2013-01-10 09:01:33 -05:00
|
|
|
"""
|
|
|
|
Export this internal playlist to a file format.
|
|
|
|
Supported formats:
|
|
|
|
-pls
|
|
|
|
-m3u
|
|
|
|
Return value is a string containing the playlist -
|
|
|
|
you can write it to file as you deem necessary.
|
|
|
|
"""
|
2012-12-26 18:37:14 -05:00
|
|
|
#Default m3u playlist type, support others as I build them.
|
2013-01-10 09:01:33 -05:00
|
|
|
playlist_string = ""
|
2012-12-26 18:37:14 -05:00
|
|
|
|
2013-01-09 20:48:28 -05:00
|
|
|
if playlist_type == "pls":
|
2013-01-10 09:01:33 -05:00
|
|
|
#Playlist header
|
|
|
|
playlist_string += "[playlist]"
|
|
|
|
|
|
|
|
#Playlist body
|
|
|
|
for index, song_id in enumerate(self.song_list):
|
|
|
|
song = self.songs.get(id = song_id)
|
|
|
|
|
|
|
|
playlist_string += "File" + str(index + 1) + "=" + song.url + "\n"
|
|
|
|
playlist_string += "Title" + str(index + 1) + "=" + song.title + "\n"
|
2013-01-10 09:38:23 -05:00
|
|
|
playlist_string += "Length" + str(index + 1) + "=" + str(song.duration) + "\n"
|
2013-01-10 09:01:33 -05:00
|
|
|
|
|
|
|
#Playlist footer
|
|
|
|
playlist_string += "NumberOfEntries=" + str(len(self.song_list))
|
|
|
|
playlist_string += "Version=2"
|
2012-12-26 18:37:14 -05:00
|
|
|
|
|
|
|
else:
|
2013-01-10 09:01:33 -05:00
|
|
|
#Export m3u, default option if nothing else is recognized
|
|
|
|
|
|
|
|
#Playlist header
|
|
|
|
playlist_string += "#EXTM3U" + "\n"
|
|
|
|
|
|
|
|
#Playlist body
|
|
|
|
for song_id in self.song_list:
|
|
|
|
song = self.songs.get(id = song_id)
|
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
playlist_string += "#EXTINF:" + str(song.duration) + "," + song.artist + " - " + song.title + "\n"
|
2013-01-10 09:01:33 -05:00
|
|
|
playlist_string += song.url + "\n"
|
|
|
|
|
|
|
|
#Playlist footer
|
2013-01-09 21:03:37 -05:00
|
|
|
|
2013-01-10 09:38:23 -05:00
|
|
|
return playlist_string
|
|
|
|
|
2013-01-10 09:01:33 -05:00
|
|
|
def _import(self, playlist_string = None):
|
|
|
|
"""
|
|
|
|
Import and convert a playlist into native DB format.
|
|
|
|
As a side note - the _import() name is used since python doesn't let
|
|
|
|
you name a function import().
|
|
|
|
"""
|
2013-01-10 09:38:23 -05:00
|
|
|
if not playlist_string:
|
|
|
|
#Make sure we have a string to operate on.
|
|
|
|
return False
|