Merge pull request #87 from Simple2B/kostia/routes_comments

refactored comments
This commit is contained in:
Костя Столярский 2023-05-30 10:24:57 +03:00 committed by GitHub
commit 3d93518b30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 96 deletions

View File

@ -3,11 +3,7 @@
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<form
{% if sub_collection %}
action="{{ url_for('book.comment_delete', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id, interpretation_id=interpretation.id) }}"
{% else %}
action="{{ url_for('book.comment_delete', book_id=book.id, collection_id=collection.id, section_id=section.id, interpretation_id=interpretation.id) }}"
{% endif %}
action="{{ url_for('book.comment_delete', book_id=book.id, interpretation_id=interpretation.id) }}"
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<!-- Modal header -->

View File

@ -3,11 +3,7 @@
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<form
{% if sub_collection %}
action="{{ url_for('book.comment_edit', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id, interpretation_id=interpretation.id) }}"
{% else %}
action="{{ url_for('book.comment_edit', book_id=book.id, collection_id=collection.id, section_id=section.id, interpretation_id=interpretation.id) }}"
{% endif %}
action="{{ url_for('book.comment_edit', book_id=book.id, interpretation_id=interpretation.id) }}"
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<!-- Modal header -->

View File

@ -14,58 +14,22 @@ from .bp import bp
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/create_comment",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/create_comment"
),
"/<int:book_id>/<int:interpretation_id>/create_comment",
methods=["POST"],
)
@login_required
def create_comment(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book 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_library"))
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))
sub_collection = None
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)
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, 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
@ -90,9 +54,8 @@ def create_comment(
log(
log.INFO,
"Create comment for interpretation [%s]. Section: [%s]",
"Create comment for interpretation [%s].",
interpretation,
section,
)
comment.save()
@ -112,25 +75,18 @@ def create_comment(
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/comment_delete",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/comment_delete"
),
"/<int:book_id>/<int:interpretation_id>/comment_delete",
methods=["POST"],
)
@register_book_verify_route(bp.name)
@login_required
def comment_delete(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
redirect_url = url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
form = f.DeleteCommentForm()
comment_id = form.comment_id.data
comment: m.Comment = db.session.get(m.Comment, comment_id)
@ -142,40 +98,29 @@ def comment_delete(
comment.save()
flash("Success!", "success")
return redirect(
url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
)
return redirect(redirect_url)
flash("Invalid id!", "danger")
return redirect(
url_for(
"book.sub_collection_view",
"book.collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/comment_edit",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/comment_edit"
),
"/<int:book_id>/<int:interpretation_id>/comment_edit",
methods=["POST"],
)
@register_book_verify_route(bp.name)
@login_required
def comment_edit(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
redirect_url = url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
form = f.EditCommentForm()
if form.validate_on_submit():
@ -192,15 +137,12 @@ def comment_edit(
comment.save()
flash("Success!", "success")
return redirect(
url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
)
return redirect(redirect_url)
flash("Invalid id!", "danger")
return redirect(
url_for(
"book.sub_collection_view",
"book.collection_view",
book_id=book_id,
collection_id=collection_id,
)
)

View File

@ -1015,9 +1015,8 @@ def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):
comment_text = "Some comment text"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/{interpretation.id}/create_comment",
f"/book/{book.id}/{interpretation.id}/create_comment",
data=dict(
section_id=section_in_subcollection.id,
text=comment_text,
interpretation_id=interpretation.id,
),
@ -1036,9 +1035,8 @@ def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):
# edit
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/{interpretation.id}/comment_edit",
f"/book/{book.id}/{interpretation.id}/comment_edit",
data=dict(
section_id=section_in_subcollection.id,
text=new_text,
interpretation_id=interpretation.id,
comment_id=comment.id,
@ -1054,9 +1052,8 @@ def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):
# delete
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/{interpretation.id}/comment_delete",
f"/book/{book.id}/{interpretation.id}/comment_delete",
data=dict(
section_id=section_in_subcollection.id,
text=comment_text,
interpretation_id=interpretation.id,
comment_id=comment.id,

View File

@ -83,13 +83,12 @@ def test_create_tags_on_comment_create_and_edit(client: FlaskClient):
create_test_book(user.id, 1)
book = db.session.get(m.Book, 1)
collection = db.session.get(m.Collection, 1)
section = db.session.get(m.Section, 1)
interpretation = db.session.get(m.Interpretation, 1)
tags = "[tag1] [tag2] [tag3]"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{section.id}/{interpretation.id}/create_comment",
f"/book/{book.id}/{interpretation.id}/create_comment",
data=dict(
section_id=section.id,
text="some text" + tags,
@ -116,7 +115,7 @@ def test_create_tags_on_comment_create_and_edit(client: FlaskClient):
tags = "[tag1] [tag5] [tag7]"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{section.id}/{interpretation.id}/comment_edit",
f"/book/{book.id}/{interpretation.id}/comment_edit",
data=dict(text="some text" + tags, comment_id=comment.id),
follow_redirects=True,
)
@ -143,14 +142,13 @@ def test_create_tags_on_interpretation_create_and_edit(client: FlaskClient):
create_test_book(user.id, 1)
book = db.session.get(m.Book, 1)
collection = db.session.get(m.Collection, 1)
section = db.session.get(m.Section, 1)
tags = "[tag1] [tag2] [tag3]"
text_1 = "Test Interpretation #1 Text"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{section.id}/create_interpretation",
f"/book/{book.id}/{section.id}/create_interpretation",
data=dict(section_id=section.id, text=text_1 + tags),
follow_redirects=True,
)