示例#1
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    curr_on_playlist = []
    for song in playlist.songs:
        curr_on_playlist.append(song.id)

    form.song.choices = db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all()

    if form.validate_on_submit():
        song_id = form.song.data

        if PlaylistSong.check_for_duplicate(playlist_id, song_id) != None:
            flash(f"{playlist.name} already includes song!", 'danger')
        else:
            new_playlist_song = PlaylistSong(playlist_id=playlist_id,
                                             song_id=song_id)
            db.session.add(new_playlist_song)
            db.session.commit()
            flash(f"{playlist.name} has been updated!", 'info')
            return redirect(f'/playlists/{playlist_id}')

    else:
        return render_template("add_song_to_playlist.html",
                               playlist=playlist,
                               form=form)
示例#2
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    # BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist

    curr_on_playlist = [song for song in playlist.songs]
    form.song.choices = [[song.id, song.title] for song in Song.query.all()
                         if song not in curr_on_playlist]

    if form.validate_on_submit():

        # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
        song = Song.query.get(form.song.data)
        new_playlistsong = PlaylistSong(playlist_id=playlist.id,
                                        song_id=song.id)
        db.session.add(new_playlistsong)
        db.session.commit()
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#3
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    # BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK

    # THE SOLUTION TO THIS IS IN A HINT IN THE ASSESSMENT INSTRUCTIONS

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist

    curr_on_playlist = Song.query.join(
        PlaylistSong, PlaylistSong.song_id == Song.id).filter(
            PlaylistSong.playlist_id == playlist_id).all()
    curr_on_playlist = [song.id for song in curr_on_playlist]
    choices = Song.query.filter(Song.id.notin_(curr_on_playlist)).all()
    choices = [(song.id, song.title) for song in choices]
    form.song.choices = choices

    if form.validate_on_submit():

        song_id = form.song.data
        for id in song_id:
            db.session.add(PlaylistSong(song_id=id, playlist_id=playlist_id))
        db.session.commit()
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#4
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    # BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK

    # THE SOLUTION TO THIS IS IN A HINT IN THE ASSESSMENT INSTRUCTIONS

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist

    curr_on_playlist = [s.id for s in playlist.songs]
    form.song.choices = (db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all())

    if form.validate_on_submit():
        playlist_song = PlaylistSong(song_id=form.song.data,
                                     playlist_id=playlist_id)
        db.session.add(playlist_song)
        # song = Song.query.get(form.song.data)
        # playlist.songs.append(song)
        db.session.commit()

        # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK

        return redirect(f"/playlists/{playlist_id}")

    return render_template(
        "add_song_to_playlist.html",
        playlist=playlist,
        form=form,
    )
示例#5
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist
    curr_on_playlist = [ song.id for song in playlist.songs ]
    form.song.choices = [
        (song.id, song.title) for song in Song.query.filter(
            Song.id.notin_(curr_on_playlist)
        ).all()
    ]

    if len(form.song.choices) == 0:
        flash("No songs are available to add to this playlist.")
        return redirect(f"/playlists/{playlist_id}")

    if form.validate_on_submit():
        playlist_song = PlaylistSong(song_id=form.song.data, playlist_id=playlist_id)
          
        db.session.add(playlist_song)
        db.session.commit()

        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
        playlist = playlist,
        form = form
    )
示例#6
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist

    curr_on_playlist = [s.id for s in playlist.songs]
    form.song.choices = (db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all())

    if form.validate_on_submit():

        # This is one way you could do this ...
        playlist_song = PlaylistSong(song_id=form.song.data,
                                     playlist_id=playlist_id)
        db.session.add(playlist_song)

        # Here's another way you could that is slightly more ORM-ish:
        #
        # song = Song.query.get(form.song.data)
        # playlist.songs.append(song)

        # Either way, you have to commit:
        db.session.commit()

        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#7
0
def save_song():
    """Save song to database."""

    song_uri = request.args.get("songData")
    song_name = request.args.get("songTitle")
    song_img = request.args.get("songImg")
    playlist_no = int(request.args.get("playlist"))

    service = ''
    if 'spotify' in song_uri:
        service = 'spotify'
    elif '/' in song_uri:
        service = 'mixcloud'
    else:
        service = 'youtube'

    song = Song(service_id=song_uri,
                song_name=song_name,
                song_img=song_img,
                service=service)

    db.session.add(song)
    db.session.commit()

    playlist_song = PlaylistSong(playlist_id=playlist_no, song_id=song.song_id)

    db.session.add(playlist_song)
    db.session.commit()

    return jsonify('Success.')
