'Getting specific recipe posted from user that is logged in and Deleting the recipe using recipe_id - flask peewee
I am trying to delete a recipe using recipe_id that belongs to a user_id. MY delete route is deleting all the recipes in the recipes [] I created. I am still new to flask and peewee. i would appreciate a pointers cause I am sure I doing something wrong.
@app.route('/profile', methods=['GET'])
@jwt_required()
def profile_page():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
@app.route('/login', methods=['POST'])
def login():
if request.method == 'POST' :
username = request.form['username']
password = request.form['password']
if not username:
return jsonify('Missing username'), 400
if not password:
return jsonify('Missing password'), 400
registered_user = Users.get(Users.username == username, Users.fullname == Users.fullname)
password_pass = check_password_hash(registered_user.password_harsh, password)
if registered_user:
if password_pass:
access_token = create_access_token(identity=registered_user)
return {"access_token": access_token} , 200
else:
return jsonify('Invalid Login Info'), 400
return jsonify("Please provide an email and password"), 400
@app.route('/add_recipe', methods=['POST'])
@jwt_required()
def add_recipe():
current_user = get_jwt_identity()
if not request.method == 'POST':
return jsonify('Please enter your data')
else:
name = request.form['name']
description = request.form['description']
ingredients = request.form['ingredients']
process = request.form['process']
poster_id = current_user
new_recipe = Recipe.create(name = name, description = description, ingredients =
ingredients, process=process, poster_id = poster_id)
return jsonify("You have added new recipe"), 200
@app.route('/my_recipes', methods=['GET'])
@jwt_required()
def get_my_recipes():
current_user = get_jwt_identity()
query = Recipe.select().join(Users).where(Recipe.poster_id == current_user)
recipes = []
for recipe in query:
recipe_data = {'Recipe ID':recipe.id}
recipes.append(recipe_data)
return jsonify(recipes)
This is the delete route that is giving me issue. I need to be able to delete a particular recipe by id. I am using peewee
@app.route('/delete/<int:id>', methods=['DELETE'])
@jwt_required()
def delete_recipe(id):
current_user = get_jwt_identity()
query = Recipe.select().join(Users).where(Recipe.poster_id == current_user)
recipes = []
for recipe in query:
recipe_data = {'Recipe ID':recipe.id}
recipes.append(recipe_data)
for i in recipes:
if i == id:
del_recipe = recipe.delete_instance()
return jsonify('')
Solution 1:[1]
There is absolutely no reason to iterate over the entire set of the users recipes.
current_user = get_jwt_identity()
query = Recipe.delete().where(
(Recipe.poster_id == current_user.id) &
(Recipe.id == id))
return jsonify({'result': query.execute()})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | coleifer |
