Example #1
0
def recipes_create():
    form = RecipeForm(request.form)

    if not form.validate():
        return render_template("recipes/form.html",
                               form=form,
                               form_action=url_for("recipes_create"),
                               button_text="Create recipe")

    recipe = Recipe(form.name.data.strip())
    recipe.servings = form.servings.data
    recipe.instructions = form.instructions.data
    recipe.account_id = current_user.id
    recipe.account_name = current_user.name

    ingredients = form.ingredients.data

    db.session().add(recipe)
    db.session().flush()

    new = create_ingredients(ingredients, recipe.id)

    db.session().commit()

    return redirect(url_for("recipes_view", recipe_id=recipe.id))
Example #2
0
def recipe_create():

    form = RecipeForm(request.form)
    ingredients = Ingredient.query.all()
    form.ingredients.choices = [(ingredient.ingredientid, ingredient.name)
                                for ingredient in ingredients]

    r = Recipe(form.name.data, form.method.data, current_user.id)

    ingredient_ids = form.ingredients.data

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

    db.session().add(r)
    db.session().commit()

    db.session.refresh(r)

    for id in ingredient_ids:
        i = Ingredient.query.get(id)
        r.recipeingredient.append(i)
        db.session().commit()

    return redirect(url_for("recipe_index"))
Example #3
0
def recipes_create():

    form = RecipeForm(request.form)
    form.recipe_id = -1
    # Checking that the form passes validations
    if not form.validate():
        return render_template("recipes/new.html", form=form)

    # Adding the new recipe
    name = form.name.data.strip()
    name = (name[0].upper() + name[1:])
    newRecipe = Recipe(name)
    newRecipe.instruction = form.instruction.data
    newRecipe.preptime = form.preptime.data
    newRecipe.account_id = current_user.id

    # Separating and adding tags
    tagsString = form.tags.data.strip()
    tags = tagsString.split(',')
    Tag.add_tags(tags, newRecipe)

    # Commiting changes
    db.session().add(newRecipe)
    db.session().commit()

    # Ingredients need recipe ID,
    # so they are added only after the recipe is added
    addedRecipe = Recipe.query.filter(Recipe.name == newRecipe.name).first()
    ingredients = form.ingredients.data.splitlines()
    Ingredient.add_ingredients(ingredients, addedRecipe)

    return redirect(url_for("recipes_index"))
Example #4
0
def recipes_save(recipe_id):
    form = RecipeForm(request.form)

    if not form.validate():
        return render_template("recipes/form.html",
                               form=form,
                               form_action=url_for(
                                   "recipes_save", recipe_id=recipe_id),
                               button_text="Save changes")

    recipe = Recipe.query.get(recipe_id)
    recipe.name = form.name.data.strip()
    recipe.servings = form.servings.data
    recipe.instructions = form.instructions.data

    ingredients = form.ingredients

    id_list = {
        ri_form.ingredient_id.data: ri_form.amount.data for ri_form in ingredients}
    ri_list = RecipeIngredient.query.filter_by(recipe_id=recipe_id).all()

    for ingredient in ri_list:
        if str(ingredient.ingredient_id) not in id_list:
            db.session.delete(ingredient)
        else:
            ingredient.amount = id_list.pop(str(ingredient.ingredient_id))

    create_ingredients(
        [item for item in form.ingredients.data if item['ingredient_id'] in id_list], recipe.id)

    db.session.commit()
    return redirect(url_for("recipes_view", recipe_id=recipe.id))
Example #5
0
def recipe_update(recipe_id):
    rToUpdate = Recipe.query.get(recipe_id)

    if rToUpdate.account_id != current_user.id:
        return login_manager.unauthorized()

    form = RecipeForm(request.form)
    form.name.data = rToUpdate.name
    form.ingredients.data = rToUpdate.ingredients
    form.recipetext.data = rToUpdate.recipe_text
    form.tips.data = rToUpdate.tips

    if request.method == "GET":
        return render_template("recipes/editrecipe.html",
                               form=form,
                               recipe=rToUpdate)

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

    form = RecipeForm(request.form)
    rToUpdate.name = form.name.data
    rToUpdate.ingredients = form.ingredients.data
    rToUpdate.recipe_text = form.recipetext.data
    rToUpdate.tips = form.tips.data

    db.session().add(rToUpdate)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #6
