def all_cook_recipes(username): form = SearchRecipeForm() if form.is_submitted(): return redirect( url_for('search.cook_recipe', username=username, recipe_name=form.recipe.data)) page, recs = utils.all_cook_recipes(username) if isinstance(recs, str): flash(recs, 'danger') return redirect(url_for('users.all_cooks')) desc = 'Todas nuestras recetas, veganas y saludables. Aquí encontrarás \ platos muy variados, algunas rápidas, otras más elaboradas, pero todas \ bien explicadas y con vídeo incluido.' return render_template('search/cook_recipes.html', title=f'Recetas de {username}', form=form, username=username, recipe_form=SearchRecipeForm(), recipes=recs, cook_recipes=utils.cook_recipe_names(username), last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=utils.get_default_keywords())
def cook_recipe(username, recipe_name): page, recs = utils.cook_recipes(username, recipe_name) if isinstance(recs, str): flash(recs, 'danger') return redirect(url_for('search.all_cook_recipes', username=username)) return render_template( 'search/cook_recipes.html', title=recipe_name, form=SearchRecipeForm(), # form is submitted to all_cook_recipes() recipe_form=SearchRecipeForm(), # sidebar form username=username, recipes=recs, cook_recipes=utils.cook_recipe_names(username), last_recipes=get_last_recipes(4), keywords=utils.get_search_keywords(recipe_name))
def search_recipe(recipe_name): form = SearchRecipeForm() if form.is_submitted(): return redirect( url_for('search.search_recipe', recipe_name=form.recipe.data)) page, recs = utils.get_recipes_by_name(recipe_name) if isinstance(recs, str): flash(recs, 'danger') return redirect(url_for('search.search_all_recipes')) return render_template('search/recipe.html', title=recipe_name, recipe_form=SearchRecipeForm(), recipes=recs, last_recipes=get_last_recipes(), keywords=utils.get_search_keywords(recipe_name))
def create_recipe(): """ NewRecipeForm doesn't have requirements, so it can be saved unfinished. """ recipe = utils.create_recipe() form = utils.prepare_form(NewRecipeForm(obj=recipe), recipe) if form.validate_on_submit(): errors = validate.validate_name(form) if len(errors) == 0: recipe = create.save_recipe(form, valid=False) if form.state.data == 1: flash('La receta ha sido creada.', 'success') return redirect(url_for('recipes.sort_by_date', arg='True')) return redirect( url_for( # check if ready for publishing. 'recipes.edit_recipe', recipe_url=recipe.url, state=form.state.data)) for error in errors: flash(error, 'danger') return render_template("edit/recipe.html", title="Crear receta", recipe=recipe, recipe_form=SearchRecipeForm(), last_recipes=utils.get_last_recipes(4), all_ingredients=get_all_ingredients(), all_subrecipes=utils.get_all_subrecipes(), all_units=utils.get_all_units(), form=form, is_edit_recipe=True)
def search_ingredient(ing_url): form = IngredientsForm() if form.is_submitted(): ingredient = utils.get_ingredient_by_name(form.ingredient.data) return redirect( url_for('ingredients.search_ingredient', ing_url=ingredient.url)) ing = utils.get_ingredient_by_url(ing_url) page, recs = search.get_recipes_by_ingredient(ing.name) if isinstance(recs, str): flash(recs, 'danger') return redirect(url_for('ingredients.search_all_ingredients')) desc = f'Recetas veganas y saludables con {ing.name}. Por si se te antoja \ {ing.name}, o lo compraste y buscas inspiración.' return render_template('search/ingredient.html', title=ing.name, chosen_url=ing_url, recipe_form=SearchRecipeForm(), form=form, all_ingredients=utils.get_all_ingredients(), recipes=recs, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=utils.get_ing_keywords(ing.name))
def search_season(season_name): if season_name == 'Todo el año': return redirect(url_for('seasons.search_current_season')) form = SeasonForm() if form.is_submitted(): form_name = get_season_name(int(form.season.data)) return redirect(url_for('seasons.search_season', season_name=form_name)) page, recs = get_season_recipes(season_name) desc = f'Recetas para {season_name}. Escoge recetas de la temporada \ actual para cocinar con productos frescos y locales. Apoya a los \ negocios y agricultores de tu ciudad mientras que le echas una \ mano al medio ambiente.' return render_template( 'search/season.html', title=season_name, recipe_form=SearchRecipeForm(), form=form, season_name=season_name, recipes=recs, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_season_keywords(season_name) )
def edit_subrecipe(subrecipe_url): subrecipe = utils.get_subrecipe_by_url(subrecipe_url) if subrecipe.id_user != current_user.id: return abort(403) form = SubrecipeForm(obj=subrecipe) for subform in form.ingredients: subform.unit.choices = [ (u.id, u.singular) for u in Unit.query.order_by(Unit.singular) ] form.case.choices = [(0, "el"), (1, "la")] # Set case choices. if form.validate_on_submit(): if form.name.data != subrecipe.name: errors = validate.validate(form) else: # if name hasn't changed, check only ingredients. errors = validate.validate_ingredients(form) if len(errors) > 0: for error in errors: flash(error, 'danger') else: update.update_subrecipe(subrecipe, form) flash("Los cambios han sido guardados.", "success") return redirect(url_for("subrecipes.sort_by_date", arg='True')) return render_template( "edit/subrecipe.html", title="Editar subreceta", subrecipe=subrecipe, recipe_form=SearchRecipeForm(), all_ingredients=get_all_ingredients(), all_units=get_all_units(), last_recipes=get_last_recipes(4), form=form, is_edit_recipe=True )
def create_subrecipe(): subrecipe = utils.create_subrecipe() form = SubrecipeForm(obj=subrecipe) for subform in form.ingredients: subform.unit.choices = [ (u.id, u.singular) for u in Unit.query.order_by(Unit.singular) ] form.case.choices = [(0, "el"), (1, "la")] # Set case choices. if form.validate_on_submit(): errors = validate.validate(form) if len(errors) > 0: for error in errors: flash(error, 'danger') else: create.save_subrecipe(form) flash('La subreceta ha sido creada.', 'success') return redirect(url_for("subrecipes.sort_by_date", arg='True')) return render_template( "edit/subrecipe.html", title="Crear subreceta", subrecipe=subrecipe, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), all_ingredients=get_all_ingredients(), all_units=get_all_units(), form=form, is_edit_recipe=True, is_new_subrecipe=True )
def edit_new_recipe(recipe_url): """ Recipe state = 'Unfinished'. Validation is not thorough. """ recipe = utils.get_recipe_with_id(recipe_url, current_user.id) if recipe.id_user != current_user.id: return abort(403) form = utils.prepare_form(NewRecipeForm(obj=recipe), recipe) if form.validate_on_submit(): errors = validate.validate_name(form, recipe) if len(errors) == 0: flash('Los cambios han sido guardados.', 'success') recipe_url = update.update_recipe(recipe, form, valid=False) if form.state.data == 1: # create recipe and leave it unfinished return redirect(url_for('recipes.sort_by_date', arg='True')) return redirect( url_for( # check if it is ready for publishing 'recipes.edit_recipe', recipe_url=recipe_url, state=form.state.data)) for error in errors: flash(error, 'danger') return render_template( 'edit/recipe.html', form=form, title=recipe.name, recipe_form=SearchRecipeForm(), recipe=recipe, is_edit_recipe=True, all_ingredients=get_all_ingredients(), all_subrecipes=utils.get_all_subrecipes(), all_units=utils.get_all_units(), last_recipes=utils.get_last_recipes(4), description=f'Receta vegana y saludable: {recipe.name}. {recipe.intro}', keywords=utils.get_recipe_keywords(recipe), is_new_recipe=True)
def search_by_name(arg): return render_template('overview/recipe.html', title='Recetas', recipe_form=SearchRecipeForm(), last_recipes=utils.get_last_recipes(4), recipes=overview.search_name(arg)[1], arg='True', search_form=SearchForm())
def search_by_name(arg): return render_template('overview/ingredient.html', title='Ingredientes', recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), ingredients=overview.search_name(arg)[1], arg='True', search_form=SearchForm())
def search_all_recipes(): form = SearchRecipeForm() if form.is_submitted(): return redirect( url_for('search.search_recipe', recipe_name=form.recipe.data)) page, recs = get_paginated_recipes() desc = 'Todas nuestras recetas, veganas y saludables. Aquí encontrarás \ platos muy variados, algunas rápidas, otras más elaboradas, pero todas \ bien explicadas y con vídeo incluido.' return render_template('search/recipe.html', title='Recetas', recipe_form=SearchRecipeForm(), recipes=recs, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=utils.get_default_keywords())
def sort_by_creator(arg): """ Shows a list with all ingredients of the user, ordered by creator. """ return render_template("overview/ingredient.html", title="Ingredientes", recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), ingredients=overview.sort_by_creator(arg)[1], arg=arg, search_form=SearchForm())
def sort_by_state(arg): """ Shows a list with all recipes of the user, ordered by state. """ return render_template("overview/recipe.html", title="Recetas", recipe_form=SearchRecipeForm(), last_recipes=utils.get_last_recipes(4), recipes=overview.sort_by_state(arg)[1], arg=arg, search_form=SearchForm())
def get_recipe(username, recipe_url): recipe = utils.get_recipe_with_username(recipe_url, username) desc = f'Receta vegana y saludable: {recipe.name}. {recipe.intro}' return render_template('view/recipe.html', title=recipe.name, recipe_form=SearchRecipeForm(), recipe=recipe, is_recipe=True, last_recipes=utils.get_last_recipes(4), description=desc, keywords=utils.get_recipe_keywords(recipe))
def get_privacy(): desc = 'La privacidad de tus datos es muy importante para nosotros. \ Tanto, que no almacenamos ninguno en nuestras bases de datos' return render_template( 'view/privacy.html', title='Privacidad', recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_default_keywords() + ', privacidad' )
def get_donate(): desc = 'Con vuestras donaciones hacemos de este blog un proyecto \ sostenible y libre de anuncios.' return render_template( 'view/donate.html', title='Donar', recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_default_keywords() + ', donación' )
def get_philosophy(): desc = 'En este blog queremos demostrar que no hace falta sacrificar el \ sabor para ser vegano, ni para mantener un buen estado de salud.' return render_template( 'view/philosophy.html', title='Filosofía', recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_default_keywords() + ', filosofía' )
def view_profile(username): """ Returns the user home page, with an intro and recipes. """ user = utils.get_user(username) if user is None: return abort(404) return render_template( 'view/profile.html', title=username, user=user, user_recipes=utils.last_user_recipes(user.id), recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), )
def get_home(): desc = 'Recetas veganas para cualquier ocasión. Nos ajustamos al tiempo \ que tengas, los ingredientes que tengas por casa, tus antojos, y \ la temporada. Échale un vistazo a nuestros buscadores, o déjanos \ inspirarte con la receta de la semana.' return render_template( 'view/home.html', title='Inicio', recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(9), description=' '.join(desc.split()), keywords=get_default_keywords() )
def sort_by_name(arg): """ Shows a list with all recipes of the user, ordered by name. Also, if the search form is submitted, it redirects to the search_by_name route.""" form = SearchForm() if form.validate_on_submit(): return redirect(url_for('recipes.search_by_name', arg=form.name.data)) return render_template('overview/recipe.html', title='Recetas', recipe_form=SearchRecipeForm(), last_recipes=utils.get_last_recipes(4), recipes=overview.sort_by_name(arg)[1], arg=arg, search_form=form)
def search_time_undefined(): form = TimesForm() if form.is_submitted(): return redirect(url_for('times.search_time', time=form.time.data)) desc = 'Busca recetas veganas y saludables por tiempo; por si tienes \ prisa, o buscas un plato más elaborado. Sin importar lo que \ tardes, ¡dedícale tiempo a degustarlo!' return render_template('search/time.html', title='Tiempo', recipe_form=SearchRecipeForm(), form=form, recipes=None, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_times_keywords())
def search_cook(username): form = CookForm() if form.is_submitted(): return redirect( url_for('search.search_recipe', recipe_name=form.recipe.data)) page, cooks = utils.get_cooks(username) if isinstance(cooks, str): flash(cooks, 'danger') return redirect(url_for('utils.all_cooks')) return render_template('search/cooks.html', title=username, cook_form=form, all_usernames=utils.all_usernames(), cooks=cooks, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(), keywords=utils.get_cook_keywords(username))
def edit_profile(): """ Edit profile of the cook. """ form = utils.add_choices(ProfileForm(obj=current_user), current_user) if form.validate_on_submit(): errors = validate.validate_user(form, current_user) if len(errors) == 0: update.update_user(current_user, form) flash('Los cambios han sido guardados', 'success') else: for error in errors: flash(error, 'danger') return render_template( 'edit/profile.html', title='Editar perfil', form=form, all_media=utils.all_media(), recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), )
def create_ingredient(): ingredient = utils.create_ingredient() form = IngredientForm(obj=ingredient) if form.validate_on_submit(): errors = validate.validate_name(form) if len(errors) > 0: for error in errors: flash(error, 'danger') else: create.save_ingredient(form) flash('El ingrediente ha sido creado.', 'success') return redirect(url_for('ingredients.sort_by_date', arg='True')) return render_template("edit/ingredient.html", title="Crear ingrediente", ingredient=ingredient, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), form=form, is_edit_recipe=True)
def all_cooks(): form = CookForm() if form.is_submitted(): return redirect( url_for('users.search_cook', username=form.username.data)) page, cooks = utils.paginated_cooks() desc = 'Todas nuestras recetas, veganas y saludables. Aquí encontrarás \ platos muy variados, algunas rápidas, otras más elaboradas, pero todas \ bien explicadas y con vídeo incluido.' return render_template('search/cooks.html', title='Cocineros', cook_form=form, all_usernames=utils.all_usernames(), cooks=cooks, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=utils.get_cook_keywords())
def login(): if current_user.is_authenticated: return redirect(url_for('main.get_home')) form = LoginForm() url = utils.get_url() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None: flash('El email no es correcto. Vuelve a intentarlo.', 'danger') elif user and bcrypt.check_password_hash(user.pw, form.password.data): login_user(user, remember=form.remember.data) return redirect(url) flash('La contraseña es incorrecta. Vuelve a intentarlo.', 'danger') return render_template('view/login.html', title='Login', form=form, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), url=url)
def search_all_ingredients(): """ Function that searchs for recipes that contain the given ingredient. """ form = IngredientsForm() if form.is_submitted(): ingredient = utils.get_ingredient_by_name(form.ingredient.data) return redirect( url_for('ingredients.search_ingredient', ing_url=ingredient.url)) desc = 'Busca recetas veganas y saludables que contengan un ingrediente. \ Por si tienes algún capricho, o un ingrediente con el que no \ sabes qué hacer.' return render_template('search/ingredient.html', title='Ingredientes', recipe_form=SearchRecipeForm(), form=form, all_ingredients=utils.get_all_ingredients(), recipes=None, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=utils.get_ing_keywords())
def get_veganizer(): form = VeggieForm() if form.validate_on_submit(): flash( f'Gracias {form.name.data}! Te mandaremos un email cuando \ la receta esté online', 'success') send_veggie_msg(form) return redirect(url_for('main.get_home')) else: desc = "Envíanos tus recetas favoritas, y nosotros idearemos versiones \ veganas y saludables, para que tu paladar mantenga el listón y \ las viejas costumbres no desaparezcan" return render_template('view/veganizer.html', title='Veganizador', recipe_form=SearchRecipeForm(), form=form, last_recipes=get_last_recipes(4), description=' '.join(desc.split()), keywords=get_veggie_keywords())
def edit_ingredient(ingredient_url): ingredient = utils.get_ingredient_by_url(ingredient_url) form = IngredientForm(obj=ingredient) if form.validate_on_submit(): errors = list() if form.name.data != ingredient.name: errors = validate.validate_name(form) if len(errors) > 0: for error in errors: flash(error, 'danger') else: update.update_ingredient(ingredient, form) flash("Los cambios han sido guardados.", "success") return redirect(url_for('ingredients.sort_by_date', arg='True')) return render_template("edit/ingredient.html", title="Editar ingrediente", form=form, recipe_form=SearchRecipeForm(), last_recipes=get_last_recipes(4), ingredient=ingredient, is_edit_recipe=True)