delete interpretation backend

This commit is contained in:
SvyatoslavArtymovych 2023-05-04 16:57:47 +03:00
parent 6fce2cd53b
commit df740078ef
2 changed files with 112 additions and 24 deletions

View File

@ -866,9 +866,9 @@ def section_delete(
) )
############### #####################
# Interpretation CRUD # Interpretation CRUD
############### #####################
@bp.route( @bp.route(
@ -1037,3 +1037,81 @@ def interpretation_edit(
for error in errors: for error in errors:
flash(error.replace("Field", field_label), "danger") flash(error.replace("Field", field_label), "danger")
return redirect(redirect_url) return redirect(redirect_url)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/delete_interpretation",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/delete_interpretation"
),
methods=["POST"],
)
@login_required
def interpretation_delete(
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.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"))
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)
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.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_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.is_deleted = True
log(log.INFO, "Delete interpretation [%s]", interpretation)
interpretation.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)

View File

@ -877,32 +877,42 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200 assert response.status_code == 200
assert b"Interpretation not found" in response.data assert b"Interpretation not found" in response.data
# response: Response = client.post( response: Response = client.post(
# f"/book/{book.id}/{collection.id}/{leaf_collection.id}/{section.id}/delete_section", f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/999/delete_interpretation",
# follow_redirects=True, follow_redirects=True,
# ) )
# assert response.status_code == 200 assert response.status_code == 200
# assert b"Success!" in response.data assert b"Interpretation not found" in response.data
# deleted_section: m.Section = db.session.get(m.Section, section.id) response: Response = client.post(
# assert deleted_section.is_deleted (
f"/book/{book.id}/{collection.id}/{sub_collection.id}/"
f"{section_in_subcollection.id}/{section_in_subcollection.interpretations[0].id}/delete_interpretation"
),
follow_redirects=True,
)
# response: Response = client.post( assert response.status_code == 200
# f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/delete_section", assert b"Success!" in response.data
# follow_redirects=True,
# )
# assert response.status_code == 200 deleted_interpretation: m.Interpretation = db.session.get(
# assert b"Success!" in response.data m.Interpretation, section_in_subcollection.interpretations[0].id
)
assert deleted_interpretation.is_deleted
# deleted_section: m.Section = db.session.get(m.Section, section_2.id) response: Response = client.post(
# assert deleted_section.is_deleted (
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/"
f"{section_in_collection.interpretations[0].id}/delete_interpretation"
),
follow_redirects=True,
)
# response: Response = client.post( assert response.status_code == 200
# f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/delete_section", assert b"Success!" in response.data
# follow_redirects=True,
# )
# assert response.status_code == 200 deleted_interpretation: m.Interpretation = db.session.get(
# assert b"Section not found" in response.data m.Interpretation, section_in_collection.interpretations[0].id
)
assert deleted_interpretation.is_deleted