Пример #1
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"))
Пример #2
0
def recipe_delete(recipe_id):
    recipe = Recipe.query.get(recipe_id)
    if ((recipe.account_id is not current_user.get_id())
            and (current_user.get_role() != 'admin')):

        return abort(401)

    Ingredient.find_recipe_ingredients(recipe).delete()
    db.session.delete(recipe)
    db.session.commit()
    return redirect(url_for("recipes_index"))
Пример #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"))
Пример #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"))
Пример #5
0
def create_ingredients(ingredients, recipe_id):
    new_ingredients = []

    for i in ingredients:
        ingredient_id = None

        if i['ingredient_id'] == None:
            existing_name = Ingredient.query.filter_by(
                name=i['ri_name'].lower().strip()).first()

            if existing_name:
                ingredient_id = existing_name.id
                i['ri_name'] = existing_name.name

            else:
                new = Ingredient(
                    name=i['ri_name'].strip(),
                    unit=i['unit'],
                    account_id=current_user.id)
                db.session().add(new)
                db.session().flush()
                ingredient_id = new.id
                new_ingredients.append(new)

        else:
            ingredient_id = i['ingredient_id']

        ri = RecipeIngredient(
            recipe_id=recipe_id,
            ingredient_id=ingredient_id,
            amount=i['amount'],
            unit=i['unit'])
        db.session().add(ri)

    return new_ingredients
def show_statistics():
    user_recipes = User.users_recipes()
    most_used = Ingredient.most_used()

    return render_template("statistics/statistics_page.html",
                           user_recipes=user_recipes,
                           most_used=most_used)
Пример #7
0
def recipes_ingredient_statistics():
    ingredientCount = Recipe.count_ingredient_recipe()
    ingredients = Ingredient.count_ingredient_usage()

    return render_template("stats.html",
                           ingredientCount=ingredientCount,
                           ingredients=ingredients)
Пример #8
0
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"))
Пример #9
0
def recipe_edit(recipe_id):

    # POST is not accepted if current user is not the creator of the recipe
    # or an administrator
    recipe = Recipe.query.get(recipe_id)
    if ((recipe.account_id is not current_user.get_id())
            and (current_user.get_role() != 'admin')):

        return abort(401)

    form = RecipeEditForm(request.form)

    # If form does not pass validations,
    # a new faulty form is created to be shown along with error messages,
    # but never put to the database
    if not form.validate():
        faultyRecipe = Recipe(request.form['name'])
        faultyRecipe.id = recipe_id
        faultyRecipe.instruction = request.form['instruction']
        faultyRecipe.preptime = request.form.get("preptime")
        faultyIngredients = request.form.get("ingredients")
        faultyTags = request.form.get("tags")
        return render_template("recipes/edit.html",
                               recipe=faultyRecipe,
                               form=form,
                               tags=faultyTags,
                               ingredients=faultyIngredients)

    # Fetching and editing the recipe
    changedRecipe = Recipe.query.get(recipe_id)
    name = request.form.get("name").strip()
    name = name[0].upper() + name[1:]
    changedRecipe.name = name
    changedRecipe.instruction = request.form.get("instruction")
    changedRecipe.preptime = request.form.get("preptime")

    # Add tags for the recipe
    tags = form.tags.data.split(',')
    Tag.add_tags(tags, changedRecipe)

    db.session().commit()

    ingredients = request.form.get("ingredients").splitlines()
    Ingredient.add_ingredients(ingredients, changedRecipe)

    return redirect(url_for("recipes_index"))
Пример #10
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"))
Пример #11
0
def go_to_statistics():

    recipesPerUser = Recipe.list_how_many_recipes_per_user()

    ingredientsPerRecipe = Ingredient.list_ingredients_per_recipe()

    return render_template("statistics/statistics.html",
                           recipesPerUser=recipesPerUser,
                           ingredientsPerRecipe=ingredientsPerRecipe)
Пример #12
0
def ingredient_create():
    form = IngredientForm(request.form)

    ingredient = Ingredient(form.name.data)

    db.session().add(ingredient)
    db.session().commit()

    return redirect(url_for("index"))
Пример #13
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"))
Пример #14
0
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"))
Пример #15
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"))
Пример #16
0
def ingredients_create():
    form = IngredientsForm(request.form)

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

    ingre = Ingredient(form.name.data, form.details.data)
    db.session().add(ingre)
    db.session().commit()

    return redirect(url_for("ingredients_index"))
Пример #17
0
def recipes_create():

    if request.method == "GET":
        form = NewRecipeForm()
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        form.ingredients.append_entry({})
        return render_template("recipes/new.html", form=form)

    form = NewRecipeForm(request.form)

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

    recipe = Recipe(form.header.data, form.category.data,
                    form.description.data, form.directions.data)
    recipe.account_id = current_user.id

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

    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)

    db.session().commit()

    return redirect(url_for("recipes_index", user_id=recipe.account_id))
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"))
Пример #19
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)
Пример #20
0
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"))
Пример #21
0
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"))
Пример #22
0
def recipe_editform(recipe_id):
    fetched_recipe = Recipe.query.get(recipe_id)
    fetched_tags = Recipe.find_recipe_tags(fetched_recipe)
    joined_tags = ""

    tags_length = len(fetched_tags)
    for i in range(tags_length):
        joined_tags += fetched_tags[i][1]
        if i < tags_length - 1:
            joined_tags += ', '

    fetched_ingredients = Ingredient.find_recipe_ingredients(fetched_recipe)
    joined_ingredients = ""
    for ingredient in fetched_ingredients:
        joined_ingredients += ingredient.line + "\n"

    return render_template("recipes/edit.html",
                           recipe=fetched_recipe,
                           form=RecipeEditForm(),
                           tags=joined_tags,
                           ingredients=joined_ingredients)
Пример #23
0
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"))
Пример #24
0
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))
Пример #25
0
def ingredient_index():
    return render_template("ingredients/list.html",
                           new_form=IngredientForm(),
                           edit_form=IngredientEditForm(),
                           ingredients=Ingredient.find_by_user(current_user.id))