Exemplo n.º 1
0
def verify_recipe():
    """ Verify if inventory has enough of the recipe's ingredients to cook."""

    recipe_id = request.form.get("data")

    recipe_details = recipe_info_by_id(int(recipe_id))

    # First check if ALL ingredients are sufficient to make recipe
    for ingredient in recipe_details['extendedIngredients']:
        # Look up ingredient in inventory table
        check_ingredient = Inventory.query.filter(
            Inventory.ingredient_id == int(ingredient['id']),
            Inventory.user_id == session['user_id']).first()

        if not check_ingredient:
            return jsonify({'result': False})
        # If unit is not the same as the base unit in inventory table, convert the unit to base unit
        if ingredient['unitLong'] != check_ingredient.ingredients.base_unit:
            (converted_amount, converted_unit) = convert_to_base_unit(
                float(ingredient['amount']), ingredient['unitLong'])

            # Check if current inventory has enough for the recipe
            if check_ingredient.current_quantity < converted_amount:
                return jsonify({'result': False})
        else:
            if check_ingredient.current_quantity < round(
                    float(ingredient['amount']), 2):
                return jsonify({'result': False})

    # If ALL ingredients are sufficient, subtract quantities
    for ingredient in recipe_details['extendedIngredients']:
        update_ingredient = Inventory.query.filter(
            Inventory.ingredient_id == int(ingredient['id']),
            Inventory.user_id == session['user_id']).one()

        if ingredient['unitLong'] != update_ingredient.ingredients.base_unit:
            (converted_amount, converted_unit) = convert_to_base_unit(
                float(ingredient['amount']), ingredient['unitLong'])
            # Subtract recipe amount from inventory amount
            update_ingredient.current_quantity -= converted_amount
        else:
            update_ingredient.current_quantity -= round(
                float(ingredient['amount']), 2)

    # Update user_recipe status to 'cooked'
    cooked_recipe = UserRecipe.query.filter(
        UserRecipe.recipe_id == int(recipe_id),
        UserRecipe.user_id == session['user_id'],
        UserRecipe.status == 'in_progress').first()
    cooked_recipe.status = 'cooked'

    db.session.commit()

    return jsonify({'result': True})
Exemplo n.º 2
0
def show_search_by_results():
    """ Takes user's selected ingredients to search for recipes and displays results."""

    # Retrieve list of selected ingredients by name and transform to search string
    search_ingredients = request.args.getlist("ingredient")
    ingredients = "%2C+".join(search_ingredients)

    search_results = search_api_by_ingredient(ingredients)

    # After getting search results, check if inventory quantity >= recipe quantity
    # since API only searches by ingredient name and doesn't include quantities.
    # Only display results that pass this criteria.

    # Filtered list of recipes to display to user
    filter_recipes = []

    for recipe in search_results['results']:
        enough_ingredients = True

        for ingredient in recipe['usedIngredients']:
            check_ingredient = Inventory.query.filter(
                Inventory.ingredient_id == int(ingredient['id']),
                Inventory.user_id == session['user_id']).first()

            # If the recipe ingredient's unit != inventory unit, convert it
            if ingredient['unitLong'] != check_ingredient.ingredients.base_unit:
                (converted_amount, converted_unit) = convert_to_base_unit(
                    round(float(ingredient['amount']), 2),
                    ingredient['unitLong'])
                # If recipe amount > inventory amount, don't add recipe to list
                if converted_amount > check_ingredient.current_quantity:
                    enough_ingredients = False
            else:
                # If recipe amount > inventory amount, don't add recipe to list
                if round(float(ingredient['amount']),
                         2) > check_ingredient.current_quantity:
                    enough_ingredients = False

        # If inventory quantities >= respective recipe quantities, add to list
        if enough_ingredients == True:
            filter_recipes.append(recipe['id'])

    current_user = User.query.get(session['user_id'])

    results_recipes = current_user.get_used_and_missing_ingredients(
        filter_recipes)

    return render_template("recipes-by-ingredient.html",
                           results_recipes=results_recipes)
Exemplo n.º 3
0
    def test_convert_to_base_other(self):
        """ Test other units."""

        self.assertEqual(convert_to_base_unit(2, 'servings'), (2, 'servings'))
Exemplo n.º 4
0
    def test_incorrect_convert_to_base_unit(self):
        """ Test incorrect conversion."""

        self.assertNotEqual(convert_to_base_unit(2, 'pounds'), (2, 'ounces'))
Exemplo n.º 5
0
    def test_convert_to_base_unit_tsp(self):
        """ Test conversion from tablespoons to teaspoons."""

        self.assertEqual(convert_to_base_unit(2, 'tbsp'), (6, 'teaspoons'))
Exemplo n.º 6
0
    def test_convert_to_base_unit_pounds(self):
        """ Test conversion from pounds to ounces."""

        self.assertEqual(convert_to_base_unit(2, 'pounds'), (32.00, 'ounces'))