def add_food_to_log(log_id): food_id = int(request.form.get('food-select')) print(food_id, type(food_id)) food_select = Food.select().where(Food.id == food_id).get() food_select.logs.add(Log.select().where(Log.id == log_id)) return redirect(url_for("main.view", log_id=log_id))
def view(log_id): foods = Food.select() dateobj = Log.select(Log.date).where(Log.id == log_id).get() date = dateobj.date.strftime('%B %d, %Y') log_foods = (Food.select().join( log_food, on=(Food.id == log_food.food_id)).where(log_food.log_id == log_id)) totals = {'protein': 0, 'carbs': 0, 'fats': 0, 'calories': 0} for item in log_foods: totals['protein'] += item.protein totals['carbs'] += item.carbs totals['fats'] += item.fats totals['calories'] += item.calories return render_template("view.html", foods=foods, date=date, log_foods=log_foods, log_id=log_id, totals=totals)
def add(): foods = Food.select() return render_template("add.html", foods=foods, food=None)
def delete_food_from_log(log_id, food_id): food_select = Food.select().where(Food.id == food_id).get() food_select.logs.remove(Log.select().where(Log.id == log_id)) return redirect(url_for("main.view", log_id=log_id))
def edit_food(food_id): food = Food.get(Food.id == food_id) foods = Food.select() return render_template("add.html", foods=foods, food=food)