示例#8
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    # BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
    songs = Song.query.all()

    # THE SOLUTION TO THIS IS IN A HINT IN THE ASSESSMENT INSTRUCTIONS

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist

    curr_on_playlist = [
        playlists_songs.song_id for playlists_songs in playlist.playlists_songs
    ]
    form.song.choices = [(song.id, song.title) for song in songs
                         if song.id not in curr_on_playlist]

    if form.validate_on_submit():
        playlist.playlists_songs.append(
            PlaylistSong(playlist_id=playlist_id, song_id=form.song.data))
        db.session.commit()

        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#9
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    # BONUS - ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
    # THE SOLUTION TO THIS IS IN A HINT IN THE ASSESSMENT INSTRUCTIONS
    # I didn't need it :)

    form = NewSongForPlaylistForm()

    playlist = Playlist.query.get_or_404(playlist_id)

    # Restrict form to songs not already on this playlist
    curr_on_playlist = playlist.songs
    all_songs = Song.query.all()
    choices = [(song.id, song.title) for song in all_songs
               if song not in curr_on_playlist]

    form.song.choices = choices

    if form.validate_on_submit():

        song_id = form.song.data
        playlist_song = PlaylistSong(playlist_id=playlist.id, song_id=song_id)

        db.session.add(playlist_song)
        db.session.commit()

        flash("song added")
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#10
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Stores current playlist song id's
    curr_on_playlist = [song.id for song in playlist.songs]

    # Restrict form to songs not already on this playlist
    song_choices = db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all()

    # Display choices on form
    form.song.choices = (song_choices)

    if form.validate_on_submit():

        song = form.song.data
        playlist_song = PlaylistSong(playlist_id=playlist_id, song_id=song)

        db.session.add(playlist_song)
        db.session.commit()
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#11
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist
    playlist = Playlist.query.get_or_404(playlist_id)
    curr_on_playlist = [song.id for song in playlist.songs]
    form.song.choices = (db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all())

    if form.validate_on_submit():
        PlaylistSong.map_song_to_pl(form, playlist_id)
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#12
0
def add_song_to_playlist(playlist_id):
    """Add a song to a playlist and redirect to playlist page."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    curr_on_playlist = [s.id for s in playlist.songs]
    form.song.choices = (db.session.query(Song.id, Song.title).filter(
        Song.id.notin_(curr_on_playlist)).all())

    if form.validate_on_submit():
        song = form.song.data
        new_playlist_song = PlaylistSong(song_id=song, playlist_id=playlist_id)
        db.session.add(new_playlist_song)
        db.session.commit()
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#13
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    curr_on_playlist = [s.id for s in playlist.songs]
    
    form = NewSongForPlaylistForm()

    # todo: review the query and try to implement it with the use if JOIN 
    form.song.choices = (db.session.query(Song.id, Song.title).filter(Song.id.notin_(curr_on_playlist)).all())
    
    if form.validate_on_submit():
        playlist_song = PlaylistSong(song_id=form.song.data, playlist_id=playlist_id)
                                  
        db.session.add(playlist_song)
        db.session.commit()

        return redirect(url_for("show_playlist", playlist_id=playlist_id))

    return render_template("add_song_to_playlist.html", playlist=playlist, form=form)
示例#14
0
def add_song_to_playlist(playlist_id):
    """Add a playlist and redirect to list."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist
    curr_on_playlist = [s.id for s in playlist.songs()]
    form.song.choices = db.session.query(Song.id, Song.title).filter(Song.id.notin_(curr_on_playlist)).all()
    
    if form.validate_on_submit():
        
        new_playlist_song = PlaylistSong(playlist_id=playlist_id, song_id=form.song.data)
        db.session.add(new_playlist_song)
        db.session.commit()
        flash(f"Song added to {playlist.name}", "success")
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                             playlist=playlist,
                             form=form)
