change section collection

This commit is contained in:
SvyatoslavArtymovych 2023-06-02 14:32:33 +03:00
parent f6e50a1184
commit 5169ab7f30
4 changed files with 148374 additions and 18 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -133,16 +133,47 @@ def section_delete(
def change_section_position(book_id: int, section_id: int): def change_section_position(book_id: int, section_id: int):
section: m.Section = db.session.get(m.Section, section_id) section: m.Section = db.session.get(m.Section, section_id)
new_position = request.json.get("position") new_position = request.json.get("position")
collection_id = request.json.get("collection_id")
sections_to_edit = m.Section.query.filter( collection: m.Collection = section.collection
m.Section.collection_id == section.collection.id, if collection_id is not None:
m.Section.position >= new_position, collection: m.Collection = db.session.get(m.Collection, collection_id)
).all() if not collection:
for child in sections_to_edit: log(log.INFO, "Collection with id [%s] not found", collection_id)
child: m.Section return {"message": "collection not found"}, 404
if child.position >= new_position:
child.position += 1 log(
child.save(False) log.INFO,
section.position = new_position "Change section [%s] collection_id to [%s]",
section,
collection_id,
)
section.collection_id = collection_id
if collection.active_sections:
sections_to_edit = m.Section.query.filter(
m.Section.collection_id == collection.id,
m.Section.position >= new_position,
).all()
if sections_to_edit:
log(log.INFO, "Calculate new positions of sections in [%s]", collection)
for child in sections_to_edit:
child: m.Section
if child.position >= new_position:
child.position += 1
child.save(False)
log(log.INFO, "Set new position [%s] of section [%s]", new_position, section)
section.position = new_position
else:
log(
log.INFO,
"Collection [%s] does not have active sections. Set section [%s] position to 1",
collection,
section,
)
section.position = 1
log(log.INFO, "Apply position changes on [%s]", section)
section.save() section.save()
return {} return {}

View File

@ -79,10 +79,12 @@ def test_change_section_ordering(client):
root_collection = m.Collection.query.filter_by(is_root=True).first() root_collection = m.Collection.query.filter_by(is_root=True).first()
assert root_collection assert root_collection
assert root_collection.is_root assert root_collection.is_root
collection_1, _ = create_sub_collection(client, book.id, root_collection.id)
collection_2, _ = create_sub_collection(client, book.id, root_collection.id)
current_ordering = {} # collection_id : position current_ordering = {} # collection_id : position
for position in range(0, 10): for position in range(0, 10):
section, _ = create_section(client, book.id, root_collection.id) section, _ = create_section(client, book.id, collection_1.id)
assert section.position == position assert section.position == position
current_ordering[section.id] = section.position current_ordering[section.id] = section.position
@ -92,9 +94,7 @@ def test_change_section_ordering(client):
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{section.id}/section/change_position", f"/book/{book.id}/{section.id}/section/change_position",
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
json=dict( json=dict(position=new_position),
position=new_position,
),
follow_redirects=True, follow_redirects=True,
) )
@ -102,8 +102,28 @@ def test_change_section_ordering(client):
section: m.Section = db.session.get(m.Section, 3) section: m.Section = db.session.get(m.Section, 3)
assert current_ordering[section.id] != section.position assert current_ordering[section.id] != section.position
assert section.position == new_position assert section.position == new_position
for section in m.Section.query.filter_by(collection_id=root_collection.id).all(): for section in m.Section.query.filter_by(collection_id=collection_1.id).all():
if section.position < new_position: if section.position < new_position:
assert current_ordering[section.id] == section.position assert current_ordering[section.id] == section.position
elif section.position > new_position: elif section.position > new_position:
assert current_ordering[section.id] + 1 == section.position assert current_ordering[section.id] + 1 == section.position
new_position = 999
assert section.collection_id == collection_1.id
assert not len(collection_2.active_sections)
response: Response = client.post(
f"/book/{book.id}/{section.id}/section/change_position",
headers={"Content-Type": "application/json"},
json=dict(position=new_position, collection_id=collection_2.id),
follow_redirects=True,
)
assert response.status_code == 200
section: m.Section = db.session.get(m.Section, section.id)
assert section.collection_id != collection_1.id
assert section.collection_id == collection_2.id
collection: m.Collection = section.collection
assert len(collection.active_sections) == 1
assert section.position != new_position
assert section.position == 1