Exemplo n.º 1
0
def add_recipe_ingredient(cuisine_name, recipe_name):
    current_user = User.get_by_id(get_jwt_identity())

    if current_user.is_admin:
        cuisine = Cuisine.get_or_none(name=cuisine_name)
        if cuisine:
            recipe = Recipe.get_or_none(name=recipe_name)
            if recipe:
                if recipe.cuisine_id == cuisine.id:
                    quantity = request.form.get("quantity")
                    name = request.form.get("name")
                    recipe_ingredient = RecipeIngredient(name=name,
                                                         quantity=quantity,
                                                         recipe_id=recipe.id)
                    recipe_ingredient.save()
                    return jsonify({
                        "message":
                        "Ingredient has been successfully created!"
                    })
                else:
                    return jsonify({
                        "errors":
                        recipe_name + " is not found in " + cuisine_name +
                        " cuisine."
                    })
            else:
                return jsonify({"errors": "Recipe not exists"})
        else:
            return jsonify({"errors": cuisine_name + " cusine is not exist"})
    else:
        return 0
Exemplo n.º 2
0
def choose_ingredient(recipe_id):
    recipe = Recipe.get_or_none(Recipe.id == recipe_id)
    ingredients = RecipeIngredient.select().where(
        RecipeIngredient.recipe_id == recipe.id)
    return render_template('recipes/choose_ingredient.html',
                           ingredients=ingredients,
                           recipe=recipe)
Exemplo n.º 3
0
def show_single_recipe(id):
    recipe = Recipe.get_by_id(id)
    step = Step.select().where(Step.recipe_id == id)
    recipe_ingredient = RecipeIngredient.select().where(
        RecipeIngredient.recipe_id == id)
    if recipe:

        step_data = []
        for s in step:
            data = {"number": s.number, "description": s.description}
            step_data.append(data)

        ingredient_data = []
        for i in recipe_ingredient:
            data = {"name": i.name}
            ingredient_data.append(data)

        results = {
            "id": recipe.id,
            "name": recipe.name,
            "image": recipe.image,
            "step": step_data,
            "ingredient": ingredient_data
        }
        return jsonify({"data": results})
Exemplo n.º 4
0
def delete_recipe_ingredient(cuisine_name, recipe_name):
    current_user = User.get_by_id(get_jwt_identity())

    if current_user.is_admin:
        cuisine = Cuisine.get_or_none(name=cuisine_name)
        if cuisine:
            recipe = Recipe.get_or_none(name=recipe_name)
            if recipe:
                if recipe.cuisine_id == cuisine.id:
                    name = request.form.get("name")
                    delete = RecipeIngredient.delete().where(
                        RecipeIngredient.name == name,
                        RecipeIngredient.recipe_id == recipe.id)
                    delete.execute()
                    return jsonify({
                        "message":
                        "Ingredient has been successfully deleted!"
                    })
                else:
                    return jsonify({
                        "errors":
                        recipe_name + " is not found in " + cuisine_name +
                        " cuisine."
                    })
            else:
                return jsonify({"errors": "Recipe not exists"})
        else:
            return jsonify({"errors": cuisine_name + " cusine is not exist"})
    else:
        return 0
Exemplo n.º 5
0
def index():
    recipe_ingredients = RecipeIngredient.select()
    return jsonify([{
        "id": r.id,
        "recipe_id": r.recipe_id.id,
        "ingredient_name": r.ingredient_id.ingredient_name,
        "ingredient_id": r.ingredient_id.id,
        "measurement_id": r.measurement_id.id,
        "amount": r.amount
    } for r in recipe_ingredients])
Exemplo n.º 6
0
def recipe(recipe_id):
    recipe_ingredients = RecipeIngredient.select().where(
        RecipeIngredient.recipe_id == recipe_id)
    return jsonify([{
        "id": r.id,
        "recipe_id": r.recipe_id.id,
        "ingredient_name": r.ingredient_id.ingredient_name,
        "ingredient_id": r.ingredient_id.id,
        "measurement_id": r.measurement_id.id,
        "amount": r.amount
    } for r in recipe_ingredients])
Exemplo n.º 7
0
def _populate_recipes(db_engine):
    session_maker = sessionmaker(bind=db_engine)
    session = session_maker()
    session.query(Recipe).delete()
    session.query(RecipeIngredient).delete()

    # Basic Pickaxe
    basic_pickaxe = Recipe(id=1, name='Basic Pickaxe')
    session.add(basic_pickaxe)
    session.query(Recipe).delete()
    stone_item = session.query(Item).filter(Item.name == 'stone').first()
    stick_item = session.query(Item).filter(Item.name == 'stick').first()

    basic_pickaxe_ingredient_1 = RecipeIngredient(recipe_id=basic_pickaxe.id,
                                                  item=stone_item,
                                                  item_amount=7)
    session.add(basic_pickaxe_ingredient_1)
    basic_pickaxe_ingredient_2 = RecipeIngredient(recipe_id=basic_pickaxe.id,
                                                  item=stick_item,
                                                  item_amount=4)
    session.add(basic_pickaxe_ingredient_2)

    session.commit()
    return
Exemplo n.º 8
0
def show_recipe_ingredient(cuisine_name, recipe_name):
    cuisine = Cuisine.get_or_none(name=cuisine_name)

    if cuisine:
        recipe = Recipe.get_or_none(name=recipe_name)
        if recipe:
            if recipe.cuisine_id == cuisine.id:
                ingredient = RecipeIngredient.select().where(
                    RecipeIngredient.recipe_id == recipe.id)
                results = []
                for i in ingredient:
                    ingredient_data = {"name": i.name, "quantity": i.quantity}
                    results.append(ingredient_data)
                return jsonify({"data": results})
            else:
                return jsonify({
                    "errors":
                    recipe_name + " is not found in " + cuisine_name +
                    " cuisine."
                })
        else:
            return jsonify({"errors": "Recipe not exists"})
    else:
        return jsonify({"errors": cuisine_name + " cusine is not exist"})