示例#15
0
def add_song_to_playlist(playlist_id):
    """Add a song to the playlist and redirect to playlist."""

    playlist = Playlist.query.get_or_404(playlist_id)
    form = NewSongForPlaylistForm()

    # Restrict form to songs not already on this playlist
    curr_on_playlist = [s.id for s in playlist.songs]

    songs_to_be_selected = Song.query.filter(
        Song.id.notin_(curr_on_playlist)).all()
    form.song.choices = [(song.id, song.title)
                         for song in songs_to_be_selected]
    # form.song.choices = (db.session.query(Song.id, Song.title)
    #                      .filter(Song.id.notin_(curr_on_playlist))
    #                      .all())

    if form.validate_on_submit():
        song_id = form.song.data
        new_playlist_song = PlaylistSong(playlist_id=playlist_id,
                                         song_id=song_id)

        db.session.add(new_playlist_song)

        # Here's another way that is slightly more ORM-ish:
        # song = Song.query.get(form.song.data)
        # playlist.songs.append(song)

        db.session.commit()

        flash(f"Song added to '{playlist.name}'")
        return redirect(f"/playlists/{playlist_id}")

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
示例#16
0
s1 = Song(title="My Song", artist="ME")
s2 = Song(title="My Other Song", artist="ME")
s3 = Song(title="Paper Planes", artist="MIA")
s4 = Song(title="Colors", artist="RED")
s5 = Song(title="Angry Birds", artist="Tweetz")
s6 = Song(title="Thunderstruck", artist="ACDC")
s7 = Song(title="Intergalactic", artist="Beastie Boys")
s8 = Song(title="Sandstorm", artist="Darud")
s9 = Song(title="Thizz Dance", artist="Mac Dre")
songs = [s1, s2, s3, s4, s5, s6, s7, s8, s9]

db.session.add_all(songs)
db.session.commit()

ps1 = PlaylistSong(playlist_id=1, song_id=1)
ps2 = PlaylistSong(playlist_id=1, song_id=2)
ps3 = PlaylistSong(playlist_id=2, song_id=2)
ps4 = PlaylistSong(playlist_id=3, song_id=3)
ps5 = PlaylistSong(playlist_id=1, song_id=4)
ps6 = PlaylistSong(playlist_id=2, song_id=5)
ps7 = PlaylistSong(playlist_id=1, song_id=5)
ps8 = PlaylistSong(playlist_id=3, song_id=6)
ps9 = PlaylistSong(playlist_id=4, song_id=6)
ps10 = PlaylistSong(playlist_id=5, song_id=7)
ps11 = PlaylistSong(playlist_id=5, song_id=9)
ps12 = PlaylistSong(playlist_id=6, song_id=8)

song_playlists = [ps1, ps2, ps3, ps4, ps5,
                  ps6, ps7, ps8, ps9, ps10, ps11, ps12]
示例#17
0
def _handle_command(command, albumid=None, songid=None, force_play=False):

	global player_state
	global playlist
	global shuffle
	global mute

	logger.debug("Playlist: %s, Shuffle: %s" %(playlist, shuffle))

	next_song = None
	next_playlistsong = None

	if command == "surprise":

		shuffle = True
		playlist = False			
		next_song = _get_next_song()

	elif command == "playlist":

		shuffle = False
		playlist = True
		next_playlistsong = _get_next_playlistsong()

	elif command == "next":
		
		if playlist:
			next_playlistsong = _get_next_playlistsong()
		elif shuffle:
			next_song = _get_next_song()

	elif command == "shuffle":

		shuffle = not shuffle

	elif command == "play":
		
		if songid is not None:
			logger.debug("Fetching song %s" % songid)
			next_song = Song.objects.get(pk=songid)
		elif playlist:
			next_playlistsong = PlaylistSong.objects.filter(is_current=True).first()
		elif shuffle:
			next_song = _get_next_song()

	elif command == "enqueue":

		if albumid is not None:
			
			album = Album.objects.get(pk=albumid)

			for song in album.song_set.all().order_by('name'):
				playlistsong = PlaylistSong(song=song)
				playlistsong.save()
				if next_playlistsong is None:
					next_playlistsong = playlistsong

		elif songid is not None:

			song = Song.objects.get(pk=songid)

			playlistsong = PlaylistSong(song=song)
			playlistsong.save()
			if next_playlistsong is None:
				next_playlistsong = playlistsong

		# - 'soft' play to start playback if it isn't already
		force_play = False

	elif command == "pause":
		if player_state == PLAYER_STATE_PAUSED:
			player_state = PLAYER_STATE_PLAYING
		else:
			player_state = PLAYER_STATE_PAUSED
		p.pause()

	elif command == "stop":
		if player_state == PLAYER_STATE_STOPPED:
			player_state = PLAYER_STATE_PLAYING
		else:
			player_state = PLAYER_STATE_STOPPED

		p.stop()

	elif command == "mute":
		mute = not mute
		p.mute()

	if next_song or next_playlistsong:

		if next_song is None and next_playlistsong is not None:
			next_song = next_playlistsong.song

		logger.debug("Playing song %s" % next_song.name)
		played = _play(next_song, force_play)
		
		if played:
			_show_player_status(next_song)
			if next_playlistsong is not None:
				_set_current_song(next_playlistsong)	

	logger.debug("handled command %s" % command)
	_show_player_state()
