from flask import Blueprint, render_template, request, redirect, url_for, current_app, send_from_directory, flash
|
|
from werkzeug.utils import secure_filename
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
|
|
|
import os
|
|
|
|
from .models import db, receta, Ingredient, Instruction
|
|
|
|
bp = Blueprint("paginas", __name__)
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
@bp.route('/recetas')
|
|
def recetas():
|
|
recetas = receta.query.all()
|
|
return render_template('recetas.html', recetas=recetas)
|
|
|
|
@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('/receta/<int:receta_id>')
|
|
def receta(receta_id):
|
|
receta = receta.query.get_or_404(receta_id)
|
|
return render_template('recetas.html', receta=receta)
|
|
|
|
@bp.route('/anade_receta', methods=['GET', 'POST'])
|
|
@login_required
|
|
def anade_receta():
|
|
if request.method == 'POST':
|
|
title = request.form['title']
|
|
description = request.form['description']
|
|
ingredients = request.form.getlist('ingredient')
|
|
quantities = request.form.getlist('quantity')
|
|
step_descriptions = request.form.getlist('step_description')
|
|
|
|
receta = receta(title=title, description=description, author=current_user)
|
|
|
|
for i, (ingredient, quantity) in enumerate(zip(ingredients, quantities), start=1):
|
|
receta.ingredients.append(Ingredient(name=ingredient, quantity=quantity, order=i))
|
|
|
|
for i, description in enumerate(step_descriptions, start=1):
|
|
receta.instructions.append(Instruction(step=i, description=description, order=i))
|
|
|
|
db.session.add(receta)
|
|
db.session.commit()
|
|
flash('receta created successfully!', 'success')
|
|
|
|
|
|
return redirect(url_for('paginas.index'))
|
|
|
|
return render_template('nueva_receta.html')
|
|
|
|
@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('/buscareceta')
|
|
def buscareceta():
|
|
query = request.args.get('query', '')
|
|
if query:
|
|
songs = Song.query.filter(Song.title.contains(query)).all()
|
|
else:
|
|
songs = []
|
|
return render_template('buscareceta.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)
|