mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-21 20:30:28 +00:00
change section collection
This commit is contained in:
parent
f6e50a1184
commit
5169ab7f30
File diff suppressed because one or more lines are too long
144973
app/static/js/main.js
144973
app/static/js/main.js
File diff suppressed because one or more lines are too long
@ -133,16 +133,47 @@ def section_delete(
|
||||
def change_section_position(book_id: int, section_id: int):
|
||||
section: m.Section = db.session.get(m.Section, section_id)
|
||||
new_position = request.json.get("position")
|
||||
collection_id = request.json.get("collection_id")
|
||||
|
||||
sections_to_edit = m.Section.query.filter(
|
||||
m.Section.collection_id == section.collection.id,
|
||||
m.Section.position >= new_position,
|
||||
).all()
|
||||
for child in sections_to_edit:
|
||||
child: m.Section
|
||||
if child.position >= new_position:
|
||||
child.position += 1
|
||||
child.save(False)
|
||||
section.position = new_position
|
||||
collection: m.Collection = section.collection
|
||||
if collection_id is not None:
|
||||
collection: m.Collection = db.session.get(m.Collection, collection_id)
|
||||
if not collection:
|
||||
log(log.INFO, "Collection with id [%s] not found", collection_id)
|
||||
return {"message": "collection not found"}, 404
|
||||
|
||||
log(
|
||||
log.INFO,
|
||||
"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()
|
||||
return {}
|
||||
|
@ -79,10 +79,12 @@ def test_change_section_ordering(client):
|
||||
root_collection = m.Collection.query.filter_by(is_root=True).first()
|
||||
assert root_collection
|
||||
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
|
||||
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
|
||||
current_ordering[section.id] = section.position
|
||||
|
||||
@ -92,9 +94,7 @@ def test_change_section_ordering(client):
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{section.id}/section/change_position",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json=dict(
|
||||
position=new_position,
|
||||
),
|
||||
json=dict(position=new_position),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
@ -102,8 +102,28 @@ def test_change_section_ordering(client):
|
||||
section: m.Section = db.session.get(m.Section, 3)
|
||||
assert current_ordering[section.id] != section.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:
|
||||
assert current_ordering[section.id] == section.position
|
||||
elif section.position > new_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
|
||||
|
Loading…
x
Reference in New Issue
Block a user