from flask import Blueprint, render_template, request, redirect, url_for
|
|
from .models import db, Song, Album
|
|
|
|
bp = Blueprint("paginas", __name__)
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
songs = Song.query.all()
|
|
albums = Album.query.all()
|
|
return render_template('index.html', songs=songs, albums=albums)
|
|
|
|
@bp.route('/song/<int:song_id>')
|
|
def song(song_id):
|
|
song = Song.query.get_or_404(song_id)
|
|
return render_template('song.html', song=song)
|
|
|
|
@bp.route('/add_song', methods=['GET', 'POST'])
|
|
def add_song():
|
|
if request.method == 'POST':
|
|
title = request.form['title']
|
|
author = request.form['author']
|
|
album_id = request.form['album_id']
|
|
lyrics = request.form['lyrics']
|
|
|
|
new_song = Song(title=title, author=author, album_id=album_id, lyrics=lyrics)
|
|
db.session.add(new_song)
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
albums = Album.query.all()
|
|
return render_template('add_song.html', albums=albums)
|
|
|
|
@bp.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')
|
|
|
|
@bp.route('/album/<int:album_id>')
|
|
def album(album_id):
|
|
album = Album.query.get_or_404(album_id)
|
|
songs = Song.query.filter_by(album_id=album_id).all()
|
|
return render_template('album.html', album=album, songs=songs)
|
|
|
|
@bp.route('/search')
|
|
def search():
|
|
query = request.args.get('query', '')
|
|
if query:
|
|
songs = Song.query.filter(Song.title.contains(query)).all()
|
|
else:
|
|
songs = []
|
|
return render_template('search.html', query=query, songs=songs)
|