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, Recipe, Ingredient, Instruction
|
|
|
|
bp = Blueprint("paginas", __name__)
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
@bp.route('/recetas')
|
|
def recetas():
|
|
recetas = Recipe.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 = Recipe.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):
|
|
Recipe.ingredients.append(Ingredient(name=ingredient, quantity=quantity, order=i))
|
|
|
|
for i, description in enumerate(step_descriptions, start=1):
|
|
Recipe.instructions.append(Instruction(step=i, description=description, order=i))
|
|
|
|
db.session.add(Recipe)
|
|
db.session.commit()
|
|
flash('receta created successfully!', 'success')
|
|
|
|
|
|
return redirect(url_for('paginas.index'))
|
|
|
|
return render_template('nueva_receta.html')
|
|
|
|
@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 = Recipe.query.filter(Recipe.title.contains(query)).all()
|
|
else:
|
|
songs = []
|
|
return render_template('buscareceta.html', query=query, songs=songs)
|