diff --git a/app/views/book.py b/app/views/book.py index 67b0e51..3dcdace 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -377,8 +377,16 @@ def collection_create(book_id: int, collection_id: int | None = None): if form.validate_on_submit(): label = form.label.data collection: m.Collection = m.Collection.query.filter_by( - is_deleted=False, label=label, version_id=book.versions[-1].id - ).first() + is_deleted=False, + label=label, + ) + if collection_id: + collection = collection.filter_by(parrent_id=collection_id) + else: + collection = collection.filter_by( + parrent_id=book.versions[-1].root_collection.id + ) + collection = collection.first() if collection: log( @@ -461,12 +469,19 @@ 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_to_edit.id) - if ( - m.Collection.query.filter_by(label=label, version_id=book.versions[-1].id) - .filter(m.Collection.id != collection_to_edit.id) - .first() - ): + if sub_collection_id: + collection_query = collection_query.filter_by(parrent_id=collection_id) + else: + collection_query = collection_query.filter_by( + parrent_id=collection_to_edit.parrent.id + ) + + if collection_query.first(): log( log.INFO, "Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]", diff --git a/tests/test_book.py b/tests/test_book.py index aee9925..13bfb39 100644 --- a/tests/test_book.py +++ b/tests/test_book.py @@ -219,7 +219,9 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner): label="Test Collection #1 Label" ).first() m.Collection( - label="Test Collection #2 Label", version_id=collection.version_id + label="Test Collection #2 Label", + version_id=collection.version_id, + parrent_id=collection.parrent_id, ).save() response: Response = client.post( @@ -300,6 +302,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner): label="Test Leaf Collection #1 Label", version_id=book.versions[-1].id, is_leaf=True, + parrent_id=book.versions[-1].root_collection.id, ).save() collection: m.Collection = m.Collection( label="Test Collection #1 Label", version_id=book.versions[-1].id @@ -329,7 +332,9 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner): response: Response = client.post( f"/book/{book.id}/{collection.id}/create_sub_collection", - data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"), + data=dict( + label="Test SubCollection #1 Label", about="Test SubCollection #1 About" + ), follow_redirects=True, ) @@ -400,7 +405,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner): assert response.status_code == 200 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, sub_collection.id) assert deleted_collection.is_deleted response: Response = client.post(