|
|
@ -1,5 +1,5 @@ |
|
|
from flask import Flask, render_template, request, redirect, url_for |
|
|
from flask import Flask, render_template, request, redirect, url_for |
|
|
from models import db, Song |
|
|
|
|
|
|
|
|
from models import db, Song, Album |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
app = Flask(__name__) |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' |
|
|
@ -15,29 +15,45 @@ def before_request(): |
|
|
@app.route('/') |
|
|
@app.route('/') |
|
|
def index(): |
|
|
def index(): |
|
|
songs = Song.query.all() |
|
|
songs = Song.query.all() |
|
|
return render_template('index.html', songs=songs) |
|
|
|
|
|
|
|
|
albums = Album.query.all() |
|
|
|
|
|
return render_template('index.html', songs=songs, albums=albums) |
|
|
|
|
|
|
|
|
@app.route('/song/<int:song_id>') |
|
|
@app.route('/song/<int:song_id>') |
|
|
def song(song_id): |
|
|
def song(song_id): |
|
|
song = Song.query.get_or_404(song_id) |
|
|
song = Song.query.get_or_404(song_id) |
|
|
return render_template('song.html', song=song) |
|
|
return render_template('song.html', song=song) |
|
|
|
|
|
|
|
|
@app.route('/add', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
|
@app.route('/add_song', methods=['GET', 'POST']) |
|
|
def add_song(): |
|
|
def add_song(): |
|
|
if request.method == 'POST': |
|
|
if request.method == 'POST': |
|
|
title = request.form['title'] |
|
|
title = request.form['title'] |
|
|
author = request.form['author'] |
|
|
author = request.form['author'] |
|
|
year = request.form['year'] |
|
|
|
|
|
album = request.form['album'] |
|
|
|
|
|
|
|
|
album_id = request.form['album_id'] |
|
|
lyrics = request.form['lyrics'] |
|
|
lyrics = request.form['lyrics'] |
|
|
|
|
|
|
|
|
new_song = Song(title=title, author=author, year=year, album=album, lyrics=lyrics) |
|
|
|
|
|
|
|
|
new_song = Song(title=title, author=author, album_id=album_id, lyrics=lyrics) |
|
|
db.session.add(new_song) |
|
|
db.session.add(new_song) |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
return redirect(url_for('index')) |
|
|
return redirect(url_for('index')) |
|
|
|
|
|
|
|
|
return render_template('add_song.html') |
|
|
|
|
|
|
|
|
albums = Album.query.all() |
|
|
|
|
|
return render_template('add_song.html', albums=albums) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/add_album', methods=['GET', 'POST']) |
|
|
|
|
|
def add_album(): |
|
|
|
|
|
if request.method == 'POST': |
|
|
|
|
|
name = request.form['name'] |
|
|
|
|
|
artist = request.form['artist'] |
|
|
|
|
|
year = request.form['year'] |
|
|
|
|
|
|
|
|
|
|
|
new_album = Album(name=name, artist=artist, year=year) |
|
|
|
|
|
db.session.add(new_album) |
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
return redirect(url_for('index')) |
|
|
|
|
|
|
|
|
|
|
|
return render_template('add_album.html') |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
with app.app_context(): |
|
|
with app.app_context(): |
|
|
|