Flask-Blog-Tutorial
Flask-Blog-Tutorial copied to clipboard
Fixes #4 and #6
Adding these two lines of code to the else statement in views.py
's delete_post()
function solves this issue(#6):
def delete_post(id):
post = Post.query.filter_by(id=id).first()
if not post:
flash('Post not found', category='error')
elif current_user.id != post.id and current_user.username not in moderators:
flash('You do not have permission to delete this post', category='error')
else:
++ for comment in post.comments:
++ db.session.delete(comment)
db.session.delete(post)
db.session.commit()
flash('Post deleted', category='success')
Also fixed #4 like so:
if not post:
flash("Post does not exist.", category='error')
-- elif current_user.id != post.id:
++ elif current_user.id != post.author:
flash('You do not have permission to delete this post.', category='error')
else:
for comment in post.comments:
db.session.delete(comment)
db.session.delete(post)
db.session.commit()
flash('Post deleted.', category='success')