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 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 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")