Beispiel #1
0
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
    )
Beispiel #2
0
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))
Beispiel #3
0
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
    )
Beispiel #4
0
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)
Beispiel #5
0
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)
Beispiel #6
0
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())
Beispiel #7
0
def edit_recipe(recipe_url, state=None):
    """Edit a finished recipe, or one that is supposed to be so. The state
    argument is used by edit_new_recipe(), where the stored state is still 1,
    in case the recipe is not valid. The given state populates the form and
    only enters the DB if the recipe is valid. 
    State is set to 'edit' when the RecipeForm is posted and the validation
    should proceed, instead of redirecting to edit_new_recipe. """
    recipe = utils.get_recipe_with_id(recipe_url, current_user.id)
    if state is None and recipe.id_state == 1:  # unfinished recipe
        return redirect(
            url_for('recipes.edit_new_recipe', recipe_url=recipe_url))
    form = utils.prepare_form(RecipeForm(obj=recipe), recipe)
    if state is not None and state != 'edit':  # Add new state to form.
        form.state.process_data(int(state))
        form.validate()
    if form.validate_on_submit():
        errors = validate.validate_name(form, recipe) + \
            validate.validate_edition(form)
        if len(errors) == 0:
            flash('Los cambios han sido guardados.', 'success')
            return redirect(
                url_for('recipes.get_recipe',
                        username=current_user.username,
                        recipe_url=update.update_recipe(recipe, form)))
        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),
        state=state)