subcollection edit, delete endpoints

This commit is contained in:
SvyatoslavArtymovych 2023-04-27 16:35:55 +03:00
parent 6cd0ca722d
commit a37f3159c6
2 changed files with 93 additions and 56 deletions

View File

@ -410,8 +410,13 @@ def collection_create(book_id: int, collection_id: int | None = None):
@bp.route("/<int:book_id>/<int:collection_id>/edit", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/edit", methods=["POST"]
)
@login_required
def collection_edit(book_id: int, collection_id: int):
def collection_edit(
book_id: int, collection_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)
@ -423,6 +428,18 @@ def collection_edit(book_id: int, collection_id: int):
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,
)
)
form = f.EditCollectionForm()
redirect_url = url_for(
@ -462,7 +479,7 @@ def collection_edit(book_id: int, collection_id: int):
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Book create errors: [%s]", form.errors)
log(log.ERROR, "Collection edit errors: [%s]", form.errors)
for field, errors in form.errors.items():
field_label = form._fields[field].label.text
for error in errors:
@ -471,8 +488,14 @@ def collection_edit(book_id: int, collection_id: int):
@bp.route("/<int:book_id>/<int:collection_id>/delete", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/delete",
methods=["POST"],
)
@login_required
def collection_delete(book_id: int, collection_id: int):
def collection_delete(
book_id: int, collection_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:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
@ -484,6 +507,18 @@ def collection_delete(book_id: int, collection_id: int):
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,
)
)
collection.is_deleted = True

View File

@ -254,7 +254,7 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/0/edit",
f"/book/{book.id}/999/edit",
data=dict(
label=new_label,
about=new_about,
@ -343,68 +343,70 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
assert sub_collection.is_leaf
assert sub_collection.parrent_id == collection.id
# m.Collection(
# label="Test Collection #2 Label", version_id=collection.version_id
# ).save()
m.Collection(
label="Test SubCollection #2 Label",
version_id=collection.version_id,
parrent_id=collection.id,
).save()
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/edit",
# data=dict(
# label="Test Collection #2 Label",
# ),
# follow_redirects=True,
# )
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
data=dict(
label="Test SubCollection #2 Label",
),
follow_redirects=True,
)
# assert response.status_code == 200
# assert b"Collection label must be unique!" in response.data
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
# new_label = "Test Collection #1 Label(edited)"
# new_about = "Test Collection #1 About(edited)"
new_label = "Test SubCollection #1 Label(edited)"
new_about = "Test SubCollection #1 About(edited)"
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/edit",
# data=dict(
# label=new_label,
# about=new_about,
# ),
# follow_redirects=True,
# )
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
# assert response.status_code == 200
# assert b"Success!" in response.data
assert response.status_code == 200
assert b"Success!" in response.data
# edited_collection: m.Collection = m.Collection.query.filter_by(
# label=new_label, about=new_about
# ).first()
# assert edited_collection
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
# response: Response = client.post(
# f"/book/{book.id}/0/edit",
# data=dict(
# label=new_label,
# about=new_about,
# ),
# follow_redirects=True,
# )
response: Response = client.post(
f"/book/{book.id}/{collection.id}/9999/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
# assert response.status_code == 200
# assert b"Collection not found" in response.data
assert response.status_code == 200
assert b"SubCollection not found" in response.data
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/delete",
# follow_redirects=True,
# )
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
# assert response.status_code == 200
# assert b"Success!" in response.data
assert response.status_code == 200
assert b"Success!" in response.data
# deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
# assert deleted_collection.is_deleted
deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
assert deleted_collection.is_deleted
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/delete",
# follow_redirects=True,
# )
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
# assert response.status_code == 200
# assert b"Collection not found" in response.data
assert response.status_code == 200
assert b"Collection not found" in response.data