mirror of https://github.com/logos-co/open-law.git
change collection parent
This commit is contained in:
parent
4b9801e4e1
commit
4e290d117a
|
@ -221,16 +221,52 @@ def collection_delete(book_id: int, collection_id: int):
|
|||
def change_collection_position(book_id: int, collection_id: int):
|
||||
collection: m.Collection = db.session.get(m.Collection, collection_id)
|
||||
new_position = request.json.get("position")
|
||||
collection_id = request.json.get("collection_id")
|
||||
|
||||
collections_to_edit = m.Collection.query.filter(
|
||||
m.Collection.parent_id == collection.parent.id,
|
||||
m.Collection.position >= new_position,
|
||||
).all()
|
||||
for child in collections_to_edit:
|
||||
child: m.Collection
|
||||
if child.position >= new_position:
|
||||
child.position += 1
|
||||
child.save(False)
|
||||
collection.position = new_position
|
||||
new_parent: m.Collection = collection.parent
|
||||
if collection_id is not None:
|
||||
new_parent: m.Collection = db.session.get(m.Collection, collection_id)
|
||||
if not new_parent:
|
||||
log(log.INFO, "Collection with id [%s] not found", collection_id)
|
||||
return {"message": "new parent collection not found"}, 404
|
||||
|
||||
log(
|
||||
log.INFO,
|
||||
"Change collection [%s] parent_id to [%s]",
|
||||
collection,
|
||||
collection_id,
|
||||
)
|
||||
collection.parent_id = collection_id
|
||||
|
||||
if new_parent.active_children:
|
||||
collections_to_edit = m.Collection.query.filter(
|
||||
m.Collection.parent_id == new_parent.id,
|
||||
m.Collection.position >= new_position,
|
||||
).all()
|
||||
if collections_to_edit:
|
||||
log(log.INFO, "Calculate new positions of collections in [%s]", collection)
|
||||
for child in collections_to_edit:
|
||||
child: m.Collection
|
||||
if child.position >= new_position:
|
||||
child.position += 1
|
||||
child.save(False)
|
||||
|
||||
log(
|
||||
log.INFO,
|
||||
"Set new position [%s] of collection [%s]",
|
||||
new_position,
|
||||
collection,
|
||||
)
|
||||
collection.position = new_position
|
||||
else:
|
||||
log(
|
||||
log.INFO,
|
||||
"Collection [%s] does not have active collection. Set collection [%s] position to 1",
|
||||
collection,
|
||||
new_parent,
|
||||
)
|
||||
collection.position = 1
|
||||
|
||||
log(log.INFO, "Apply position changes on [%s]", collection)
|
||||
collection.save()
|
||||
return {}
|
||||
return {"message": "success"}
|
||||
|
|
|
@ -176,4 +176,4 @@ def change_section_position(book_id: int, section_id: int):
|
|||
|
||||
log(log.INFO, "Apply position changes on [%s]", section)
|
||||
section.save()
|
||||
return {}
|
||||
return {"message": "success"}
|
||||
|
|
|
@ -58,6 +58,30 @@ def test_change_collection_ordering(client):
|
|||
elif collection.position > new_position:
|
||||
assert current_ordering[collection.id] + 1 == collection.position
|
||||
|
||||
collection: m.Collection = db.session.get(m.Collection, 3)
|
||||
collection_1, _ = create_sub_collection(client, book.id, root_collection.id)
|
||||
assert collection.parent_id != collection_1.id
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/collection/change_position",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json=dict(position=999, collection_id=collection_1.id),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
collection: m.Collection = db.session.get(m.Collection, 3)
|
||||
assert collection.parent_id == collection_1.id
|
||||
assert collection.position == 1
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/collection/change_position",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json=dict(position=999, collection_id=999),
|
||||
follow_redirects=True,
|
||||
)
|
||||
assert response.status_code == 404
|
||||
assert response.json["message"] == "new parent collection not found"
|
||||
|
||||
|
||||
def test_ordering_on_section_create(client):
|
||||
login(client)
|
||||
|
|
Loading…
Reference in New Issue