0
def recipe_edit(recipeid):
    try:
        recipe = Recipe.query.filter_by(id=recipeid).first()
        RecipeIngredient.query.filter_by(recipe_id=recipeid).delete()
    except:
        flash("Could not connect to database")
        return redirect(url_for("error_page", message="Error connecting to database"))

    form = RecipeForm().new()
    if request.method == "GET":
        form.name.data = recipe.name
        form.instruction.data = recipe.instruction
        return render_template("recipes/edit.html", recipeid=recipeid, form=form)

    if request.method == "POST" and form.validate():
        recipe.name = form.name.data
        recipe.instruction = form.instruction.data
        for data in form.ingredients.data:
            ingredient = Ingredient.query.filter_by(name=data).first()
            recipe.ingredient.append(ingredient)
        try:
            db.session().commit()
        except:
            flash("Problems connecting to database")
            return redirect(url_for("error_page", message="Error connecting to database"))
        return redirect(url_for("recipes_index"))

    return render_template("recipes/edit.html", recipeid=recipeid, form=form)
Example #7
0
def recipe_update(recipe_id):

    r = Recipe.query.get(recipe_id)
    form = RecipeForm(request.form)

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

    r.name = form.name.data
    r.description = form.description.data
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #8
0
def recipe_create():

    form = RecipeForm(request.form)
    r = Recipe(form.name.data, form.description.data)
    r.creator = current_user.id

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

    db.session().add(r)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #9
0
def recipes_create():
    form = RecipeForm(request.form)

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

    r = Recipe(form.name.data, form.text.data)
    r.difficult = form.difficult.data
    r.event = form.event.data
    r.account_id = current_user.id

    db.session().add(r)
    db.session().commit()

    return redirect(url_for("recipes_list"))
Example #10
0
def recipes_create():
    form = RecipeForm(request.form)

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

    recipe = Recipe(form.name.data)
    recipe.description = form.description.data
    recipe.content = form.content.data
    recipe.account_id = current_user.id

    db.session().add(recipe)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #11
0
def recipes_create():
    form = RecipeForm(request.form)

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

    r = Recipe(form.name.data)
    r.favourite = form.favourite.data
    r.account_id = current_user.id
    r.description = cleanstr(form.description.data)

    db.session().add(r)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #12
0
def recipes_update(recipe_id):
    recipe = Recipe.query.get(recipe_id)

    if recipe.account_id != current_user.id:
        return login_manager.unauthorized()

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

    recipe.name = form.name.data
    recipe.description = form.description.data
    recipe.content = form.content.data
    db.session.commit()

    return redirect(url_for("recipes_index"))
Example #13
0
def recipes_edit(recipe_id):
    form = RecipeForm(request.form)
    r = Recipe.query.get(recipe_id)

    if not form.validate():
        return redirect(url_for("recipes_view", recipe_id=recipe_id,
                                form=form))

    r.name = form.name.data
    r.favourite = form.favourite.data
    r.description = cleanstr(form.description.data)
    r.text = cleanstr(form.text.data)

    db.session().commit()

    return redirect(url_for("recipes_view", recipe_id=recipe_id))
Example #14
0
def recipes_create():
    form = RecipeForm(request.form)

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

    nimi = form.nimi.data
    annoksia = form.annoksia.data
    aika = form.aika.data
    ohje = form.ohje.data

    r = Recipe(nimi, annoksia, aika, ohje)

    db.session().add(r)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #15
0
def recipes_save_edit(recipeId):
    form = RecipeForm(request.form)
    
    if not form.validate():
        return render_template("/recipes/edit.html",
                                form=form,
                                recipeId=recipeId)

    recipe = Recipe.query.get(recipeId)
    
    recipe.image = uploadImage(form.image.name) or recipe.image
    recipe.name = form.name.data
    recipe.instructions = form.instructions.data
    
    db.session.commit()

    return redirect(url_for("recipes_ingredients", recipeId=recipeId))
Example #16
0
def recipes_create():
    form = RecipeForm(request.form)

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

    name = form.name.data
    duration = form.duration.data
    instructions = form.instructions.data

    recipe = Recipe(name, duration, instructions)
    recipe.account_id = current_user.id
    recipe.tags = get_tags(form)

    db.session().add(recipe)
    db.session().commit()

    return redirect(url_for("recipes_index"))
