コード例 #1
0
def add_missing_ingredients():
    """ Displays shopping list with missing ingredients."""

    new_recipes_to_add = db.session.query(UserRecipe.recipe_id).filter(
        UserRecipe.user_id == session['user_id'],
        UserRecipe.status == 'needs_missing_ingredients').all()

    new_recipe_list = []
    for recipe in new_recipes_to_add:
        new_recipe_list.append(recipe[0])

    new_shopping_list = ShoppingList(
        user_id=session['user_id'],
        has_shopped=False,
    )
    db.session.add(new_shopping_list)

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

    results_recipes = current_user.get_used_and_missing_ingredients(
        new_recipe_list)

    for recipe in results_recipes:
        for missing_ingredient in results_recipes[recipe]['missing_ing']:
            ingredient = Ingredient.query.filter(
                Ingredient.ingredient_id == missing_ingredient[0]).first()
            if not ingredient:
                new_missing_ingredient = Ingredient(
                    ingredient_id=missing_ingredient[0],
                    ingredient_name=missing_ingredient[3],
                    base_unit=missing_ingredient[2],
                    ingredient_aisle=missing_ingredient[4],
                )
                db.session.add(new_missing_ingredient)

            new_list_ingredient = ListIngredient(
                shopping_list_id=new_shopping_list.list_id,
                ingredient_id=missing_ingredient[0],
                aggregate_quantity=missing_ingredient[1],
            )

            db.session.add(new_list_ingredient)

    # Update status of recipes added to shopping list to 'in progress'
    update_recipes = UserRecipe.query.filter(
        UserRecipe.user_id == session['user_id'],
        UserRecipe.status == 'needs_missing_ingredients').all()
    for recipe in update_recipes:
        recipe.status = 'in_progress'
        db.session.commit()

    user_ingredients = new_shopping_list.get_ingredients()

    return render_template("shopping.html", ingredients=user_ingredients)
コード例 #2
0
def show_shopping_list():
    """ Creates shopping list of missing ingredients with aggregated quantities and base units."""

    all_user_recipes = db.session.query(UserRecipe.recipe_id).filter(
        UserRecipe.user_id == session['user_id'],
        UserRecipe.status == 'needs_ingredients').all()
    new_shopping_list = ShoppingList(
        user_id=session['user_id'],
        has_shopped=False,
    )
    db.session.add(new_shopping_list)

    aggregated_ingredients = aggregate_ingredients(all_user_recipes)

    for ingredient_id in aggregated_ingredients:
        ingredient = db.session.query(Ingredient).filter(
            Ingredient.ingredient_id == ingredient_id).first()

        if not ingredient:
            new_ingredient = Ingredient(
                ingredient_id=ingredient_id,
                ingredient_name=aggregated_ingredients[ingredient_id]['name'],
                base_unit=aggregated_ingredients[ingredient_id]['unit'],
                ingredient_aisle=aggregated_ingredients[ingredient_id]
                ['aisle'],
            )
            db.session.add(new_ingredient)

        new_list_ingredient = ListIngredient(
            shopping_list_id=new_shopping_list.list_id,
            ingredient_id=ingredient_id,
            aggregate_quantity=aggregated_ingredients[ingredient_id]
            ['quantity'],
        )
        db.session.add(new_list_ingredient)

    # Update status of recipes added to shopping list to 'in progress'
    update_recipes = UserRecipe.query.filter(
        UserRecipe.user_id == session['user_id'],
        UserRecipe.status == 'needs_ingredients').all()
    for recipe in update_recipes:
        recipe.status = 'in_progress'

    db.session.commit()

    user_ingredients = new_shopping_list.get_ingredients()

    return render_template("shopping.html", ingredients=user_ingredients)
コード例 #3
0
def checkOrUncheck(check):
	my_view = View()
	my_database = ShoppingList()

	user_input = my_view.takeInput("Enter an ID of an ite in your list: ")

	try:
		user_input = int(user_input)
	except ValueError:
		return "Not a number. Try again"

	found_result = my_database.checkID(user_input)

	if len(found_result) == 0:
		return "ID not found"
	else:
		my_database.updateChecked(check, user_input)
		return check
コード例 #4
0
		user_input = int(user_input)
	except ValueError:
		return "Not a number. Try again"

	found_result = my_database.checkID(user_input)

	if len(found_result) == 0:
		return "ID not found"
	else:
		my_database.updateChecked(check, user_input)
		return check


my_view = View()

my_database = ShoppingList()

running = True

while running == True:
	user_input = my_view.menu()

	if user_input == "1":
		item = my_view.takeInput("Enter an item to add to the list: ")

		my_database.insertRow(item)

	elif user_input == "2":
		rows = my_database.selectTable()

		my_view.printList(rows)