def ingredient_editform(ingredient_id): ingredient = Ingredient.query.get(ingredient_id) form = IngredientForm() form.name.data = ingredient.name form.unit.data = ingredient.unit form.id = ingredient.id return render_template("ingredients/editraw.html", form=form)
def ingredient_create(): form = IngredientForm(request.form) if not form.validate(): return render_template("ingredient/new_ingredient.html", form=IngredientForm()) ingredient = Ingredient(form.name.data) db.session().add(ingredient) db.session().commit() return redirect(url_for("ingredients_list"))
def ingredient_update(ingredient_id): ingredient = Ingredient.query.get(ingredient_id) form = IngredientForm(request.form) if not form.validate(): return render_template("ingredients/editraw.html/", form=form) ingredient.name = form.name.data ingredient.unit = form.unit.data db.session().commit() return redirect(url_for("ingredients_index"))
def ingredient_create(): form = IngredientForm(request.form) ingredient = Ingredient(form.name.data, form.unit.data) ingredient.creator = current_user.id if not form.validate(): return render_template("ingredients/newraw.html", form=form) db.session().add(ingredient) db.session().commit() return redirect(url_for("ingredients_index"))
def ingredients_create(): form = IngredientForm(request.form) if not form.validate(): return render_template("ingredients/new.html", form=form) i = Ingredient(form.name.data) i.account_id = current_user.id i.unit = form.unit.data db.session().add(i) db.session().commit() return redirect(url_for("ingredients_index"))
def ingredients_edit(ingredient_id): form = IngredientForm(request.form) i = Ingredient.query.get(ingredient_id) if not form.validate(): return render_template("ingredients/" + ingredient_id, form=form) i.name = form.name.data i.account_id = current_user.id i.unit = form.unit.data db.session().commit() return redirect(url_for("ingredients_index"))
def ingredient_lisatiedot(): form = IngredientForm(request.form) id = session["ingredientid"] ingre = Ingredient.query.get(id) ingre.details = form.details.data db.session().commit() return redirect(url_for("ingredients_index"))
def ingredients_create(): form = IngredientForm(request.form) i = Ingredient(form.name.data) if current_user.is_admin(): i.accepted = True set_message("ainesosa lisätty") else: i.accepted = False set_message("kiitos ehdotuksestasi!") db.session().add(i) db.session().commit() form = IngredientForm() return redirect(url_for("ingredients_form"))
def ingredient_create(): form = IngredientForm(request.form) ingredient = Ingredient(form.name.data) db.session().add(ingredient) db.session().commit() return redirect(url_for("index"))
def ingredients_index(): return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=IngredientForm(), form_action=url_for("ingredients_create"), button_text="Add", action="Add an ingredient", account_id=current_user.id)
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 ingredients_create(): form = IngredientForm(request.form) if not form.validate(): return render_template("ingredients/new.html", form=form) x = db.session.query(Ingredient).filter( Ingredient.name == form.name.data).one_or_none() if x is not None: flash("Ingredient you are trying add already exist", "warning") else: flash("Ingredient has been added", "success") t = Ingredient(form.name.data) t.account_id = current_user.id db.session().add(t) db.session().commit() return redirect(url_for("ingredients_create"))
def ingredients_save(ingredient_id): ingredient = Ingredient.query.get(ingredient_id) if ingredient.account_id != current_user.id: abort(403) form = IngredientForm(request.form, obj=ingredient) if not form.validate(): return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=form, form_action=url_for( "ingredients_save", ingredient_id=ingredient_id), button_text="Save changes", action="Edit an ingredient", account_id=current_user.id) form.populate_obj(ingredient) try: db.session().commit() return redirect(url_for("ingredients_index")) except IntegrityError as error: db.session.rollback() return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=form, form_action=url_for( "ingredients_save", ingredient_id=ingredient_id), db_error="Ingredient name already exists.", button_text="Save changes", action="Edit an ingredient", account_id=current_user.id)
def ingredients_create(): form = IngredientForm(request.form) if not form.validate(): return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=form, form_action=url_for("ingredients_create"), button_text="Add", action="Add an ingredient", account_id=current_user.id) i = Ingredient(name=form.name.data, category=form.category.data, unit=form.unit.data, account_id=current_user.id) kcal = form.kcal.data i.kcal = kcal try: db.session().add(i) db.session().commit() return redirect(url_for("ingredients_index")) except IntegrityError as error: db.session.rollback() return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=form, form_action=url_for("ingredients_create"), db_error="Ingredient name already exists.", button_text="Add", action="Add an ingredient", account_id=current_user.id)
def ingredients_form(): page = request.args.get('page', 1, type=int) ingredients = Ingredient.query.filter_by(accepted=True).paginate( page, 5, False) next_url = url_for('ingredients_form', page = ingredients.next_num) \ if ingredients.has_next else None prev_url = url_for('ingredients_form', page = ingredients.prev_num) \ if ingredients.has_prev else None return render_template("ingredients/new.html", message=get_message(), ingredients=ingredients.items, next_url=next_url, prev_url=prev_url, form=IngredientForm())
def ingredients_edit(ingredient_id): ingredient = Ingredient.query.get(ingredient_id) if ingredient.account_id != current_user.id: abort(403) form = IngredientForm(obj=ingredient) return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=form, form_action=url_for("ingredients_save", ingredient_id=ingredient_id), button_text="Save changes", action="Edit an ingredient", account_id=current_user.id)
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 ingredients_delete(ingredient_id): i = Ingredient.query.get(ingredient_id) if i.account_id != current_user.id: abort(403) r_i = RecipeIngredient.query.filter_by(ingredient_id=i.id).all() if r_i: return render_template("ingredients/list.html", ingredients=Ingredient.query.filter_by( account_id=current_user.id).order_by( func.lower(Ingredient.name)).all(), form=IngredientForm(), form_action=url_for("ingredients_create"), db_error="Ingredient is used in a recipe.", button_text="Add", action="Add an ingredient", account_id=current_user.id) else: db.session.delete(i) db.session.commit() return redirect(url_for("ingredients_index"))
def recipes_edit(recipe_id): recipe = Recipe.query.get(recipe_id) recipeIngredients = RecipeIngredient.query.filter_by( recipe_id=recipe.id).all() if request.method == "GET": form = EditRecipeForm() form.header.data = recipe.header form.category.data = recipe.category form.description.data = recipe.description for recipeIngredient in recipeIngredients: ingredientForm = IngredientForm() ingredient = Ingredient.query.get(recipeIngredient.ingredient_id) ingredientForm.ingredientName = ingredient.name ingredientForm.ingredientAmount = recipeIngredient.amount ingredientForm.ingredientUnit = recipeIngredient.unit form.ingredients.append_entry(ingredientForm) form.ingredients.append_entry({}) form.ingredients.append_entry({}) form.directions.data = recipe.directions return render_template("recipes/edit.html", recipe=recipe, form=form) form = EditRecipeForm(request.form) if not form.validate(): return render_template("recipes/edit.html", recipe=recipe, form=form) recipe.header = form.header.data recipe.category = form.category.data recipe.description = form.description.data for recipeIngredient in recipeIngredients: db.session().delete(recipeIngredient) for ingredientForm in form.ingredients.data: if ingredientForm['ingredientName']: ingredient = Ingredient.query.filter_by( name=ingredientForm['ingredientName'].lower()).first() if not ingredient: ingredient = Ingredient( ingredientForm['ingredientName'].lower()) db.session().add(ingredient) db.session().flush() recipeIngredient = RecipeIngredient( ingredientForm['ingredientAmount'], ingredientForm['ingredientUnit']) recipeIngredient.recipe_id = recipe.id recipeIngredient.ingredient_id = ingredient.id db.session().add(recipeIngredient) recipe.directions = form.directions.data db.session().commit() return redirect(url_for("recipes_index", user_id=recipe.account_id))
def singredient_index(ingredient_id): session["ingredientid"] = ingredient_id return render_template("ingredients/id.html", form=IngredientForm(), ingredient=Ingredient.query.get(ingredient_id))
def ingredient_form(): return render_template("ingredient/new_ingredient.html", form=IngredientForm())
def ingredient_index(): return render_template("ingredients/list.html", new_form=IngredientForm(), edit_form=IngredientEditForm(), ingredients=Ingredient.find_by_user(current_user.id))
def ingredients_view(ingredient_id): r = Ingredient.query.get(ingredient_id) f = IngredientForm(obj=Ingredient.query.get(ingredient_id)) return render_template("ingredients/ingredient.html", form=f, r=r)
def ingredients_form(): return render_template("ingredients/new.html", form=IngredientForm())