def delete(self, recipe_id): user_id = get_jwt_identity() recipe = FavouriteRecipesModel.find_by_recipe_id_user_id(recipe_id, user_id) if not recipe: return {"message": "Sorry, I couldn't find this recipe among your favourites."}, 404 recipe.delete_from_db() if not FavouriteRecipesModel.find_by_recipe_id(recipe_id): RecipeModel.delete_from_db(recipe_id) return {"message": f"Recipe with the id {recipe_id} removed successfully from your favourites!"}, 200
def delete(self, user_id): claims = get_jwt_claims() if not claims["admin"]: return {"message": "Admin permission required!"}, 403 user_to_delete = UserModel.find_by_id(user_id) user_favourite_recipe_ids = FavouriteRecipesModel.show_my_recipe_ids( user_id) user_to_delete.delete_from_db() # this is to remove the deleted user's recipes from the table 'recipes'/ # if no other user have it saved in their favourites: for recipe_id in user_favourite_recipe_ids: if not FavouriteRecipesModel.find_by_recipe_id(recipe_id[0]): RecipeModel.delete_from_db(recipe_id[0]) return { "message": f"User's account (user_id: {user_id}) deleted successfully!" }, 200
def delete(self, username): current_user_id = get_jwt_identity() current_user = UserModel.find_by_id(current_user_id) if not current_user.username == username: return { "message": "You need to be logged in to the account you want to remove!" }, 401 user_favourite_recipe_ids = FavouriteRecipesModel.show_my_recipe_ids( current_user_id) current_user.delete_from_db() # this is to remove the deleted user's recipes from the table 'recipes'/ # if no other user have it saved in their favourites: for recipe_id in user_favourite_recipe_ids: if not FavouriteRecipesModel.find_by_recipe_id(recipe_id[0]): RecipeModel.delete_from_db(recipe_id[0]) return { "message": f"User's account (username: {username}) deleted successfully!", "access_token": None, "refresh_token": None }, 200
def test_crud(self): with self.app_context(): recipe = RecipeModel("Title1", "Url1", "Ingredients1, test1") self.assertIsNone( RecipeModel.find_by_href("Url1"), f"Recipe object with a href {recipe.href} should not exist in the table." ) recipe.save_to_db() self.assertIsNotNone( RecipeModel.find_by_href("Url1"), f"Recipe object with a link {recipe.href} failed to save to db" ) self.assertIsNotNone( RecipeModel.find_by_recipe_id(1), f"Recipe did not find by its (manually created) id.") self.assertEqual( RecipeModel.find_by_href("Url1").recipe_title, "Title1", f"Recipe title does not equal the desired one {recipe.recipe_title}" ) recipe.delete_from_db(1) self.assertIsNone( RecipeModel.find_by_href("Url1"), "Recipe object found in db, while it should already be deleted." )