def get_playlist_by_id(self, request): """ Returns a playlist based on the plalist id """ pl = Playlist.find_by_id(request.pid) response = PlaylistResponse(pid=pl.key.id(), name=pl.name, songs=[]) songs = Song.find_by_playlist(pl.key).fetch() for song in songs: response.songs.append(SongMessage(id=song.key.id(), spotify_id=song.spotify_id, name=song.name, vote_count=song.vote_count)) return response
def build_playlist_response(playlists): """ Builds a playlist response for the given playlists """ response = MultiplePlaylistResponse(playlists=[]) for pl in playlists: playlist = PlaylistResponse(pid=pl.key.id(), name=pl.name, songs=[]) songs = Song.find_by_playlist(pl.key).fetch() for song in songs: playlist.songs.append(SongMessage(id=song.key.id(), spotify_id=song.spotify_id, name=song.name, vote_count=song.vote_count)) response.playlists.append(playlist) return response
def test_playlist(self): owner=Account(username='******', email='*****@*****.**').put() pl = [] pl.append(Playlist(owner=owner, name='MM\'s awesome playlist')) pl[0].put() self.assertEqual(1, len(Playlist.query().fetch(2))) # test find by id found = Playlist.find_by_id(pl[0].key.id()) self.assertEqual(pl[0], found, 'Failed to find playlist based on id') # test add new playlist pl.append(Playlist.add_new_playlist(owner, 'A great playlist')) self.assertEqual(2, len(Playlist.query().fetch(3))) # test find by owner pl.append(Playlist(owner=owner, name='MM\'s other awesome playlist')) pl[2].put() self.assertEqual(3, len(Playlist.query().fetch(4))) pl_sorted = sorted(pl, key=lambda Playlist: Playlist.name) found_pl = Playlist.find_by_owner(owner).fetch(3) for i in range(len(found_pl)): self.assertEqual(pl_sorted[i], found_pl[i]) # test add song request = PlaylistRequest(pl_sorted[0].key.id(), '1892300kdkeo', 'Space Oddity') Playlist.add_song(request) self.assertEqual(1, len(pl_sorted[0].songs), 'add_song failed to add song') self.assertEqual('1892300kdkeo', pl_sorted[0].songs[0].spotify_id) self.assertEqual('Space Oddity', pl_sorted[0].songs[0].name) self.assertEqual(0, pl_sorted[0].songs[0].vote_count) # Upvote a song new_request = PlaylistRequest(pl_sorted[0].key.id(), 'asdlfjsadlfkj', 'What A Wonderful World') Playlist.add_song(new_request) songs = Song.find_by_playlist(pl_sorted[0].key) self.assertEqual(2, len(songs.fetch(4)), 'Failed to find songs by playlist id') song_entity = songs.get() Song.upvote(song_entity.key.id()) self.assertEqual(1, song_entity.vote_count, song_entity) songs_on_pl = pl_sorted[0].songs self.assertEqual(1, songs_on_pl[0].vote_count, songs_on_pl) song_entity = songs.fetch(2)[1] Song.upvote(song_entity.key.id()) Song.upvote(song_entity.key.id()) songs = Song.find_by_playlist(pl_sorted[0].key).fetch(2) self.assertEqual('What A Wonderful World', songs[0].name, song_entity) self.assertEqual(2, songs[0].vote_count, song_entity) # downvote self.assertEqual(2, song_entity.vote_count) Song.downvote(song_entity.key.id()) self.assertEqual(1, song_entity.vote_count)