def edit_food(food_id=None): if request.method == "POST": food = Food.get_food(food_id) if not request.form['cal'] == "": food.cal = float(request.form['cal']) if not request.form['pro'] == "": food.pro = float(request.form['pro']) if not request.form['fat'] == "": food.fat = float(request.form['fat']) if not request.form['carbs'] == "": food.carbs = float(request.form['carbs']) food.update() return render_template("food/edit_food.html", all_food=Food.get_foods()) elif request.method == "GET": try: return render_template("food/edit_food.html", food=Food.get_food(food_id)) except food_exceptions.IdNotFoundException: if session['email'] in ADMINS: all_food = Food.get_foods() else: user = User.get_by_email(session['email']) all_food = Food.get_by_user_id(user_id=user._id) return render_template("food/edit_food.html", all_food=all_food)
def delete_food(food_id): try: for food in user_blueprint.user_food: if food._id == food_id: user_blueprint.user_food.remove(food) break Food.remove(food_id) except food_exceptions.IdNotFoundException: return render_template("user/profile.html", all_food=Food.get_foods(), ex="לא נמצא.") return render_template("food/edit_food.html", all_food=Food.get_foods())
def add_to_my_list(list_id=None): if request.method == "POST": try: food = Food.get_food(request.form["id"]) food.gram = int(request.form["gram"]) DietList.add_to_list(list_id=list_id, food=food) user = User.get_by_email(session['email']) except food_exceptions.InvalidFoodAmount: return render_template("list/add_to_list.html", user_list=DietList.get_list(list_id), all_food=Food.get_foods(), ex="הכנס כמות אל התיבה.") return render_template("list/my_lists.html", user_lists=DietList.get_user_lists(user._id)) return render_template("list/add_to_list.html", user_list=DietList.get_list(list_id), all_food=Food.get_foods())
def list_food(): all_food = Food.get_foods() for each_food in all_food: print("Name: {}, Cal: {}, Pro: {}, Fat: {}, Carb: {}, Id: {}.". format(each_food.name, each_food.cal, each_food.pro, each_food.fat , each_food.carbs, each_food._id)) id = input("Input id of the item: ") gram = input("Input gram amount: ") food = Food.get_food(id) food.gram = gram return food
def select_food(food_id): try: current_food = Food.get_food(food_id) except food_exceptions.IdNotFoundException: return render_template("user/profile.html", email=session['email'], current_food=None, all_food=Food.get_foods(), ex="לא נמצא.", result=list_blueprint.result) return render_template("user/profile.html", email=session['email'], current_food=current_food, all_food=user_blueprint.user_food, current_list=list_blueprint.current_list, result=list_blueprint.result)
def login(): if request.method == "POST": email = request.form['email'] password = request.form['password'] try: if User.login_valid(email=email, password=password): User.login(email) if email.lower() in ADMINS: user_blueprint.user_food = Food.get_foods() else: admin = User.get_by_email("*****@*****.**") user = User.get_by_email(email) user_blueprint.user_food = Food.get_by_user_id(admin._id) + Food.get_by_user_id(user._id) return render_template("user/profile.html", email=email, all_food=user_blueprint.user_food) except user_exceptions.WrongPasswordException: return render_template("user/login.html", ex="הסיסמה שגויה, נסה שנית.") except user_exceptions.UserNotExistsException: return render_template("user/login.html", ex="המשתמש אינו קיים במערכת.") return render_template("user/login.html")