crud sections

This commit is contained in:
SvyatoslavArtymovych 2023-05-11 12:39:50 +03:00
parent b231bdc396
commit 0f4c91ce69
3 changed files with 12 additions and 79 deletions

View File

@ -58,7 +58,7 @@ def book_validator() -> Response | None:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted: if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id) log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("SubCollection not found", "danger") flash("Subcollection not found", "danger")
return redirect( return redirect(
url_for( url_for(
"book.sub_collection_view", "book.sub_collection_view",

View File

@ -594,35 +594,14 @@ def collection_delete(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/create_section", "/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/create_section",
methods=["POST"], methods=["POST"],
) )
@register_book_verify_route(bp.name)
@login_required @login_required
def section_create( def section_create(
book_id: int, collection_id: int, sub_collection_id: int | None = None book_id: int, collection_id: int, sub_collection_id: int | None = None
): ):
book: m.Book = db.session.get(m.Book, book_id) book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, 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))
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, 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.collection_view", book_id=book_id) redirect_url = url_for("book.collection_view", book_id=book_id)
if collection_id: if collection_id:
@ -667,6 +646,7 @@ def section_create(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/edit_section", "/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/edit_section",
methods=["POST"], methods=["POST"],
) )
@register_book_verify_route(bp.name)
@login_required @login_required
def section_edit( def section_edit(
book_id: int, book_id: int,
@ -674,31 +654,6 @@ def section_edit(
section_id: int, section_id: int,
sub_collection_id: int | None = None, 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( redirect_url = url_for(
"book.interpretation_view", "book.interpretation_view",
book_id=book_id, book_id=book_id,
@ -745,6 +700,7 @@ def section_edit(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/delete_section", "/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/delete_section",
methods=["POST"], methods=["POST"],
) )
@register_book_verify_route(bp.name)
@login_required @login_required
def section_delete( def section_delete(
book_id: int, book_id: int,
@ -752,32 +708,9 @@ def section_delete(
section_id: int, section_id: int,
sub_collection_id: int | None = None, sub_collection_id: int | None = None,
): ):
book: m.Book = db.session.get(m.Book, book_id) collection: m.Collection = db.session.get(
if not book or book.owner != current_user: m.Collection, sub_collection_id or collection_id
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))
collection_to_edit = collection
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,
) )
)
collection_to_edit = sub_collection
redirect_url = url_for( redirect_url = url_for(
"book.section_view", "book.section_view",
@ -792,13 +725,13 @@ def section_delete(
return redirect(redirect_url) return redirect(redirect_url)
section.is_deleted = True section.is_deleted = True
if not collection_to_edit.active_sections: if not collection.active_sections:
log( log(
log.INFO, log.INFO,
"Section [%s] has no active section. Set is_leaf = False", "Section [%s] has no active section. Set is_leaf = False",
section.id, section.id,
) )
collection_to_edit.is_leaf = False collection.is_leaf = False
log(log.INFO, "Delete section [%s]", section.id) log(log.INFO, "Delete section [%s]", section.id)
section.save() section.save()

View File

@ -498,7 +498,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
) )
assert response.status_code == 200 assert response.status_code == 200
assert b"SubCollection not found" in response.data assert b"Subcollection not found" in response.data
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete", f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
@ -517,7 +517,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
) )
assert response.status_code == 200 assert response.status_code == 200
assert b"Collection not found" in response.data assert b"Subcollection not found" in response.data
def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner): def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):