Ejemplo n.º 1
0
def edit_recipe(recipe_name):
    recipe = Recipe.query.filter_by(name=recipe_name).first_or_404()
    if current_user.is_authenticated and current_user == recipe.author:
        form = form_ingredients_and_allergens(
            RecipeForm(obj=recipe, original_name=recipe.name))
        if form.validate_on_submit() and form.validate_recipe_name:
            f = form.image.data
            filename = secure_filename(f.filename)
            file_path = os.path.join("app/static/img/recipes_images", filename)
            f.save(file_path)
            recipe.name = "non-existent"
            created_recipe = Recipe(
                name=form.name.data,
                short_description=form.short_description.data,
                content=form.content.data,
                cuisine=form.cuisine.data,
                category=form.category.data,
                time_to_prepare=form.time_to_prepare.data,
                serves_num_people=form.serves_num_people.data,
                cooking_time=form.cooking_time.data,
                image="static/img/recipes_images/" + filename,
                author=current_user,
                calories=form.calories.data,
                carbohydrates=form.carbohydrates.data,
                proteins=form.proteins.data,
                fats=form.fats.data,
                cholesterol=form.cholesterol.data)

            ingredients_in_recipe = []
            for ingredient in form.ingredients.data:
                queried_ingredient = Ingredient.query.filter_by(
                    id=ingredient).first()
                ingredients_in_recipe.append(queried_ingredient)
            created_recipe._ingredients = ingredients_in_recipe

            allergens_in_recipe = []
            for allergen in form.allergens.data:
                queried_allergen = Allergen.query.filter_by(
                    id=allergen).first()
                allergens_in_recipe.append(queried_allergen)
            created_recipe._allergens = allergens_in_recipe
            try:
                db.session.query(Recipe).filter(
                    Recipe.id == recipe.id).delete()
                db.session.commit()

                db.session.add(created_recipe)
                db.session.commit()
            except:
                print("There was an error while saving changes.")
            flash("Congrats, you have edited a recipe!")
            return redirect(url_for('recipes_list'))
    else:
        flash("You need to login before you edit recipe.")
        return redirect(url_for('login'))
    return render_template("edit_recipe.html",
                           title='Edit Recipe',
                           form=form,
                           recipe=recipe)
Ejemplo n.º 2
0
def add_recipe():
    if current_user.is_authenticated:
        form = form_ingredients_and_allergens(RecipeForm(original_name=""))
        if form.validate_on_submit():
            f = form.image.data
            print(form.image.data)
            filename = secure_filename(f.filename)
            file_path = os.path.join("app/static/img/recipes_images", filename)
            f.save(file_path)

            recipe = Recipe(name=form.name.data,
                            short_description=form.short_description.data,
                            content=form.content.data,
                            cuisine=form.cuisine.data,
                            category=form.category.data,
                            time_to_prepare=form.time_to_prepare.data,
                            serves_num_people=form.serves_num_people.data,
                            cooking_time=form.cooking_time.data,
                            image="static/img/recipes_images/" + filename,
                            author=current_user,
                            calories=form.calories.data,
                            carbohydrates=form.carbohydrates.data,
                            proteins=form.proteins.data,
                            fats=form.fats.data,
                            cholesterol=form.cholesterol.data)

            ingredients_in_recipe = []
            for ingredient in form.ingredients.data:
                queried_ingredient = Ingredient.query.filter_by(
                    id=ingredient).first()
                ingredients_in_recipe.append(queried_ingredient)
            recipe._ingredients = ingredients_in_recipe

            allergens_in_recipe = []
            for allergen in form.allergens.data:
                queried_allergen = Allergen.query.filter_by(
                    id=allergen).first()
                allergens_in_recipe.append(queried_allergen)

            recipe._allergens = allergens_in_recipe
            try:
                db.session.add(recipe)
                db.session.commit()
            except:
                print("There was a DB error while saving Recipe.")
            print("Recipe added")

            flash("Congrats, you have added a recipe!")
            return redirect(url_for('recipe', recipe_name=form.name.data))
        else:
            print("Something has failed")

    else:
        flash("You need to login before you add recipe.")
        return redirect(url_for('login'))
    return render_template("add_recipe.html", title='Add Recipe', form=form)