from flask import Blueprint, render_template, request, redirect, url_for, current_app, send_from_directory
|
|
from werkzeug.utils import secure_filename
|
|
from flask_login import login_user, logout_user, login_required
|
|
from sqlalchemy import desc
|
|
|
|
import os
|
|
|
|
from .models import db, Song, Album
|
|
|
|
bp = Blueprint("paginas", __name__)
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
@bp.route('/letras')
|
|
def letras():
|
|
songs = Song.query.all()
|
|
return render_template('letras.html', songs=songs)
|
|
|
|
@bp.route('/albumes')
|
|
def albumes():
|
|
albums = Album.query.all()
|
|
return render_template('albumes.html', albums=albums)
|
|
|
|
@bp.route('/404')
|
|
def cuatrocerocuatro():
|
|
return render_template('404.html')
|
|
|
|
@bp.route('/help')
|
|
def help():
|
|
return render_template('help.html')
|
|
|
|
@bp.route('/account')
|
|
@login_required
|
|
def account():
|
|
return render_template('account.html')
|
|
|
|
@bp.route('/song/<int:song_id>')
|
|
def song(song_id):
|
|
song = Song.query.get_or_404(song_id)
|
|
album = Album.query.get_or_404(song.album_id)
|
|
|
|
return render_template('song.html', song=song, album=album)
|
|
|
|
@bp.route('/add_song', methods=['GET', 'POST'])
|
|
@login_required
|
|
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']
|
|
pista = request.form['pista']
|
|
|
|
|
|
new_song = Song(title=title, author=author, album_id=album_id, lyrics=lyrics, pista=pista)
|
|
db.session.add(new_song)
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('paginas.album', album_id=album_id))
|
|
|
|
albums = Album.query.all()
|
|
return render_template('add_song.html', albums=albums)
|
|
|
|
@bp.route('/add_song2album/<int:album_id>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def add_song2album(album_id):
|
|
album = Album.query.filter_by(id=album_id).first() # obtiene el album cuyo id hemos recibido
|
|
|
|
ultimapista = Song.query.filter_by(album_id=album.id).order_by(desc(Song.pista)).first()
|
|
|
|
if ultimapista:
|
|
pista = ultimapista.pista + 1
|
|
print("Mayor: ", ultimapista.pista)
|
|
else:
|
|
pista = 1
|
|
print("No hay ninguna pista")
|
|
|
|
if request.method == 'POST':
|
|
title = request.form['title']
|
|
lyrics = request.form['lyrics']
|
|
author = album.artist
|
|
|
|
new_song = Song(title=title, author=author, album_id=album_id, lyrics=lyrics, pista=pista)
|
|
db.session.add(new_song)
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('paginas.album', album_id=album_id))
|
|
|
|
albums = Album.query.all()
|
|
return render_template('add_song2album.html', pista=pista, album=album, album_id=album_id)
|
|
|
|
@bp.route('/add_album', methods=['GET', 'POST'])
|
|
@login_required
|
|
def add_album():
|
|
if request.method == 'POST':
|
|
|
|
name = request.form['name']
|
|
artist = request.form['artist']
|
|
year = request.form['year']
|
|
|
|
# Verificar que el campo 'cover_image' está en request.files
|
|
if 'coverimage' not in request.files:
|
|
return "No file part in the request", 400
|
|
|
|
cover_image = request.files['coverimage']
|
|
|
|
# Verificar que se ha seleccionado un archivo
|
|
|
|
if cover_image.filename == '':
|
|
return "No selected file", 400
|
|
|
|
if cover_image:
|
|
image_filename = secure_filename(cover_image.filename)
|
|
cover_image.save(os.path.join(current_app.config['UPLOAD_FOLDER'], image_filename))
|
|
else:
|
|
image_filename = None
|
|
|
|
new_album = Album(name=name, artist=artist, year=year, cover_image=image_filename)
|
|
db.session.add(new_album)
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('paginas.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('/uploads/<filename>')
|
|
def uploaded_file(filename):
|
|
return send_from_directory(current_app.config['UPLOAD_FOLDER'], filename)
|
|
|
|
@bp.route('/searchsong')
|
|
def searchsong():
|
|
query = request.args.get('query', '')
|
|
if query:
|
|
songs = Song.query.filter(Song.title.contains(query)).all()
|
|
else:
|
|
songs = []
|
|
return render_template('searchsong.html', query=query, songs=songs)
|
|
|
|
|
|
@bp.route('/searchalbum')
|
|
def searchalbum():
|
|
query = request.args.get('query', '')
|
|
if query:
|
|
albumes = Album.query.filter(Album.name.contains(query)).all()
|
|
else:
|
|
albumes = []
|
|
return render_template('searchalbum.html', query=query, albumes=albumes)
|