Example #17
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"))
Example #18
0
def recipes_create():
    form = RecipeForm(request.form)
    form.ingredients.choices = [(ingredient.id, ingredient.name)
                                for ingredient in Ingredient.query.all()]

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

    ingredients = [Ingredient.query.get(id) for id in form.ingredients.data]
    r = Recipe(form.name.data, form.text.data)
    r.difficult = form.difficult.data
    r.event = form.event.data
    r.account_id = current_user.id
    r.recipeingredients = ingredients

    db.session().add(r)
    db.session().commit()

    return redirect(url_for("recipes_list"))
Example #19
0
def recipes_create():
    if request.method == "GET":
        return render_template("recipes/new.html", form=RecipeForm())

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

    url = uploadImage(form.image.name)
    
    r = Recipe(form.name.data, form.instructions.data, url)
    r.account_id = current_user.id

    db.session().add(r)
    db.session().flush()
    recipeId = r.id

    db.session().commit()
    
    return redirect(url_for("recipes_ingredients", recipeId=recipeId))
Example #20
0
def recipe_create():
    form = RecipeForm(request.form)
    if not form.validate():
        return render_template("recipes/newrecipe.html", form=form)

    newrecipe = Recipe(form.name.data, form.ingredients.data,
                       form.recipetext.data, form.tips.data)

    newrecipe.account_id = current_user.id

    db.session().add(newrecipe)
    db.session.commit()

    for category in form.categories.data:
        newrecipecategory = RecipeCategory(newrecipe.id, category.id)
        db.session.add(newrecipecategory)

    db.session.commit()

    return redirect(url_for("recipes_index"))
Example #21
0
def recipes_create():
    form = RecipeForm().new()
    if request.method == "GET":
        return render_template("recipes/new.html", form=form)

    if request.method == "POST" and form.validate():
        try:
            r = Recipe(form.name.data, form.instruction.data)
            r.account.append(current_user)

            for data in form.ingredients.data:
                ingredient = Ingredient.query.filter_by(name=data).first()
                r.ingredient.append(ingredient)
            db.session().add(r)
            db.session().commit()
            return redirect(url_for("recipes_index"))
        except:
            flash("could not connect to database")
            return redirect(url_for("error_page", message="Error connecting to database"))

    return render_template("recipes/new.html", form=form)
Example #22
0
def recipe_edit(recipe_id):
    form = RecipeForm(request.form)

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

    recipe = Recipe.query.get(recipe_id)

    if recipe.account_id != current_user.id:
        abort(403)

    recipe.name = form.name.data
    recipe.duration = form.duration.data
    recipe.instructions = form.instructions.data
    recipe.tags = get_tags(form)

    db.session().add(recipe)
    db.session().commit()

    return redirect(url_for("recipe_view", recipe_id=recipe.id))
Example #23
0
def recipes_create():

    form = RecipeForm(request.form)

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

    r = Recipe(form.name.data, form.timeNeeded.data, form.instructions.data)
    r.account_id = current_user.id

    db.session().add(r)
    db.session().flush()

    if form.ingredient_1.data and form.quantity_1.data:
        ing = form.ingredient_1.data
        recipeing = RecipeIngredient(form.quantity_1.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_2.data and form.quantity_2.data:
        ing = form.ingredient_2.data
        recipeing = RecipeIngredient(form.quantity_2.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_3.data and form.quantity_3.data:
        ing = form.ingredient_3.data
        recipeing = RecipeIngredient(form.quantity_3.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_4.data and form.quantity_4.data:
        ing = form.ingredient_4.data
        recipeing = RecipeIngredient(form.quantity_4.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_5.data and form.quantity_5.data:
        ing = form.ingredient_5.data
        recipeing = RecipeIngredient(form.quantity_5.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_6.data and form.quantity_6.data:
        ing = form.ingredient_6.data
        recipeing = RecipeIngredient(form.quantity_6.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_7.data and form.quantity_7.data:
        ing = form.ingredient_7.data
        recipeing = RecipeIngredient(form.quantity_7.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_8.data and form.quantity_8.data:
        ing = form.ingredient_8.data
        recipeing = RecipeIngredient(form.quantity_8.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_9.data and form.quantity_9.data:
        ing = form.ingredient_9.data
        recipeing = RecipeIngredient(form.quantity_9.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    if form.ingredient_10.data and form.quantity_10.data:
        ing = form.ingredient_10.data
        recipeing = RecipeIngredient(form.quantity_10.data)

        recipeing.recipe_id = r.id
        recipeing.ingredient_id = ing.get_id()
        db.session().add(recipeing)

    db.session().commit()

    return redirect(url_for("recipes_index"))