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"))
Ejemplo n.º 2
0
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"))
Ejemplo n.º 3
0
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"))
Ejemplo n.º 4
0
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"))
Ejemplo n.º 5
0
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"))
Ejemplo n.º 6
0
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"))
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)