def edit_recipe_category(user, recipe_cat, category_id): """ Edits a recipe category """ recipe_cat_data = request.get_json() recipe_cat_errors = ValidateCat.validate_recipe(recipe_cat_data) if recipe_cat_errors: return jsonify({"errors": recipe_cat_errors}), 400 existing_recipe_cat = models.RecipeCategory.query.filter_by( name=format_data(recipe_cat_data.get("cat_name")), owner=user.id).first() if existing_recipe_cat and existing_recipe_cat.id != category_id: return jsonify({ "errors": [ "The new recipe category name you are trying to use already exists" ] }), 400 if format_data(recipe_cat_data.get("cat_name")) == recipe_cat.name: return jsonify({ "message": "Recipe category name is similar to the previous. No changes where made" }), 400 # edit the recipe recipe_cat.name = format_data(recipe_cat_data.get("cat_name")) db.session.commit() return jsonify({ "message": "Successfully edited recipe category", "recipe_cat": recipe_cat.recipe_cat_details }), 200
def add_recipe_category(user): """ Adds a recipe category """ # validate submitted recipe category details recipe_cat_data = request.get_json() recipe_errors = ValidateCat.validate_recipe(recipe_cat_data) if recipe_errors: return jsonify({"errors": recipe_errors}), 400 # check whether a recipe category name by the same already exists existing_recipe = models.RecipeCategory.query.filter_by( name=format_data(recipe_cat_data.get("cat_name")), owner=user.id).first() if existing_recipe: return jsonify({ "errors": ["The Recipe Category you are trying to add already exists"] }), 400 # create the recipe category recipe = models.RecipeCategory( format_data(recipe_cat_data.get("cat_name")), user.id) recipe.save_recipe_cat() return jsonify({ "message": "Successfully created recipe category", "recipe_cat": recipe.recipe_cat_details }), 201, { "Location": recipe.recipe_cat_details.get("url") }
def edit_a_recipe(user, recipe, recipe_id): """ Edits a recipe :param user: Current user object, passed from a decorator :param recipe: Recipe object, passed from a decorator :param recipe_id: The recipe id :return: """ recipe_data = request.get_json() # validate submitted data recipe_errors = ValidateRecipe.validate_recipe(recipe_data) if recipe_errors: return jsonify({"errors": recipe_errors}), 400 # check if the supplied recipe category exists recipe_cat = models.RecipeCategory.query.filter_by( id=format_data(recipe_data.get("category"))).first() if not recipe_cat: return jsonify({ "errors": ["Trying to move a recipe to a category that does not exist"] }), 404 if recipe_cat.owner != user.id: return jsonify({ "errors": [ "Trying to move a recipe to a category that does not belong to you" ] }), 403 ''' check whether the new name does not belong to any other recipe by the same user in the same category ''' recipe_exists = models.Recipe.query.filter_by( name=format_data(recipe_data.get("name")), owner=user.id, category_id=recipe_data.get("category")).first() if recipe_exists and recipe_exists.id != recipe_id: return jsonify({ "errors": [ "A recipe with the same name, by the same user already exists in the same category" ], "existing_recipe": recipe_exists.recipe_details }), 400 # edit the recipe recipe.edit_recipe(recipe_data) return jsonify({ "message": "Successfully edited recipe", "recipe": recipe.recipe_details }), 200
def add_recipe(user): """ Adds a recipe """ # validate recipe data recipe_data = request.get_json() recipe_errors = ValidateRecipe.validate_recipe(recipe_data) if recipe_errors: return jsonify({"errors": recipe_errors}), 400 # check if the supplied recipe category exists recipe_cat = models.RecipeCategory.query.filter_by( id=format_data(recipe_data.get("category"))).first() if not recipe_cat: return jsonify({ "errors": ["Trying to add a recipe to a category that does not exist"] }), 404 if recipe_cat.owner != user.id: return jsonify({ "errors": [ "Trying to add a recipe to a category that does not belong to you" ] }), 403 # check if a recipe with the same name exists in the same category and by the same user recipe_exists = models.Recipe.query.filter_by( name=format_data(recipe_data.get("name")), owner=user.id, category_id=recipe_data.get("category")).first() if recipe_exists: return jsonify({ "errors": [ "A recipe with the same name, by the same user already exists in the same category" ], "existing_recipe": recipe_exists.recipe_details }), 400 # add the recipe recipe = models.Recipe(format_data(recipe_data.get("name")), recipe_data.get("steps"), recipe_data.get("ingredients"), recipe_data.get("category"), user.id) recipe.save_recipe() return jsonify({ "message": "Successfully added recipe", "recipe": recipe.recipe_details }), 201, { "Location": recipe.recipe_details["url"] }
def edit_recipe(self, recipe_data): """ Edits a recipe """ self.name = format_data(recipe_data.get("name", self.name)) self.steps = recipe_data.get("steps", self.steps) self.ingredients = recipe_data.get("ingredients", self.ingredients) self.category_id = recipe_data.get("category", self.category_id) db.session.commit()