Exemplo n.º 1
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"))
Exemplo n.º 2
0
def ingredients_create():
    form = NewIngredientForm(request.form)

    if not form.validate():
        return render_template("ingredients/new.html", form=form)

    name = str(form.name.data).capitalize()
    i = Ingredient(name, form.unit.data)

    if current_user is not None:
        i.account_id = current_user.id
        if current_user.role.name == "USER+" or current_user.role.name == "ADMIN":
            i.accepted = True

    db.session().add(i)
    db.session().commit()

    return redirect(url_for("ingredients_index"))
Exemplo n.º 3
0
def recipes_create():
    form = RecipeForm(request.form)

    if not form.validate():
        return render_template("recipes/new.html", form=form)

    r = db.session.query(Recipe).filter(
        Recipe.name == form.name.data).one_or_none()

    if r is not None:
        flash("Recipe you are trying to add already exists", "warning")
    elif form.name.data.__len__() < 2:
        flash("Recipe name has to be longer than 1 character", "warning")
    else:
        flash("Recipe has been added", "success")
        t = Recipe(form.name.data)
        t.account_id = current_user.id
        db.session().add(t)

        ingredients_string = form.ingredientString.data

        ingredients = [x.strip() for x in ingredients_string.split(',')]

        for ingredient in ingredients:
            x = db.session.query(Ingredient).filter(
                Ingredient.name == ingredient).one_or_none()

            if not x:
                if ingredient.__len__() < 2:
                    flash("Ingredient name has to be longer than 1 character",
                          "warning")
                else:
                    x = Ingredient(ingredient)
                    x.account_id = current_user.id
                    db.session().add(x)

            t.recipeIngredients.append(x)

    try:
        db.session().commit()
    except Exception as e:
        print(str(e))

    return redirect(url_for("recipes_create"))
Exemplo n.º 4
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"))