Example #1
0
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
Example #2
0
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)
Example #3
0
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)
Example #4
0
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')
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)