|
|
@ -1,29 +1,20 @@ |
|
|
from flask import Flask, render_template, request, redirect, url_for |
|
|
|
|
|
from models import db, Song, Album |
|
|
|
|
|
|
|
|
from flask import Blueprint, render_template, request, redirect, url_for |
|
|
|
|
|
from .models import db, Song, Album |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' |
|
|
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
|
|
|
|
|
db.init_app(app) |
|
|
|
|
|
|
|
|
bp = Blueprint("paginas", __name__) |
|
|
|
|
|
|
|
|
@app.before_request |
|
|
|
|
|
def before_request(): |
|
|
|
|
|
if not hasattr(app, 'db_initialized'): |
|
|
|
|
|
db.create_all() |
|
|
|
|
|
app.db_initialized = True |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
|
|
|
|
|
@bp.route('/') |
|
|
def index(): |
|
|
def index(): |
|
|
songs = Song.query.all() |
|
|
songs = Song.query.all() |
|
|
albums = Album.query.all() |
|
|
albums = Album.query.all() |
|
|
return render_template('index.html', songs=songs, albums=albums) |
|
|
return render_template('index.html', songs=songs, albums=albums) |
|
|
|
|
|
|
|
|
@app.route('/song/<int:song_id>') |
|
|
|
|
|
|
|
|
@bp.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_song', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
|
@bp.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'] |
|
|
@ -40,7 +31,7 @@ def add_song(): |
|
|
albums = Album.query.all() |
|
|
albums = Album.query.all() |
|
|
return render_template('add_song.html', albums=albums) |
|
|
return render_template('add_song.html', albums=albums) |
|
|
|
|
|
|
|
|
@app.route('/add_album', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
|
@bp.route('/add_album', methods=['GET', 'POST']) |
|
|
def add_album(): |
|
|
def add_album(): |
|
|
if request.method == 'POST': |
|
|
if request.method == 'POST': |
|
|
name = request.form['name'] |
|
|
name = request.form['name'] |
|
|
@ -55,13 +46,13 @@ def add_album(): |
|
|
|
|
|
|
|
|
return render_template('add_album.html') |
|
|
return render_template('add_album.html') |
|
|
|
|
|
|
|
|
@app.route('/album/<int:album_id>') |
|
|
|
|
|
|
|
|
@bp.route('/album/<int:album_id>') |
|
|
def album(album_id): |
|
|
def album(album_id): |
|
|
album = Album.query.get_or_404(album_id) |
|
|
album = Album.query.get_or_404(album_id) |
|
|
songs = Song.query.filter_by(album_id=album_id).all() |
|
|
songs = Song.query.filter_by(album_id=album_id).all() |
|
|
return render_template('album.html', album=album, songs=songs) |
|
|
return render_template('album.html', album=album, songs=songs) |
|
|
|
|
|
|
|
|
@app.route('/search') |
|
|
|
|
|
|
|
|
@bp.route('/search') |
|
|
def search(): |
|
|
def search(): |
|
|
query = request.args.get('query', '') |
|
|
query = request.args.get('query', '') |
|
|
if query: |
|
|
if query: |
|
|
@ -69,8 +60,3 @@ def search(): |
|
|
else: |
|
|
else: |
|
|
songs = [] |
|
|
songs = [] |
|
|
return render_template('search.html', query=query, songs=songs) |
|
|
return render_template('search.html', query=query, songs=songs) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
with app.app_context(): |
|
|
|
|
|
db.create_all() |
|
|
|
|
|
app.run(debug=True) |
|
|
|