mirror of https://github.com/logos-co/open-law.git
subcollection edit, delete endpoints
This commit is contained in:
parent
6cd0ca722d
commit
a37f3159c6
|
@ -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>/edit", methods=["POST"])
|
||||||
|
@bp.route(
|
||||||
|
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/edit", methods=["POST"]
|
||||||
|
)
|
||||||
@login_required
|
@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)
|
book: m.Book = db.session.get(m.Book, book_id)
|
||||||
if not book or book.owner != current_user or book.is_deleted:
|
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)
|
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)
|
log(log.WARNING, "Collection with id [%s] not found", collection_id)
|
||||||
flash("Collection not found", "danger")
|
flash("Collection not found", "danger")
|
||||||
return redirect(url_for("book.collection_view", book_id=book_id))
|
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()
|
form = f.EditCollectionForm()
|
||||||
redirect_url = url_for(
|
redirect_url = url_for(
|
||||||
|
@ -462,7 +479,7 @@ def collection_edit(book_id: int, collection_id: int):
|
||||||
flash("Success!", "success")
|
flash("Success!", "success")
|
||||||
return redirect(redirect_url)
|
return redirect(redirect_url)
|
||||||
else:
|
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():
|
for field, errors in form.errors.items():
|
||||||
field_label = form._fields[field].label.text
|
field_label = form._fields[field].label.text
|
||||||
for error in errors:
|
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>/delete", methods=["POST"])
|
||||||
|
@bp.route(
|
||||||
|
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/delete",
|
||||||
|
methods=["POST"],
|
||||||
|
)
|
||||||
@login_required
|
@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)
|
book: m.Book = db.session.get(m.Book, book_id)
|
||||||
if not book or book.owner != current_user:
|
if not book or book.owner != current_user:
|
||||||
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
|
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)
|
log(log.WARNING, "Collection with id [%s] not found", collection_id)
|
||||||
flash("Collection not found", "danger")
|
flash("Collection not found", "danger")
|
||||||
return redirect(url_for("book.collection_view", book_id=book_id))
|
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
|
collection.is_deleted = True
|
||||||
|
|
||||||
|
|
|
@ -254,7 +254,7 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert edited_collection
|
assert edited_collection
|
||||||
|
|
||||||
response: Response = client.post(
|
response: Response = client.post(
|
||||||
f"/book/{book.id}/0/edit",
|
f"/book/{book.id}/999/edit",
|
||||||
data=dict(
|
data=dict(
|
||||||
label=new_label,
|
label=new_label,
|
||||||
about=new_about,
|
about=new_about,
|
||||||
|
@ -343,68 +343,70 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert sub_collection.is_leaf
|
assert sub_collection.is_leaf
|
||||||
assert sub_collection.parrent_id == collection.id
|
assert sub_collection.parrent_id == collection.id
|
||||||
|
|
||||||
# m.Collection(
|
m.Collection(
|
||||||
# label="Test Collection #2 Label", version_id=collection.version_id
|
label="Test SubCollection #2 Label",
|
||||||
# ).save()
|
version_id=collection.version_id,
|
||||||
|
parrent_id=collection.id,
|
||||||
|
).save()
|
||||||
|
|
||||||
# response: Response = client.post(
|
response: Response = client.post(
|
||||||
# f"/book/{book.id}/{collection.id}/edit",
|
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
|
||||||
# data=dict(
|
data=dict(
|
||||||
# label="Test Collection #2 Label",
|
label="Test SubCollection #2 Label",
|
||||||
# ),
|
),
|
||||||
# follow_redirects=True,
|
follow_redirects=True,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
# assert b"Collection label must be unique!" in response.data
|
assert b"Collection label must be unique!" in response.data
|
||||||
|
|
||||||
# new_label = "Test Collection #1 Label(edited)"
|
new_label = "Test SubCollection #1 Label(edited)"
|
||||||
# new_about = "Test Collection #1 About(edited)"
|
new_about = "Test SubCollection #1 About(edited)"
|
||||||
|
|
||||||
# response: Response = client.post(
|
response: Response = client.post(
|
||||||
# f"/book/{book.id}/{collection.id}/edit",
|
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
|
||||||
# data=dict(
|
data=dict(
|
||||||
# label=new_label,
|
label=new_label,
|
||||||
# about=new_about,
|
about=new_about,
|
||||||
# ),
|
),
|
||||||
# follow_redirects=True,
|
follow_redirects=True,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
# assert b"Success!" in response.data
|
assert b"Success!" in response.data
|
||||||
|
|
||||||
# edited_collection: m.Collection = m.Collection.query.filter_by(
|
edited_collection: m.Collection = m.Collection.query.filter_by(
|
||||||
# label=new_label, about=new_about
|
label=new_label, about=new_about
|
||||||
# ).first()
|
).first()
|
||||||
# assert edited_collection
|
assert edited_collection
|
||||||
|
|
||||||
# response: Response = client.post(
|
response: Response = client.post(
|
||||||
# f"/book/{book.id}/0/edit",
|
f"/book/{book.id}/{collection.id}/9999/edit",
|
||||||
# data=dict(
|
data=dict(
|
||||||
# label=new_label,
|
label=new_label,
|
||||||
# about=new_about,
|
about=new_about,
|
||||||
# ),
|
),
|
||||||
# follow_redirects=True,
|
follow_redirects=True,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
# response: Response = client.post(
|
response: Response = client.post(
|
||||||
# f"/book/{book.id}/{collection.id}/delete",
|
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
|
||||||
# follow_redirects=True,
|
follow_redirects=True,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
# assert b"Success!" in response.data
|
assert b"Success!" in response.data
|
||||||
|
|
||||||
# deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
|
deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
|
||||||
# assert deleted_collection.is_deleted
|
assert deleted_collection.is_deleted
|
||||||
|
|
||||||
# response: Response = client.post(
|
response: Response = client.post(
|
||||||
# f"/book/{book.id}/{collection.id}/delete",
|
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
|
||||||
# follow_redirects=True,
|
follow_redirects=True,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
# assert b"Collection not found" in response.data
|
assert b"Collection not found" in response.data
|
||||||
|
|
Loading…
Reference in New Issue