def delete_ingredients(identifier): ingredient_to_delete = get_resource_or_404(Ingredient, identifier) db.session.delete(ingredient_to_delete) db.session.commit() return ('', 204)
def change_ingredients_in_line(id_): line_to_change = get_resource_or_404(RecipeLine, id_) logged_in = hasattr(g, 'user') def change_line(): new_ingredients_json = request.json.get("new_ingredients") print(new_ingredients_json) new_ingredients = get_new_ingredients_on_line(new_ingredients_json, line_to_change) print(new_ingredients) line_to_change.ingredients = recipeline_association_schema.loads( new_ingredients) print(line_to_change) db.session.commit() return jsonify(recipeline_schema.dump(line_to_change)) if not line_to_change.recipe.creator_id: # anonymously created return change_line() print(logged_in, g.user) if logged_in and g.user.id == line_to_change.recipe.creator_id: return change_line() else: raise InvalidUsage("You don't have permission to modify that line.", 401)
def delete_user(id_): cur_user = get_resource_or_404(User, id_) if g.user != cur_user: raise InvalidUsage("You don't have permission to delete this user.", 401) db.session.delete(cur_user) db.session.commit() return ("", 204)
def post_line(): recipe_to_add_line = get_resource_or_404(Recipe, request.json.get("recipe_id")) if recipe_to_add_line.creator_id == g.user.id: new_line = post_new_resource(RecipeLine, request.json) return jsonify(recipeline_schema.dump(new_line)) else: raise InvalidUsage("You don't have permission to modify that recipe.")
def modify_list(id_): list_to_modify = get_resource_or_404(GroceryList, id_) if list_to_modify.creator_id == g.user.id or g.user.id in list_to_modify.editors: list_to_modify.name = request.json.get("name", list_to_modify.name) db.session.commit() return jsonify(grocerylist_schema.dump(list_to_modify)) else: raise InvalidUsage("You don't have permission to modify this list.", 401)
def put_recipe(id_): recipe_to_change = get_resource_or_404(Recipe, id_) if recipe_to_change.creator_id == g.user.id: recipe_to_change.name = request.json.get("name", recipe_to_change.name) recipe_to_change.url = request.json.get("url", recipe_to_change.url) db.session.commit() return jsonify(recipe_schema.dump(recipe_to_change)) else: raise InvalidUsage("You don't have permission to modify this recipe.")
def add_editors(id_): current_list = get_resource_or_404(GroceryList, id_) if current_list.creator is not g.user: raise InvalidUsage("You are not the creator of this Grocery List", 401) new_editors = users_schema.load(request.json.get("users")) current_list.editors = new_editors db.session.commit() return jsonify(users_schema.dump(new_editors)), 201
def delete_recipe(id_): recipe_to_delete = get_resource_or_404(Recipe, id_) if recipe_to_delete.creator is not g.user: raise InvalidUsage("You are not the creator of this recipe.", 401) db.session.delete(recipe_to_delete) db.session.commit() return ('', 204)
def delete_line(id_): line_to_delete = get_resource_or_404(RecipeLine, id_) if not line_to_delete.recipe.creator_id or hasattr( g, 'user') and g.user.id == line_to_delete.recipe.creator_id: db.session.delete(line_to_delete) db.session.commit() return ('', 204) else: return ('', 403)
def put_line(id_): line_to_change = get_resource_or_404(RecipeLine, id_) if g.user.id == line_to_change.recipe.creator_id: line_to_change.text = request.json.get("text", "") line_to_change.ingredients = ingredients_schema.load( request.json.get("ingredients", "")) db.session.commit() return jsonify(recipeline_schema.dump(line_to_change)) else: raise InvalidUsage("You don't have permission to modify that line.", 401)
def change_user_info(id_): updated_user = get_resource_or_404(User, id_) if updated_user.id == g.user.id: try: updated_user_information = create_user_schema.load(request.json) updated_user.email = updated_user_information.email updated_user.hashed_password = updated_user_information.hashed_password db.session.commit() return jsonify(user_schema.dump(updated_user)) except ValidationError as error: raise InvalidUsage("Your user data was not formatted correctly", payload=error.messages) except IntegrityError: raise InvalidUsage("That email is already in use.") raise InvalidUsage("You don't have permission to edit accounts that aren't yours.", 401)
def delete_list(id_): list_to_delete = get_resource_or_404(GroceryList, id_) db.session.delete(list_to_delete) db.session.commit() return ("", 204)
def get_recipe_info(id_): current_recipe = get_resource_or_404(Recipe, id_) return jsonify(recipe_schema.dump(current_recipe))
def get_ingredient(identifier): cur_ingredient = get_resource_or_404(Ingredient, identifier) return jsonify(ingredient_schema.dump(cur_ingredient))
def get_list(id_): current_list = get_resource_or_404(GroceryList, id_) return jsonify(grocerylist_schema.dump(current_list))
def get_line(id_): current_line = get_resource_or_404(RecipeLine, id_) return jsonify(recipeline_schema.dump(current_line))
def get_user(id_): cur_user = get_resource_or_404(User, id_) return jsonify(user_schema.dump(cur_user))
def get_additional_ingredients(id_): list_to_get = get_resource_or_404(GroceryList, id_) recipe_schema = RecipeSchema() return jsonify(recipe_schema.dump(list_to_get.additional_ingredients))