def add_recipe(): form_data = request.form if form_data: # validate recipe validator = Validate() validation_errors = validator.validate_data(form_data,{ "name":{ "required":True, "min":5, "max":200 }, "description":{ "max":200 } }) if not validation_errors: recips = RecipesDict(session["logged_in"]) existing_recipes = recips.fetch_user_recipes() if len(existing_recipes): new_recipe_id = int(sorted([key for key in existing_recipes.keys()])[-1]) + 1 else: new_recipe_id = 1 recipe = Recipe(new_recipe_id, form_data["name"], form_data["description"], form_data["category"],session["logged_in"]) recips.add_recipe(recipe.recipe_details(),session["logged_in"]) users = Users() user = users.get_all_users()[session["logged_in"]] return render_template("recipes.html", recipes = recips.fetch_user_recipes().values(), user_is_logged_in = True, user = user ) return str(request.form) return render_template("add-recipe.html", page_title="Add a Recipe",user_is_logged_in=True, user = Users().get_all_users()[session["logged_in"]])
def login_user(): form_data = request.form users = Users() available_users = users.get_all_users() if form_data["email"] in available_users and available_users[form_data["email"]]["password"] == form_data["password"]: session["logged_in"] = form_data["email"] return redirect(url_for("home")) flash("Invalid username and password combination", "login_errors") return render_template("index.html")
def get_recipe(recipe_id): recipes = RecipesDict(session["logged_in"]) form_data = request.form if not form_data: return render_template("edit-recipe.html", recipe = recipes.fetch_user_recipes()[str(recipe_id)], user_is_logged_in = True, user = Users().get_all_users()[session["logged_in"]]) else: new_recipe = Recipe(recipe_id, form_data["name"], form_data["description"], form_data["category"],session["logged_in"]) recipes.edit_recipe(session["logged_in"],recipe_id,new_recipe.recipe_details()) return render_template("recipes.html", recipes = recipes.fetch_user_recipes().values(), user=Users().get_all_users()[session["logged_in"]], user_is_logged_in = True)
def delete_recipe(recipe_id): recipes = RecipesDict(session["logged_in"]) available_recipes = recipes.fetch_user_recipes() available_recipes.pop(str(recipe_id)) recipes.save_all_recipes(available_recipes,session["logged_in"]) return render_template("recipes.html", recipes = available_recipes.values(), user=Users().get_all_users()[session["logged_in"]], user_is_logged_in = True)
class UserSimulatorTestCases(unittest.TestCase): def setUp(self): self.user = User(1, "brian", "os", "*****@*****.**", "password@1") self.users = Users() def test_access_of_inexistent_user(self): self.assertRaises(UserNotFoundException, self.users.get_user, 7) def test_new_empty_user_addition(self): self.assertRaises(NullUserError, self.users.add_user, {}) def test_users_increment_when_a_user_is_added(self): users_len = len(self.users.users) self.users.add_user( User(1, "dennis", "Deilson", "*****@*****.**", "password")) new_users_len = len(self.users.users) self.assertEqual(users_len + 1, new_users_len) def test_user_absence_on_deletion(self): self.users.delete_user("*****@*****.**") self.assertFalse("*****@*****.**" in self.users.users) def test_raise_user_not_found_exception_if_user_is_deleted(self): self.assertRaises(NullUserError, self.users.delete_user, 7)
def setUp(self): self.user = User(1, "brian", "os", "*****@*****.**", "password@1") self.users = Users()
def get_recipes(): users = Users() my_user = users.get_all_users()[session["logged_in"]] recipes = RecipesDict(session["logged_in"]).fetch_user_recipes() # return str(recipes.values()) return render_template("recipes.html", recipes= recipes.values(), user_is_logged_in=True, user=my_user)
def register_user(): form_data = request.form users = Users() available_users = users.get_all_users() # validate form form_data validate = Validate() validation_errors = validate.validate_data(form_data, { "fname": { "required": True, "min": 4, "max": 20, "no_number": True }, "lname": { "required": True, "min": 4, "max": 20, "no_number": True, }, "email": { "required": True, "max": 100, "min": 10 }, "password": { "required": True, "min": 8, "max": 20 }, "c_password": { "matches": "password" } }) # if validation is passed if not validation_errors: if form_data["email"] in available_users: flash("Email \"{}\" is already in use".format( form_data["email"]), "register_errors") else: users = Users() user = User(len(users.get_all_users( )) + 1, form_data["fname"], form_data["lname"], form_data["email"], form_data["password"]) users.add_user(user) users.save_users(users.get_all_users()) flash("You have successfully registered and you can now login", "success") else: for validation_error in validation_errors: flash((validation_error), "register_errors") return redirect(url_for("home_page"))
def home(): users = Users() my_user = users.get_all_users()[session["logged_in"]] recipes = RecipesDict(str(session["logged_in"])) return render_template("dashboard.html", recipes=recipes.fetch_user_recipes().values(), user_is_logged_in=True, user=my_user)