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 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 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 get_food_list(list_of_food): full_list = [] for each_item in list_of_food: food = Food.get_food(each_item._id) food.gram = int(each_item.gram) full_list.append(food) return full_list
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 remove_from_list(): food = Food.get_food(request.form['id']) food.gram = int(request.form['gram']) DietList.remove_food_from_list(request.form['list_id'], food) user_list = DietList.get_list(request.form['list_id']) if user_list.list_of_food.__len__() == 0: DietList.remove_list(user_list._id) user = User.get_by_email(session['email']) return render_template("list/my_lists.html", user_lists=DietList.get_user_lists(user._id)) return render_template("list/edit_list.html", user_list=user_list, list_of_food=user_list.list_of_food)
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")
def render_list(cls, data): list = cls(**data) list_of_food = [] # fixing list from dict array to obj array for each_food in list.list_of_food: food = Food.get_food(each_food['_id']) food.gram = each_food['gram'] list_of_food.append(food) list.list_of_food = list_of_food # fixing result from dict to obj result = Result(cal=list.result['cal'], pro=list.result['pro'], fat=list.result['fat'], carbs=list.result['carbs']) list.result = result return list
def add_food(): if request.method == "POST": try: user = User.get_by_email(email=session['email']) food = Food(request.form['name'], user_id=user._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.save_to_mongo() user_blueprint.user_food.append(food) except food_exceptions.NameAlreadyExistsException: return render_template("food/add_food.html", ex="שם זה קיים כבר, הכנס שם אחר.") return render_template("food/add_food.html", added=food.name) return render_template("food/add_food.html")
def register(): if request.method == "POST": email = request.form['email'] password2 = request.form['match_password'] password = request.form['password'] try: Utils.match_password(password1=password, password2=password2) User.register(email=email, password=password) admin = User.get_by_email("*****@*****.**") user_blueprint.user_food = Food.get_by_user_id(admin._id) return render_template("user/profile.html", email=email, all_food=user_blueprint.user_food) except user_exceptions.PasswordNotMatchedException: return render_template("user/register.html", ex="שני הסיסמאות אינן תואמות") except user_exceptions.EmailAlreadyExistsException: return render_template("user/register.html", ex="אימייל קיים במערכת. האם אתה מנסה להתחבר למשתמש קיים?") except user_exceptions.EmailPatternInvalidException: return render_template("user/register.html", ex="כתובת אימייל אינה תקינה.") return render_template("user/register.html")
def add_with_a_link(): if request.method == "POST": user = User.get_by_email(session['email']) try: food = Food(name=request.form['name'], user_id=user._id, url=request.form['url']) food.load_values() food.save_to_mongo() user_blueprint.user_food.append(food) except food_exceptions.NameAlreadyExistsException: return render_template("food/add_with_a_link.html", ex="שם זה קיים כבר, הכנס שם אחר.") return render_template("food/add_with_a_link.html", added=food.name) return render_template("food/add_with_a_link.html")
def add_to_list(): try: food = Food.get_by_name(request.form['name']) gram = request.form['gram'] if Utils.valid_amount(gram): food.gram = gram food.calculate_amount_value() list_blueprint.current_list.append(food) list_blueprint.result.add_to_result(food=food) except food_exceptions.NameNotFoundException: return render_template("user/profile.html", email=session['email'], ex="שם לא נמצא במאגר.", current_food=None, all_food=user_blueprint.user_food, current_list=list_blueprint.current_list, result=list_blueprint.result) except HTTPException: return render_template("user/profile.html", email=session['email'], ex="אנא בחר מוצר מתוך הטבלה.", current_food=None, all_food=user_blueprint.user_food, current_list=list_blueprint.current_list, result=list_blueprint.result) except food_exceptions.InvalidFoodAmount: return render_template("user/profile.html", email=session['email'], ex="אנא הוסף כמות.", current_food=None, all_food=user_blueprint.user_food, current_list=list_blueprint.current_list, result=list_blueprint.result) return render_template("user/profile.html", email=session['email'], current_food=None, all_food=user_blueprint.user_food, current_list=list_blueprint.current_list, result=list_blueprint.result)
def get_foods(self): foods = Database.find('food', {'user_id': self._id}) return [Food(**each_food) for each_food in foods]