示例#18
0
from models import db, connect_db, Playlist, Song, PlaylistSong
from app import app

db.drop_all()
db.create_all()

s1 = Song(title="I See Fire", artist="EdSheran")
s2 = Song(title="Give Me Love", artist="EdSheran")
s3 = Song(title="Bloodstream", artist="EdSheran")
s4 = Song(title="Big Girls Dont Cry", artist="Sia")
s5 = Song(title="Thunderstorm", artist="Sia")
s6 = Song(title="Counting stars", artist="OneRepublick")

p1 = Playlist(name="Ed's", description="EdSheran songs")
p2 = Playlist(name="Sia's", description="Sia songs")

ps1 = PlaylistSong(playlist_id=1, song_id=1)
ps2 = PlaylistSong(playlist_id=1, song_id=2)
ps3 = PlaylistSong(playlist_id=1, song_id=3)

db.session.add_all([s1, s2, s3, s4, s5, s6])
db.session.commit()

db.session.add_all([p1, p2])
db.session.commit()

db.session.add_all([ps1, ps2, ps3])
db.session.commit()
示例#19
0
def _handle_command(command, albumid=None, songid=None, force_play=False):

    global player_state
    global playlist
    global shuffle
    global mute

    logger.debug("Playlist: %s, Shuffle: %s" % (playlist, shuffle))

    next_song = None
    next_playlistsong = None

    if command == "surprise":

        shuffle = True
        playlist = False
        next_song = _get_next_song()

    elif command == "playlist":

        shuffle = False
        playlist = True
        next_playlistsong = _get_next_playlistsong()

    elif command == "next":

        if playlist:
            next_playlistsong = _get_next_playlistsong()
        elif shuffle:
            next_song = _get_next_song()

    elif command == "shuffle":

        shuffle = not shuffle

    elif command == "play":

        if songid is not None:
            logger.debug("Fetching song %s" % songid)
            next_song = Song.objects.get(pk=songid)
        elif playlist:
            next_playlistsong = PlaylistSong.objects.filter(
                is_current=True).first()
        elif shuffle:
            next_song = _get_next_song()

    elif command == "enqueue":

        if albumid is not None:

            album = Album.objects.get(pk=albumid)

            for song in album.song_set.all().order_by('name'):
                playlistsong = PlaylistSong(song=song)
                playlistsong.save()
                if next_playlistsong is None:
                    next_playlistsong = playlistsong

        elif songid is not None:

            song = Song.objects.get(pk=songid)

            playlistsong = PlaylistSong(song=song)
            playlistsong.save()
            if next_playlistsong is None:
                next_playlistsong = playlistsong

        # - 'soft' play to start playback if it isn't already
        force_play = False

    elif command == "pause":
        if player_state == PLAYER_STATE_PAUSED:
            player_state = PLAYER_STATE_PLAYING
        else:
            player_state = PLAYER_STATE_PAUSED
        p.pause()

    elif command == "stop":
        if player_state == PLAYER_STATE_STOPPED:
            player_state = PLAYER_STATE_PLAYING
        else:
            player_state = PLAYER_STATE_STOPPED

        p.stop()

    elif command == "mute":
        mute = not mute
        p.mute()

    if next_song or next_playlistsong:

        if next_song is None and next_playlistsong is not None:
            next_song = next_playlistsong.song

        logger.debug("Playing song %s" % next_song.name)
        played = _play(next_song, force_play)

        if played:
            _show_player_status(next_song)
            if next_playlistsong is not None:
                _set_current_song(next_playlistsong)

    logger.debug("handled command %s" % command)
    _show_player_state()
