Flask-Blog-Tutorial icon indicating copy to clipboard operation
Flask-Blog-Tutorial copied to clipboard

Fixes #4 and #6

Open transcental opened this issue 3 years ago • 1 comments

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')

transcental avatar Aug 14 '21 17:08 transcental

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')

transcental avatar Aug 14 '21 17:08 transcental