def showReleases(genre_id): """Render template to show all releases of one genre.""" # check if user logged if 'username' not in login_session: user_id = False else: user_id = login_session['user_id'] genre = session.query(Genre).filter_by(id=genre_id).one() releases = (session.query(Album).filter_by(genre_id=genre_id).order_by( Album.title).all()) return render_template('releases.html', genre=genre, releases=releases, user_id=user_id)
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 getUserInfo(user_id): """Get user info from DB. arg: user ID integer """ user = session.query(User).filter_by(id=user_id).one() return user
def showSongs(release_id): """Render template to show all songs.""" # check if user logged if 'username' not in login_session: user_id = False else: user_id = login_session['user_id'] release = session.query(Album).filter_by(id=release_id).one() genre = session.query(Genre).filter_by(id=release.genre_id).one() songs = (session.query(Song).filter_by(release_id=release_id).order_by( Song.position).all()) return render_template('songs.html', release=release, songs=songs, user_id=user_id, 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 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 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 getUserID(email): """Get user ID from DB. arg: user's email string """ try: user = session.query(User).filter_by(email=email).one() return user.id except Exception: return None
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 showGenres(): """Render template displaying all genres.""" # check if user logged in: if 'username' not in login_session: user_id = False else: user_id = login_session['user_id'] genres_count = (session.query( Genre.id, Genre.name, Genre.user_id, func.count(Album.genre_id)).outerjoin(Album).group_by(Genre).order_by( Genre.name).all()) return render_template('genres.html', genres_count=genres_count, user_id=user_id)
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 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 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 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)
def genresJSON(): """Handle genres JSON API Endpoint.""" genres = session.query(Genre).all() return jsonify([genre.serialize for genre in genres])
def songsJSON(release_id): """Handle songs JSON API Endpoint.""" songs = session.query(Song).filter_by(release_id=release_id).all() return jsonify([song.serialize for song in songs])
def releasesJSON(genre_id): """Handle specific genre JSON API Endpoint.""" releases = session.query(Album).filter_by(genre_id=genre_id).all() return jsonify([release.serialize for release in releases])