示例#20
0
# Commit--otherwise, this never gets saved!
db.session.commit()

# Repeat for posts
s1 = Song(title='TestSong1', artist='TestArtist1')
s2 = Song(title='TestSong2', artist='TestArtist1')
s3 = Song(title='TestSong3', artist='TestArtist2')

db.session.add(s1)
db.session.add(s2)
db.session.add(s3)

db.session.commit()

# Repeat for PostTags
ps1 = PlaylistSong(playlist_id=1, song_id=1)
ps2 = PlaylistSong(playlist_id=2, song_id=2)
ps3 = PlaylistSong(playlist_id=3, song_id=3)
ps4 = PlaylistSong(playlist_id=3, song_id=1)
ps5 = PlaylistSong(playlist_id=2, song_id=3)
ps6 = PlaylistSong(playlist_id=1, song_id=3)

db.session.add(ps1)
db.session.add(ps2)
db.session.add(ps3)
db.session.add(ps4)
db.session.add(ps5)
db.session.add(ps6)

db.session.commit()
示例#21
0
songs = [
    Song(title='song1', artist='artist1', genre='Rap'),
    Song(title='song2', artist='artist1', genre='Country'),
    Song(title='song3', artist='artist2', genre='Rock')
]

playlists = [
    Playlist(name='Playlist One',
             description='A Playlist with a number of songs'),
    Playlist(name='Playlist Two',
             description='Another Playlist with a number of songs')
]

playlistsongs = [
    PlaylistSong(song_id=1, playlist_id=1),
    PlaylistSong(song_id=2, playlist_id=1),
    PlaylistSong(song_id=3, playlist_id=1),
    PlaylistSong(song_id=1, playlist_id=2),
    PlaylistSong(song_id=2, playlist_id=2),
    PlaylistSong(song_id=3, playlist_id=2)
]

db.session.add_all(songs)
db.session.commit()

db.session.add_all(playlists)
db.session.commit()

db.session.add_all(playlistsongs)
db.session.commit()
示例#22
0
from models import db, Song, Playlist, PlaylistSong
from app import app

db.drop_all()
db.create_all()

song1 = Song(title='dancing in the dark', artist='Joji')
song2 = Song(title='demons', artist='Joji')
playlist = Playlist(name='my playlist', description='test')
db.session.add(song1)
db.session.add(song2)
db.session.add(playlist)
db.session.commit()

playlistsong1 = PlaylistSong(song_id=song1.id, playlist_id=playlist.id)
playlistsong2 = PlaylistSong(song_id=song2.id, playlist_id=playlist.id)
db.session.add(playlistsong1)
db.session.add(playlistsong2)
db.session.commit()
示例#23
0
bohemian_rhapsody = Song(title='Bohemian Rhapsody', artist='Queen')
red_barchetta = Song(title='Red Barchetta', artist='Rush')

feel_good_inc = Song(title='Feel Good Inc.', artist='Gorillaz')
uptown_funk = Song(title='Uptown Funk', artist='Mark Ronson')

#Add playlists
classic_rock = Playlist(name='Classic Rock',
                        description='music from the 60'
                        's-80'
                        's')
modern_rock = Playlist(name='Modern Rock',
                       description='music from the last ten years')

#Add playlist-songs
BR_CR = PlaylistSong(playlist_id=1, song_id=1)
RB_CR = PlaylistSong(playlist_id=1, song_id=2)
FGI_MR = PlaylistSong(playlist_id=2, song_id=3)
UF_MR = PlaylistSong(playlist_id=2, song_id=4)

db.session.add(bohemian_rhapsody)
db.session.add(red_barchetta)
db.session.add(feel_good_inc)
db.session.add(uptown_funk)

