mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-10 06:46:04 +00:00
Merge pull request #85 from Simple2B/svyat/routes_collection
refactor collection routes
This commit is contained in:
commit
3131360ede
File diff suppressed because one or more lines are too long
@ -5,7 +5,7 @@
|
||||
<!-- Modal content -->
|
||||
<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">
|
||||
{{ form_hidden_tag() }}
|
||||
<input type="hidden" name="collection_id" id="delete_sub_collection_modal_collection_id" value="" />
|
||||
|
@ -4,12 +4,9 @@
|
||||
<div class="relative w-full max-w-2xl max-h-full">
|
||||
<!-- Modal content -->
|
||||
<form
|
||||
{% if sub_collection %}
|
||||
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id) }}"
|
||||
{% 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">
|
||||
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id) }}"
|
||||
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700"
|
||||
>
|
||||
{{ form_hidden_tag() }}
|
||||
<!-- Modal header -->
|
||||
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
|
||||
|
@ -34,28 +34,8 @@ def collection_view(book_id: int):
|
||||
|
||||
@bp.route("/<int:book_id>/<int:collection_id>/subcollections", methods=["GET"])
|
||||
def sub_collection_view(book_id: int, collection_id: int):
|
||||
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_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,
|
||||
)
|
||||
# TODO REMOVE ME
|
||||
return {"REMOVE ME": "REMOVE ME"}
|
||||
|
||||
|
||||
@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")
|
||||
return redirect(
|
||||
url_for(
|
||||
"book.sub_collection_view",
|
||||
"book.collection_view",
|
||||
book_id=book_id,
|
||||
collection_id=collection_id,
|
||||
)
|
||||
)
|
||||
|
||||
redirect_url = url_for(
|
||||
"book.sub_collection_view", book_id=book_id, collection_id=collection_id
|
||||
)
|
||||
|
||||
form = f.CreateCollectionForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
@ -114,7 +90,7 @@ def collection_create(book_id: int, collection_id: int | None = None):
|
||||
label=label,
|
||||
about=form.about.data,
|
||||
parent_id=book.versions[-1].root_collection.id,
|
||||
version_id=book.versions[-1].id,
|
||||
version_id=book.last_version.id,
|
||||
)
|
||||
if 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>/<int:sub_collection_id>/edit", methods=["POST"]
|
||||
)
|
||||
@register_book_verify_route(bp.name)
|
||||
@login_required
|
||||
def collection_edit(
|
||||
book_id: int, collection_id: int, sub_collection_id: int | None = None
|
||||
):
|
||||
def collection_edit(book_id: int, collection_id: int):
|
||||
book: m.Book = db.session.get(m.Book, book_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()
|
||||
redirect_url = url_for(
|
||||
@ -158,19 +127,15 @@ def collection_edit(
|
||||
|
||||
if form.validate_on_submit():
|
||||
label = form.label.data
|
||||
collection_query: m.Collection = m.Collection.query.filter_by(
|
||||
is_deleted=False,
|
||||
label=label,
|
||||
).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
|
||||
existing_collection: m.Collection = (
|
||||
m.Collection.query.filter_by(
|
||||
is_deleted=False, label=label, parent_id=collection.parent.id
|
||||
)
|
||||
.filter(m.Collection.id != collection.id)
|
||||
.first()
|
||||
)
|
||||
|
||||
if collection_query.first():
|
||||
if existing_collection:
|
||||
log(
|
||||
log.INFO,
|
||||
"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>/<int:sub_collection_id>/delete",
|
||||
methods=["POST"],
|
||||
)
|
||||
@register_book_verify_route(bp.name)
|
||||
@login_required
|
||||
def collection_delete(
|
||||
book_id: int, collection_id: int, sub_collection_id: int | None = None
|
||||
):
|
||||
def collection_delete(book_id: int, collection_id: int):
|
||||
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
|
||||
if collection.children:
|
||||
|
@ -36,14 +36,13 @@ export function deleteSubCollection() {
|
||||
}
|
||||
deleteSubCollectionModalBtns.forEach(btn =>
|
||||
btn.addEventListener('click', () => {
|
||||
const collectionId = btn.getAttribute('data-collection-id');
|
||||
const subCollectionId = btn.getAttribute('data-sub-collection-id');
|
||||
collectionIdInDeleteSubCollectionModal.value = collectionId;
|
||||
collectionIdInDeleteSubCollectionModal.value = subCollectionId;
|
||||
let newActionPath: string = '';
|
||||
|
||||
newActionPath = defaultActionPath.replace(
|
||||
'0/0/delete',
|
||||
`${collectionId}/${subCollectionId}/delete`,
|
||||
'0/delete',
|
||||
`${subCollectionId}/delete`,
|
||||
);
|
||||
|
||||
deleteSubCollectionForm.setAttribute('action', `${newActionPath}`);
|
||||
|
@ -23,15 +23,13 @@ export function renameSubCollection() {
|
||||
e.preventDefault();
|
||||
const bookId =
|
||||
subCollectionRenameForms[index].getAttribute('data-book-id');
|
||||
const collectionId =
|
||||
subCollectionRenameForms[index].getAttribute('data-collection-id');
|
||||
const subCollectionId = subCollectionRenameForms[index].getAttribute(
|
||||
'data-sub-collection-id',
|
||||
);
|
||||
const newLabel = inputsForRename[index].value;
|
||||
inputsForRename[index].readOnly = true;
|
||||
|
||||
let url = `/book/${bookId}/${collectionId}/${subCollectionId}/edit`;
|
||||
let url = `/book/${bookId}/${subCollectionId}/edit`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
|
@ -468,7 +468,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
).save()
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
|
||||
f"/book/{book.id}/{sub_collection.id}/edit",
|
||||
data=dict(
|
||||
label="Test SubCollection #2 Label",
|
||||
),
|
||||
@ -482,7 +482,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
new_about = "Test SubCollection #1 About(edited)"
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
|
||||
f"/book/{book.id}/{sub_collection.id}/edit",
|
||||
data=dict(
|
||||
label=new_label,
|
||||
about=new_about,
|
||||
@ -499,7 +499,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
assert edited_collection
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/9999/edit",
|
||||
f"/book/{book.id}/9999/edit",
|
||||
data=dict(
|
||||
label=new_label,
|
||||
about=new_about,
|
||||
@ -508,10 +508,10 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
)
|
||||
|
||||
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(
|
||||
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
|
||||
f"/book/{book.id}/{sub_collection.id}/delete",
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
@ -523,12 +523,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
check_if_nested_collection_entities_is_deleted(deleted_collection)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user