def PrivatePlaylist(): # get songID of the song being added to a playlist songID= request.args.get('songID') # get all playlist form data form = PlaylistForm() # done on a POST if form.validate_on_submit(): # check if a playlist with the given name already exists userid = current_user.get_id() cur=conn.cursor() cur.execute("SELECT id FROM PrivatePlaylist WHERE name = %s", form.playlistName.data) results = cur.fetchall() # Don't allow multiple playlists by the same user to have the same name if(len(results) > 0): flash("Playlist with that name already exists") return redirect(url_for('Search')) # Create the new playlist and add the song to it cur.execute("INSERT INTO PrivatePlaylist(name, datecreated, userid, id) VALUES(%s, %s, %s, NULL)", (form.playlistName.data, date.today(), userid)) conn.commit() cur.execute("INSERT INTO PrivateSongs(id, userid, songid, playlistID) VALUES(NULL, %s, %s, LAST_INSERT_ID())", (userid, songID)) conn.commit() cur.close() return redirect(url_for('Search')) # done on a GET cur = conn.cursor() # show the user all of their current private playlists cur.execute("SELECT DISTINCT name FROM PrivatePlaylist where userid=%s", (current_user.get_id())) results = cur.fetchall() cur.close() reqtype = 'Private' return render_template('user.html', form=form, title='User', results=results, songID=songID, reqtype=reqtype)
def Register(): # redirect the user to the account page if they're already logged in if current_user.is_authenticated: return redirect(url_for('Account')) # grab the information from the register form form = RegisterForm() # check information when a valid form is submitted if form.validate_on_submit(): # create a connection to the database and insert the new user conn = mysql.connect() cur = conn.cursor() # hash the given password hashPass = bcrypt.generate_password_hash(form.password.data).decode('utf-8') cur.execute("INSERT INTO User(username, password, email) VALUES(%s, %s, %s)", (form.username.data, hashPass, form.email.data)) conn.commit() cur.close() conn.close() flash("Your account has been created!", 'success') # redirect to login page once registered return redirect(url_for('Login')) return render_template('register.html', title='Search', form=form)
def AddPrivateSong(): # gather all information from previous page playlist = request.args.get('playlist') userid = current_user.get_id() songID = request.args.get('songID') # get the id of the playlist being inserted into cur=conn.cursor() cur.execute("SELECT id FROM PrivatePlaylist WHERE name = %s", playlist) results = cur.fetchone() # check if song already exists in playlist cur.execute("SELECT PrivateSongs.songid FROM PrivateSongs, PrivatePlaylist WHERE PrivateSongs.songid = %s and PrivatePlaylist.name = %s", (songID, playlist)) check = cur.fetchone() # don't allow the same song to be inserted into the same playlist if check != None: cur.close() flash(f"The song you've selected is already in { playlist }", 'info') return redirect(url_for('Search')) else: # insert the new song into the playlist cur.execute("INSERT INTO PrivateSongs(id, userid, songid, playlistID) VALUES(NULL, %s, %s, %s)", (userid, songID, results[0])) conn.commit() cur.close() return redirect(url_for('Search'))
def DeleteSong(): # gather all data from previous html page candelete=request.args.get('candelete') songID= request.args.get('songID') playlistID= request.args.get('playlistid') playlistType= request.args.get('playlistType') name=request.args.get('name') # if the song is being deleted from a public playlist if playlistType == 'public': # delete the specified song from the specified playlist then return the remaining playlists to the user cur=conn.cursor() cur.execute("delete from PublicSongs where playlistID=%s and songid=%s", (playlistID, songID)) conn.commit() cur.execute("SELECT PublicSongs.songid, Song.title, Artist.name, Album.title FROM PublicSongs, Song, Artist, Album WHERE PublicSongs.playlistID = %s AND PublicSongs.songid = Song.id AND Artist.id = Song.artistID AND Album.id = Song.albumID", playlistID) results = cur.fetchall() # if there are no more songs on the playlist, delete it and return to the account page if not results: cur.execute("delete from PublicPlaylist where id=%s ", playlistID) conn.commit() cur.close() return redirect(url_for('Account')) cur.close() return render_template('playlist.html', title='View', candelete=candelete, name=name, results=results, playlistid=playlistID, playlistType='public') # if the song is being deleted from a private playlist elif playlistType == 'private': # delete the specified song from the specified playlist then return the remaining playlists to the user cur=conn.cursor() cur.execute("delete from PrivateSongs where playlistID=%s and songid=%s", (playlistID, songID)) conn.commit() cur.execute("SELECT PrivateSongs.songid, Song.title, Artist.name, Album.title FROM PrivateSongs, Song, Artist, Album WHERE PrivateSongs.playlistID = %s AND PrivateSongs.songid = Song.id AND Artist.id = Song.artistID AND Album.id = Song.albumID", playlistID) results = cur.fetchall() # if there are no more songs on the playlist, delete it and return to the account page if not results: cur.execute("delete from PrivatePlaylist where id=%s ", playlistID) conn.commit() cur.close() return redirect(url_for('Account')) cur.close() return render_template('playlist.html', title='View', candelete=candelete, name=name, results=results, playlistid=playlistID, playlistType='private') # error handle else: flash(f"Failed to delete your song from { name }. Try again.", 'info') return redirect(url_for('Account'))