Merge pull request #85 from Simple2B/svyat/routes_collection

refactor collection routes
This commit is contained in:
Svyatoslav Artymovych 2023-05-30 09:12:13 +03:00 committed by GitHub
commit 3131360ede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 78 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@
<!-- Modal content --> <!-- Modal content -->
<form <form
id="delete_sub_collection_modal_form" id="delete_sub_collection_modal_form"
action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=0, sub_collection_id=0) }}" action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=0) }}"
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700"> method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }} {{ form_hidden_tag() }}
<input type="hidden" name="collection_id" id="delete_sub_collection_modal_collection_id" value="" /> <input type="hidden" name="collection_id" id="delete_sub_collection_modal_collection_id" value="" />

View File

@ -4,12 +4,9 @@
<div class="relative w-full max-w-2xl max-h-full"> <div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content --> <!-- Modal content -->
<form <form
{% if sub_collection %} action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id) }}"
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700"
{% else %} >
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }} {{ form_hidden_tag() }}
<!-- Modal header --> <!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600"> <div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">

View File

@ -34,28 +34,8 @@ def collection_view(book_id: int):
@bp.route("/<int:book_id>/<int:collection_id>/subcollections", methods=["GET"]) @bp.route("/<int:book_id>/<int:collection_id>/subcollections", methods=["GET"])
def sub_collection_view(book_id: int, collection_id: int): def sub_collection_view(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id) # TODO REMOVE ME
if not book or book.is_deleted: return {"REMOVE ME": "REMOVE ME"}
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "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))
breadcrumbs = create_breadcrumbs(book_id=book_id, collection_path=(collection.id,))
if collection.is_leaf:
return redirect(
url_for("book.section_view", book_id=book.id, collection_id=collection.id)
)
else:
return render_template(
"book/sub_collection_view.html",
book=book,
collection=collection,
breadcrumbs=breadcrumbs,
)
@bp.route("/<int:book_id>/create_collection", methods=["POST"]) @bp.route("/<int:book_id>/create_collection", methods=["POST"])
@ -73,16 +53,12 @@ def collection_create(book_id: int, collection_id: int | None = None):
flash("You can't create subcollection for this collection", "danger") flash("You can't create subcollection for this collection", "danger")
return redirect( return redirect(
url_for( url_for(
"book.sub_collection_view", "book.collection_view",
book_id=book_id, book_id=book_id,
collection_id=collection_id, collection_id=collection_id,
) )
) )
redirect_url = url_for(
"book.sub_collection_view", book_id=book_id, collection_id=collection_id
)
form = f.CreateCollectionForm() form = f.CreateCollectionForm()
if form.validate_on_submit(): if form.validate_on_submit():
@ -114,7 +90,7 @@ def collection_create(book_id: int, collection_id: int | None = None):
label=label, label=label,
about=form.about.data, about=form.about.data,
parent_id=book.versions[-1].root_collection.id, parent_id=book.versions[-1].root_collection.id,
version_id=book.versions[-1].id, version_id=book.last_version.id,
) )
if collection_id: if collection_id:
collection.parent_id = collection_id collection.parent_id = collection_id
@ -137,18 +113,11 @@ 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"]
)
@register_book_verify_route(bp.name) @register_book_verify_route(bp.name)
@login_required @login_required
def collection_edit( def collection_edit(book_id: int, collection_id: int):
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)
collection: m.Collection = db.session.get(m.Collection, collection_id) collection: m.Collection = db.session.get(m.Collection, collection_id)
if sub_collection_id:
collection = db.session.get(m.Collection, sub_collection_id)
form = f.EditCollectionForm() form = f.EditCollectionForm()
redirect_url = url_for( redirect_url = url_for(
@ -158,19 +127,15 @@ def collection_edit(
if form.validate_on_submit(): if form.validate_on_submit():
label = form.label.data label = form.label.data
collection_query: m.Collection = m.Collection.query.filter_by( existing_collection: m.Collection = (
is_deleted=False, m.Collection.query.filter_by(
label=label, is_deleted=False, label=label, parent_id=collection.parent.id
).filter(m.Collection.id != collection.id)
if sub_collection_id:
collection_query = collection_query.filter_by(parent_id=collection_id)
else:
collection_query = collection_query.filter_by(
parent_id=collection.parent.id
) )
.filter(m.Collection.id != collection.id)
.first()
)
if collection_query.first(): if existing_collection:
log( log(
log.INFO, log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]", "Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
@ -203,18 +168,10 @@ def collection_edit(
@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"],
)
@register_book_verify_route(bp.name) @register_book_verify_route(bp.name)
@login_required @login_required
def collection_delete( def collection_delete(book_id: int, collection_id: int):
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
collection: m.Collection = db.session.get(m.Collection, collection_id) collection: m.Collection = db.session.get(m.Collection, collection_id)
if sub_collection_id:
collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
collection.is_deleted = True collection.is_deleted = True
if collection.children: if collection.children:

View File

@ -36,14 +36,13 @@ export function deleteSubCollection() {
} }
deleteSubCollectionModalBtns.forEach(btn => deleteSubCollectionModalBtns.forEach(btn =>
btn.addEventListener('click', () => { btn.addEventListener('click', () => {
const collectionId = btn.getAttribute('data-collection-id');
const subCollectionId = btn.getAttribute('data-sub-collection-id'); const subCollectionId = btn.getAttribute('data-sub-collection-id');
collectionIdInDeleteSubCollectionModal.value = collectionId; collectionIdInDeleteSubCollectionModal.value = subCollectionId;
let newActionPath: string = ''; let newActionPath: string = '';
newActionPath = defaultActionPath.replace( newActionPath = defaultActionPath.replace(
'0/0/delete', '0/delete',
`${collectionId}/${subCollectionId}/delete`, `${subCollectionId}/delete`,
); );
deleteSubCollectionForm.setAttribute('action', `${newActionPath}`); deleteSubCollectionForm.setAttribute('action', `${newActionPath}`);

View File

@ -23,15 +23,13 @@ export function renameSubCollection() {
e.preventDefault(); e.preventDefault();
const bookId = const bookId =
subCollectionRenameForms[index].getAttribute('data-book-id'); subCollectionRenameForms[index].getAttribute('data-book-id');
const collectionId =
subCollectionRenameForms[index].getAttribute('data-collection-id');
const subCollectionId = subCollectionRenameForms[index].getAttribute( const subCollectionId = subCollectionRenameForms[index].getAttribute(
'data-sub-collection-id', 'data-sub-collection-id',
); );
const newLabel = inputsForRename[index].value; const newLabel = inputsForRename[index].value;
inputsForRename[index].readOnly = true; inputsForRename[index].readOnly = true;
let url = `/book/${bookId}/${collectionId}/${subCollectionId}/edit`; let url = `/book/${bookId}/${subCollectionId}/edit`;
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: 'POST',

View File

@ -468,7 +468,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
).save() ).save()
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit", f"/book/{book.id}/{sub_collection.id}/edit",
data=dict( data=dict(
label="Test SubCollection #2 Label", label="Test SubCollection #2 Label",
), ),
@ -482,7 +482,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
new_about = "Test SubCollection #1 About(edited)" new_about = "Test SubCollection #1 About(edited)"
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit", f"/book/{book.id}/{sub_collection.id}/edit",
data=dict( data=dict(
label=new_label, label=new_label,
about=new_about, about=new_about,
@ -499,7 +499,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
assert edited_collection assert edited_collection
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/9999/edit", f"/book/{book.id}/9999/edit",
data=dict( data=dict(
label=new_label, label=new_label,
about=new_about, about=new_about,
@ -508,10 +508,10 @@ 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"Collection 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}/{sub_collection.id}/delete",
follow_redirects=True, follow_redirects=True,
) )
@ -523,12 +523,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
check_if_nested_collection_entities_is_deleted(deleted_collection) check_if_nested_collection_entities_is_deleted(deleted_collection)
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete", f"/book/{book.id}/{sub_collection.id}/delete",
follow_redirects=True, follow_redirects=True,
) )
assert response.status_code == 200 assert response.status_code == 200
assert b"Subcollection not found" in response.data assert b"Collection not found" in response.data
def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner): def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):