Merge pull request #56 from Simple2B/svyat/fix/deleting_collections

Deleting nested book items
This commit is contained in:
Костя Столярский 2023-05-17 16:42:19 +03:00 committed by GitHub
commit e9ff1e8511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 3 deletions

View File

@ -0,0 +1,65 @@
from app import models as m
from app.logger import log
def delete_nested_book_entities(book: m.Book):
for version in book.versions:
version: m.BookVersion
version.is_deleted = True
log(log.INFO, "Delete version [%s]", version.id)
version.save(False)
delete_nested_version_entities(version)
def delete_nested_version_entities(book_version: m.BookVersion):
root_collection: m.Collection = book_version.root_collection
root_collection.is_deleted = True
log(log.INFO, "Delete root collection [%s]", root_collection.id)
root_collection.save(False)
for collection in root_collection.children:
collection: m.Collection
collection.is_deleted = True
log(log.INFO, "Delete collection [%s]", collection.id)
collection.save(False)
delete_nested_collection_entities(collection)
def delete_nested_collection_entities(collection: m.Collection):
for section in collection.sections:
section: m.Section
section.is_deleted = True
log(log.INFO, "Delete section [%s]", section.id)
section.save(False)
delete_nested_section_entities(section)
def delete_nested_section_entities(section: m.Section):
for interpretation in section.interpretations:
interpretation: m.Interpretation
interpretation.is_deleted = True
log(log.INFO, "Delete interpretation [%s]", interpretation.id)
interpretation.save(False)
delete_nested_interpretation_entities(interpretation)
def delete_nested_interpretation_entities(interpretation: m.Interpretation):
for comment in interpretation.comments:
comment: m.Comment
comment.is_deleted = True
log(log.INFO, "Delete comment [%s]", comment.id)
comment.save(False)
delete_nested_comment_entities(comment)
def delete_nested_comment_entities(comment: m.Comment):
for child in comment.children:
child: m.Comment
child.is_deleted = True
log(log.INFO, "Delete sub comment [%s]", comment.id)
child.save(False)

View File

@ -14,6 +14,13 @@ from app.controllers import (
register_book_verify_route,
book_validator,
)
from app.controllers.delete_nested_book_entities import (
delete_nested_book_entities,
delete_nested_collection_entities,
delete_nested_section_entities,
delete_nested_interpretation_entities,
delete_nested_comment_entities,
)
from app import models as m, db, forms as f
from app.logger import log
@ -127,6 +134,7 @@ def delete(book_id: int):
return redirect(url_for("book.my_library"))
book.is_deleted = True
delete_nested_book_entities(book)
log(log.INFO, "Book deleted: [%s]", book)
book.save()
flash("Success!", "success")
@ -604,8 +612,13 @@ def collection_delete(
collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
collection.is_deleted = True
log(log.INFO, "Delete collection [%s]", collection.id)
if collection.children:
for child in collection.children:
child: m.Collection
delete_nested_collection_entities(child)
log(log.INFO, "Delete subcollection [%s]", collection.id)
child.save()
delete_nested_collection_entities(collection)
collection.save()
flash("Success!", "success")
@ -745,6 +758,7 @@ def section_delete(
section: m.Section = db.session.get(m.Section, section_id)
section.is_deleted = True
delete_nested_section_entities(section)
if not collection.active_sections:
log(
log.INFO,
@ -901,6 +915,8 @@ def interpretation_delete(
)
interpretation.is_deleted = True
delete_nested_interpretation_entities(interpretation)
log(log.INFO, "Delete interpretation [%s]", interpretation)
interpretation.save()
@ -1133,6 +1149,7 @@ def comment_delete(
if form.validate_on_submit():
comment.is_deleted = True
delete_nested_comment_entities(comment)
log(log.INFO, "Delete comment [%s]", comment)
comment.save()

View File

@ -3,7 +3,14 @@ from flask import current_app as Response
from flask.testing import FlaskClient, FlaskCliRunner
from app import models as m, db
from tests.utils import login, logout
from tests.utils import (
login,
logout,
check_if_nested_book_entities_is_deleted,
check_if_nested_collection_entities_is_deleted,
check_if_nested_section_entities_is_deleted,
check_if_nested_interpretation_entities_is_deleted,
)
def test_create_edit_delete_book(client: FlaskClient):
@ -100,6 +107,7 @@ def test_create_edit_delete_book(client: FlaskClient):
assert b"Success!" in response.data
book = db.session.get(m.Book, book.id)
assert book.is_deleted == True
check_if_nested_book_entities_is_deleted(book)
def test_add_contributor(client: FlaskClient):
@ -363,6 +371,7 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
assert deleted_collection.is_deleted
check_if_nested_collection_entities_is_deleted(deleted_collection)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/delete",
@ -511,6 +520,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
deleted_collection: m.Collection = db.session.get(m.Collection, sub_collection.id)
assert deleted_collection.is_deleted
check_if_nested_collection_entities_is_deleted(deleted_collection)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
@ -745,6 +755,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
deleted_section: m.Section = db.session.get(m.Section, section.id)
assert deleted_section.is_deleted
check_if_nested_section_entities_is_deleted(deleted_section)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/delete_section",
@ -756,6 +767,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
deleted_section: m.Section = db.session.get(m.Section, section_2.id)
assert deleted_section.is_deleted
check_if_nested_section_entities_is_deleted(deleted_section)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/delete_section",
@ -935,6 +947,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
m.Interpretation, section_in_subcollection.interpretations[0].id
)
assert deleted_interpretation.is_deleted
check_if_nested_interpretation_entities_is_deleted(deleted_interpretation)
response: Response = client.post(
(
@ -951,6 +964,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
m.Interpretation, section_in_collection.interpretations[0].id
)
assert deleted_interpretation.is_deleted
check_if_nested_interpretation_entities_is_deleted(deleted_interpretation)
def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):

View File

@ -56,3 +56,58 @@ def create_test_book(owner_id: int, entity_id: int = randint(1, 100)):
user_id=owner_id,
interpretation_id=interpretation.id,
).save()
def check_if_nested_book_entities_is_deleted(book: m.Book, is_deleted: bool = True):
for version in book.versions:
version: m.BookVersion
assert version.is_deleted == is_deleted
check_if_nested_version_entities_is_deleted(version)
def check_if_nested_version_entities_is_deleted(
book_version: m.BookVersion, is_deleted: bool = True
):
root_collection: m.Collection = book_version.root_collection
assert root_collection.is_deleted == is_deleted
for collection in root_collection.children:
collection: m.Collection
assert collection.is_deleted == is_deleted
check_if_nested_collection_entities_is_deleted(collection)
def check_if_nested_collection_entities_is_deleted(
collection: m.Collection, is_deleted: bool = True
):
for section in collection.sections:
section: m.Section
assert section.is_deleted == is_deleted
check_if_nested_section_entities_is_deleted(section, is_deleted)
def check_if_nested_section_entities_is_deleted(
section: m.Section, is_deleted: bool = True
):
for interpretation in section.interpretations:
interpretation: m.Interpretation
assert interpretation.is_deleted == is_deleted
check_if_nested_interpretation_entities_is_deleted(interpretation, is_deleted)
def check_if_nested_interpretation_entities_is_deleted(
interpretation: m.Interpretation, is_deleted: bool = True
):
for comment in interpretation.comments:
comment: m.Comment
assert comment.is_deleted == is_deleted
def check_if_nested_comment_entities_is_deleted(
comment: m.Comment, is_deleted: bool = True
):
for child in comment.children:
child: m.Comment
assert child.is_deleted == is_deleted