def ingredient_update(id): updated_ingredientUser = IngredientUser.query.get((id, current_user.id)) query_type = request.args.get("type") if query_type == "delete": db.session.delete(updated_ingredientUser) db.session.commit() else: update_form = IngredientEditForm(request.form) # Return site with errors if form validation check doesnt work if not update_form.validate: return render_template("ingredients/list.html", new_form=IngredientForm(), edit_form=IngredientEditForm(), ingredients=Ingredient.find_by_user( current_user.id), new_error=update_form.errors.items()) updated_ingredientUser.amount = request.form.get("amount") db.session().commit() return redirect(url_for("ingredient_index"))
def ingredient_form(): new_form = IngredientForm(request.form) if not new_form.validate: return render_template("ingredients/list.html", new_form=IngredientForm(), edit_form=IngredientEditForm(), ingredients=Ingredient.find_by_user( current_user.id), new_error=new_form.errors.items()) new_ingredient = Ingredient(request.form.get( "name"), request.form.get("measurement_unit")) # add ingredient to foreing table db.session.add(new_ingredient) db.session.commit() new_ingredient_user = IngredientUser( new_ingredient.id, current_user.id, request.form.get("amount")) db.session.add(new_ingredient_user) db.session.commit() return redirect(url_for("ingredient_index"))
def ingredient_listing(): form_and_value_tuples = [] for ingredient in Ingredient.query.all(): form = IngredientAddForm() form.id = ingredient.id form.amount.name = "amount-{}".format(ingredient.id) form_and_value_tuples.append((ingredient, form)) if request.method == "GET": return render_template("ingredients/all_list.html", add_forms=form_and_value_tuples) else: for selected_ingredient in request.form.getlist("selected"): if any(listing.get("id") == int(selected_ingredient) for listing in Ingredient.find_by_user(current_user.id)): return render_template("ingredients/all_list.html", add_forms=form_and_value_tuples, errors=["Can't add ingredients you already have"]) else: newListing = IngredientUser(selected_ingredient, current_user.id, request.form.get( "amount-{}".format(selected_ingredient))) db.session.add(newListing) db.session.commit() return redirect(url_for("ingredient_index"))
def recipe_new(): if request.method == "GET": form = RecipeForm() for ingredient in Ingredient.find_by_user(current_user.id): form.ingredients.choices.append((ingredient.get("id"), ingredient.get("name"))) form.process() return render_template("recipe/new_recipe.html", recipe_form=form) else: new_recipe = Recipe(request.form.get("name"), request.form.get("instructions")) db.session.add(new_recipe) db.session.commit() print(request.form.getlist("ingredients")) for ingredient_id in request.form.getlist("ingredients"): new_RecipeIngredient = RecipeIngredient( new_recipe.id, ingredient_id) new_RecipeIngredient.amount = request.form.get( "amount-" + ingredient_id) db.session.add(new_RecipeIngredient) db.session.commit() return redirect(url_for("recipe_index"))
def ingredient_index(): return render_template("ingredients/list.html", new_form=IngredientForm(), edit_form=IngredientEditForm(), ingredients=Ingredient.find_by_user(current_user.id))