def createUser(login_session): """Create new user in DB.""" newUser = User(name=login_session['username'], email=login_session['email']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def deleteGenre(genre_id): """Render template to delete genre and handle post request.""" if 'username' not in login_session: return redirect('/login') genre = session.query(Genre).filter_by(id=genre_id).one() if login_session['user_id'] != genre.user_id: return render_template('notallowed.html') if request.method == 'POST': session.delete(genre) session.commit() flash('Genre deleted!') return redirect(url_for('showGenres')) else: return render_template('deletegenre.html', genre=genre)
def deleteSong(song_id, release_id): """Render template to delete a song and handle post request.""" if 'username' not in login_session: return redirect('/login') song = session.query(Song).filter_by(id=song_id).one() release = session.query(Album).filter_by(id=release_id).one() if login_session['user_id'] != song.user_id: return render_template('notallowed.html') if request.method == 'POST': session.delete(song) session.commit() flash('Song deleted!') return redirect(url_for('showSongs', release_id=release_id)) if request.method == 'GET': return render_template('deletesong.html', song=song, release=release)
def editGenre(genre_id): """Render template to edit genre and handle post request.""" if 'username' not in login_session: return redirect('/login') genre = session.query(Genre).filter_by(id=genre_id).one() if login_session['user_id'] != genre.user_id: return render_template('notallowed.html') if request.method == 'POST': if request.form['name']: genre.name = request.form['name'] session.add(genre) session.commit() flash('Genre updated!') return redirect(url_for('showGenres')) return render_template('editgenre.html', genre=genre)
def deleteRelease(genre_id, release_id): """Render template to delete a release and handle post request.""" if 'username' not in login_session: return redirect('/login') genre = session.query(Genre).filter_by(id=genre_id).one() release = session.query(Album).filter_by(id=release_id).one() if login_session['user_id'] != release.user_id: return render_template('notallowed.html') if request.method == 'POST': session.delete(release) session.commit() flash('Release deleted!') return redirect(url_for('showReleases', genre_id=genre_id)) if request.method == 'GET': return render_template('deleterelease.html', genre=genre, release=release)
def editSong(song_id, release_id): """Render template to edit a song and handle post request.""" if 'username' not in login_session: return redirect('/login') song = session.query(Song).filter_by(id=song_id).one() release = session.query(Album).filter_by(id=release_id).one() if login_session['user_id'] != song.user_id: return render_template('notallowed.html') if request.method == 'POST': if request.form['title']: song.title = request.form['title'] if request.form['position']: song.position = request.form['position'] session.add(song) session.commit() flash('Song updated!') return redirect(url_for('showSongs', release_id=release_id)) return render_template('editsong.html', song=song, release=release)
def newGenre(): """Render template to add new genre and handle post request.""" if 'username' not in login_session: return redirect('/login') if request.method == 'POST': if not (session.query(Genre).filter_by( name=request.form['name']).first()): session.add( Genre(name=request.form['name'], user_id=login_session['user_id'])) session.commit() flash('New Genre added!') return redirect(url_for('showGenres')) else: flash('This genre already exists!') return render_template('newgenre.html') if request.method == 'GET': return render_template('newgenre.html')
def newSong(release_id): """Render template to add song to a release and handle post request.""" if 'username' not in login_session: return redirect('/login') release = session.query(Album).filter_by(id=release_id).one() # connected user must be the creator of a release to add a new song to it if login_session['user_id'] != release.user_id: return render_template('notallowed.html') if request.method == 'POST': song = Song( title=request.form['title'], position=request.form['position'], release_id=release.id, # force song.user_id == release.user_id user_id=release.user_id) session.add(song) session.commit() flash('New song added!') return redirect(url_for('showSongs', release_id=release_id)) if request.method == 'GET': return render_template('newsong.html', release=release)
def editRelease(genre_id, release_id): """Render template to edit a release and handle post request.""" if 'username' not in login_session: return redirect('/login') genre = session.query(Genre).filter_by(id=genre_id).one() release = session.query(Album).filter_by(id=release_id).one() if login_session['user_id'] != release.user_id: return render_template('notallowed.html') if request.method == 'POST': if request.form['title']: release.title = request.form['title'] if request.form['artist']: release.artist = request.form['artist'] if request.form['label']: release.label = request.form['label'] if request.form['released']: release.released = request.form['released'] if request.form['image']: release.image = request.form['image'] session.add(release) session.commit() flash('Release updated!') return redirect(url_for('showReleases', genre_id=genre_id)) return render_template('editrelease.html', genre=genre, release=release)
def newRelease(genre_id): """Render template to add new release and handle post request.""" if 'username' not in login_session: return redirect('/login') genre = session.query(Genre).filter_by(id=genre_id).one() if request.method == 'POST': image = request.form['image'] if request.form[ 'image'] != '' else '/static/record-player.jpeg' release = Album( title=request.form['title'], artist=request.form['artist'], label=request.form['label'], released=request.form['released'], image=image, genre_id=genre.id, # connected user doesn't have to be # the creator of a genre to add a new release to it user_id=login_session['user_id']) session.add(release) session.commit() flash('New release added!') return redirect(url_for('showReleases', genre_id=genre_id)) if request.method == 'GET': return render_template('newrelease.html', genre=genre)