Exemplo n.º 1
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)
Exemplo n.º 2
0
def add_song_to_playlist(playlist_id):
    """Add a song to 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 = (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
        song = Song.query.get_or_404(song_id)

        playlist.songs.append(song)
        db.session.commit()

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

    return render_template("add_song_to_playlist.html",
                           playlist=playlist,
                           form=form)
Exemplo n.º 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 = [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,
    )
Exemplo n.º 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
    # 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)
Exemplo n.º 5
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)
Exemplo n.º 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)
Exemplo n.º 7
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
    )
Exemplo n.º 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

    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)
Exemplo n.º 9
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)
Exemplo n.º 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)
Exemplo n.º 11
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.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 request.method == "GET":

        return render_template("add_song_to_playlist.html",
                               playlist=playlist,
                               form=form)
    elif request.method == "POST":
        if form.validate_on_submit():

            # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK
            song_id = form.song.data
            song = Song.query.get_or_404(song_id)
            playlist.songs.append(song)
            db.session.add(playlist)
            db.session.commit()

            print(
                'Playlist songs on add route:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'
            )
            print(playlist.songs)
            print(
                ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'
            )

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

        else:

            return render_template('/add_song_to_playlist.html',
                                   playlist=playlist,
                                   form=form)

    else:

        flash('something went wrong')
        return redirect(f'/playlists/{playlist_id}')
Exemplo n.º 12
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)
Exemplo n.º 13
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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 16
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 = ...
    form.song.choices = ...

    if form.validate_on_submit():

          # 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)