db.session.add(classic_rock)
db.session.add(modern_rock)

db.session.commit()

db.session.add(BR_CR)
示例#24
0
from models import Playlist, Song, PlaylistSong, db
from app import app

db.drop_all()
db.create_all()

p1 = Playlist(name='Romantic', description='date night songs')
p2 = Playlist(name='Italian', description='cooking music')
p3 = Playlist(name='Coffee shop', description='Study music')

db.session.add_all([p1, p2, p3])
db.session.commit()

s1 = Song(title='Fire For You', artist='Cannons', relation=[
          PlaylistSong(playlist_id=p1.id)])
s2 = Song(title='Lei e noi', artist='I Segreti', relation=[
          PlaylistSong(playlist_id=p2.id)])
s3 = Song(title='A Bridge To Mend', artist='Rizik', relation=[
          PlaylistSong(playlist_id=p3.id)])

db.session.add_all([s1, s2, s3])

db.session.commit()
示例#25
0
def show_form(playlist_id):
    """Show form that searches new form, and show results"""
    playlist = Playlist.query.get(playlist_id)
    play_id = playlist_id
    form = SearchSongsForm()
    resultsSong = []
    checkbox_form = request.form

    list_of_songs_spotify_id_on_playlist = []
    for song in playlist.songs:
        list_of_songs_spotify_id_on_playlist.append(song.spotify_id)
    songs_on_playlist_set = set(list_of_songs_spotify_id_on_playlist)

    if form.validate_on_submit() and checkbox_form['form'] == 'search_songs':
        track_data = form.track.data
        api_call_track = my_spotify_client.search(track_data, 'track')

        # get search results, don't inclue songs that are on playlist already
        for item in api_call_track['tracks']['items']:
            if item['id'] not in songs_on_playlist_set:
                images = [image['url'] for image in item['album']['images']]
                artists = [artist['name'] for artist in item['artists']]
                urls = item['album']['external_urls']['spotify']
                resultsSong.append({
                    'title': item['name'],
                    'spotify_id': item['id'],
                    'album_name': item['album']['name'],
                    'album_image': first(images, ''),
                    'artists': ", ".join(artists),
                    'url': urls
                })

    # search results checkbox form
    if 'form' in checkbox_form and checkbox_form['form'] == 'pick_songs':
        list_of_picked_songs = checkbox_form.getlist('track')
        # map each item in list of picked songs
        jsonvalues = [json.loads(item) for item in list_of_picked_songs]

        for item in jsonvalues:
            title = item['title']
            spotify_id = item['spotify_id']
            album_name = item['album_name']
            album_image = item['album_image']
            artists = item['artists']
            # print(title)
            new_songs = Song(title=title,
                             spotify_id=spotify_id,
                             album_name=album_name,
                             album_image=album_image,
                             artists=artists)
            db.session.add(new_songs)
            db.session.commit()
            # add new song to its playlist
            playlist_song = PlaylistSong(song_id=new_songs.id,
                                         playlist_id=playlist_id)
            db.session.add(playlist_song)
            db.session.commit()

        return redirect(f'/playlists/{playlist_id}')

    def serialize(obj):
        return json.dumps(obj)

    return render_template('song/search_new_songs.html',
                           playlist=playlist,
                           form=form,
                           resultsSong=resultsSong,
                           serialize=serialize)
示例#26
0
# Create dummy song
song1 = Song(title="Pare Ko", artist="Eraserheads")
song2 = Song(title="Gitara", artist="Parokya Ni Edgar")
song3 = Song(title="Pare", artist="Jay Pegarido")

db.session.add(song1)
db.session.add(song2)
db.session.add(song3)

db.session.commit()

# create dummy playlist
pl1 = Playlist(name="Personal", description="Songs written by me")
pl2 = Playlist(name="OPM", description="Original Pinoy Music")

db.session.add(pl1)
db.session.add(pl2)

db.session.commit()

# Create Playlistsong table
pls1 = PlaylistSong(playlist_id=1, song_id=3)
pls2 = PlaylistSong(playlist_id=2, song_id=1)
pls3 = PlaylistSong(playlist_id=2, song_id=2)

db.session.add(pls1)
db.session.add(pls2)
db.session.add(pls3)

db.session.commit()