From e439c2e8fb855c8e95a45b7c058d605013bf00d8 Mon Sep 17 00:00:00 2001 From: Kostiantyn Stoliarskyi Date: Tue, 9 May 2023 13:01:05 +0300 Subject: [PATCH] fix comment --- app/views/book.py | 107 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/app/views/book.py b/app/views/book.py index 31570fb..55b4d06 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -1341,68 +1341,67 @@ def comment_delete( interpretation_id: int, sub_collection_id: int | None = None, ): - form = f.DeleteCommentForm() - if form.validate_on_submit(): - book: m.Book = db.session.get(m.Book, book_id) - comment_id = form.comment_id.data - if not book or book.owner != current_user or book.is_deleted: - log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book) - flash("You are not owner of this book!", "danger") - return redirect(url_for("book.my_books")) + book: m.Book = db.session.get(m.Book, book_id) - collection: m.Collection = db.session.get(m.Collection, collection_id) - if not collection or collection.is_deleted: - log(log.WARNING, "Collection with id [%s] not found", collection_id) - flash("Collection not found", "danger") - return redirect(url_for("book.collection_view", book_id=book_id)) + if not book or book.owner != current_user or book.is_deleted: + log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book) + flash("You are not owner of this book!", "danger") + return redirect(url_for("book.my_books")) - if sub_collection_id: - sub_collection: m.Collection = db.session.get( - m.Collection, sub_collection_id + collection: m.Collection = db.session.get(m.Collection, collection_id) + if not collection or collection.is_deleted: + log(log.WARNING, "Collection with id [%s] not found", collection_id) + flash("Collection not found", "danger") + return redirect(url_for("book.collection_view", book_id=book_id)) + + if sub_collection_id: + sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) + if not sub_collection or sub_collection.is_deleted: + log( + log.WARNING, + "Sub_collection with id [%s] not found", + sub_collection_id, ) - if not sub_collection or sub_collection.is_deleted: - log( - log.WARNING, - "Sub_collection with id [%s] not found", - sub_collection_id, - ) - flash("SubCollection not found", "danger") - return redirect( - url_for( - "book.sub_collection_view", - book_id=book_id, - collection_id=collection_id, - ) + flash("SubCollection not found", "danger") + return redirect( + url_for( + "book.sub_collection_view", + book_id=book_id, + collection_id=collection_id, ) + ) - redirect_url = url_for( - "book.qa_view", - book_id=book_id, - collection_id=collection_id, - sub_collection_id=sub_collection_id, - section_id=section_id, - interpretation_id=interpretation_id, - ) - section: m.Section = db.session.get(m.Section, section_id) - if not section or section.is_deleted: - log(log.WARNING, "Section with id [%s] not found", section_id) - flash("Section not found", "danger") - return redirect(redirect_url) + redirect_url = url_for( + "book.qa_view", + book_id=book_id, + collection_id=collection_id, + sub_collection_id=sub_collection_id, + section_id=section_id, + interpretation_id=interpretation_id, + ) + section: m.Section = db.session.get(m.Section, section_id) + if not section or section.is_deleted: + log(log.WARNING, "Section with id [%s] not found", section_id) + flash("Section not found", "danger") + return redirect(redirect_url) - interpretation: m.Interpretation = db.session.get( - m.Interpretation, interpretation_id - ) - if not interpretation or interpretation.is_deleted: - log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id) - flash("Interpretation not found", "danger") - return redirect(redirect_url) + interpretation: m.Interpretation = db.session.get( + m.Interpretation, interpretation_id + ) + if not interpretation or interpretation.is_deleted: + log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id) + flash("Interpretation not found", "danger") + return redirect(redirect_url) - comment: m.Comment = db.session.get(m.Comment, comment_id) - if not comment or comment.is_deleted: - log(log.WARNING, "Comment with id [%s] not found", comment_id) - flash("Comment not found", "danger") - return redirect(redirect_url) + form = f.DeleteCommentForm() + comment_id = form.comment_id.data + comment: m.Comment = db.session.get(m.Comment, comment_id) + if not comment or comment.is_deleted: + log(log.WARNING, "Comment with id [%s] not found", comment_id) + flash("Comment not found", "danger") + return redirect(redirect_url) + if form.validate_on_submit(): comment.is_deleted = True log(log.INFO, "Delete comment [%s]", comment) comment.save()