Skip to content

Conversation

transcental
Copy link

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
Copy link
Author

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 transcental changed the title Fixes #6 Fixes #4 and #6 Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant