Verkuerze die Inhaltsliste und fuehre Unterkapitel ein, damit Richtung, Bezuge und Struktur ohne Zusatzzeilen bedienbar bleiben.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
eeeb81148a
commit
e4380261fc
30 files changed
+2384
-430
No files matched your search
+211
-19
@@ -37,6 +37,8 @@ from app.core.errors import forbidden, not_found, unprocessable
|
||||
from app.core.timeutil import utcnow
|
||||
from app.media.storage import asset_path, display_filename, sha256, sniff, write_bytes
|
||||
|
||||
MAX_CHAPTER_DEPTH = 32
|
||||
|
||||
|
||||
def _ctx(value) -> Ctx | None:
|
||||
if value is None:
|
||||
@@ -121,6 +123,105 @@ def patch_case(conn, actor: UUID, case_id: UUID, expected: int, title: str | Non
|
||||
return updated
|
||||
|
||||
|
||||
def _chapter_column(kind: str) -> str:
|
||||
return "case_id" if kind == "case" else "matter_id"
|
||||
|
||||
|
||||
def _parent_chain(conn, chapter_id: UUID | None) -> list[UUID]:
|
||||
chain: list[UUID] = []
|
||||
current = chapter_id
|
||||
seen: set[UUID] = set()
|
||||
while current and current not in seen:
|
||||
seen.add(current)
|
||||
chain.append(current)
|
||||
row = conn.execute("SELECT parent_chapter_id FROM chapters WHERE id = %s", (current,)).fetchone()
|
||||
current = row["parent_chapter_id"] if row else None
|
||||
return chain
|
||||
|
||||
|
||||
def _subtree_height(conn, chapter_id: UUID) -> int:
|
||||
rows = conn.execute("SELECT id FROM chapters WHERE parent_chapter_id = %s", (chapter_id,)).fetchall()
|
||||
if not rows:
|
||||
return 1
|
||||
return 1 + max(_subtree_height(conn, row["id"]) for row in rows)
|
||||
|
||||
|
||||
def _assert_chapter_parent(
|
||||
conn,
|
||||
kind: str,
|
||||
container_id: UUID,
|
||||
parent_id: UUID | None,
|
||||
moving_id: UUID | None = None,
|
||||
) -> None:
|
||||
if parent_id is None:
|
||||
return
|
||||
if moving_id is not None and parent_id == moving_id:
|
||||
raise unprocessable("Ein Kapitel kann nicht unter sich selbst stehen.")
|
||||
column = _chapter_column(kind)
|
||||
parent = conn.execute(
|
||||
f"SELECT id FROM chapters WHERE id = %s AND {column} = %s",
|
||||
(parent_id, container_id),
|
||||
).fetchone()
|
||||
if parent is None:
|
||||
raise unprocessable("Das Elternkapitel gehört nicht zu diesem Kontext.")
|
||||
if moving_id is not None and moving_id in _parent_chain(conn, parent_id):
|
||||
raise unprocessable("Diese Zuordnung würde einen Kreis erzeugen.")
|
||||
extra = _subtree_height(conn, moving_id) if moving_id is not None else 1
|
||||
if len(_parent_chain(conn, parent_id)) + extra > MAX_CHAPTER_DEPTH:
|
||||
raise unprocessable(
|
||||
f"Die Kapiteltiefe überschreitet die technische Schutzgrenze von {MAX_CHAPTER_DEPTH} Ebenen."
|
||||
)
|
||||
|
||||
|
||||
def _sibling_ids(conn, kind: str, container_id: UUID, parent_id: UUID | None) -> list[UUID]:
|
||||
column = _chapter_column(kind)
|
||||
if parent_id is None:
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT id FROM chapters
|
||||
WHERE {column} = %s AND parent_chapter_id IS NULL
|
||||
ORDER BY position, id
|
||||
""",
|
||||
(container_id,),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT id FROM chapters
|
||||
WHERE {column} = %s AND parent_chapter_id = %s
|
||||
ORDER BY position, id
|
||||
""",
|
||||
(container_id, parent_id),
|
||||
).fetchall()
|
||||
return [row["id"] for row in rows]
|
||||
|
||||
|
||||
def _renumber_chapters(conn, chapter_ids: list[UUID]) -> None:
|
||||
for index, chapter_id in enumerate(chapter_ids):
|
||||
conn.execute("UPDATE chapters SET position = %s WHERE id = %s", (index, chapter_id))
|
||||
|
||||
|
||||
def _next_sibling_position(conn, kind: str, container_id: UUID, parent_id: UUID | None) -> int:
|
||||
column = _chapter_column(kind)
|
||||
if parent_id is None:
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT COALESCE(MAX(position), -1) + 1 AS pos FROM chapters
|
||||
WHERE {column} = %s AND parent_chapter_id IS NULL
|
||||
""",
|
||||
(container_id,),
|
||||
).fetchone()
|
||||
else:
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT COALESCE(MAX(position), -1) + 1 AS pos FROM chapters
|
||||
WHERE {column} = %s AND parent_chapter_id = %s
|
||||
""",
|
||||
(container_id, parent_id),
|
||||
).fetchone()
|
||||
return int(row["pos"])
|
||||
|
||||
|
||||
def create_chapter(
|
||||
conn,
|
||||
actor: UUID,
|
||||
@@ -130,33 +231,30 @@ def create_chapter(
|
||||
position: int | None,
|
||||
context: Ctx | None,
|
||||
document_on: date | None = None,
|
||||
parent_chapter_id: UUID | None = None,
|
||||
) -> dict:
|
||||
if kind == "case":
|
||||
container, _rights = require_case(conn, actor, container_id, "edit")
|
||||
else:
|
||||
container, _rights = require_matter(conn, actor, container_id, context, "edit")
|
||||
_assert_chapter_parent(conn, kind, container_id, parent_chapter_id)
|
||||
if position is None:
|
||||
column = "case_id" if kind == "case" else "matter_id"
|
||||
current = conn.execute(
|
||||
f"SELECT COALESCE(MAX(position), -1) + 1 AS pos FROM chapters WHERE {column} = %s",
|
||||
(container_id,),
|
||||
).fetchone()
|
||||
position = int(current["pos"])
|
||||
position = _next_sibling_position(conn, kind, container_id, parent_chapter_id)
|
||||
if kind == "case":
|
||||
row = conn.execute(
|
||||
"""
|
||||
INSERT INTO chapters (home_scope_id, case_id, title, position, document_on)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING *
|
||||
INSERT INTO chapters (home_scope_id, case_id, title, position, document_on, parent_chapter_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s) RETURNING *
|
||||
""",
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on),
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on, parent_chapter_id),
|
||||
).fetchone()
|
||||
else:
|
||||
row = conn.execute(
|
||||
"""
|
||||
INSERT INTO chapters (home_scope_id, matter_id, title, position, document_on)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING *
|
||||
INSERT INTO chapters (home_scope_id, matter_id, title, position, document_on, parent_chapter_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s) RETURNING *
|
||||
""",
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on),
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on, parent_chapter_id),
|
||||
).fetchone()
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_created", kind, container_id, context)
|
||||
return row
|
||||
@@ -182,16 +280,28 @@ def reorder_chapters(
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
version = bump_version(conn, table, container_id, expected)
|
||||
rows = conn.execute(f"SELECT id FROM chapters WHERE {column} = %s", (container_id,)).fetchall()
|
||||
existing = [row["id"] for row in rows]
|
||||
rows = conn.execute(
|
||||
f"SELECT id, parent_chapter_id FROM chapters WHERE {column} = %s",
|
||||
(container_id,),
|
||||
).fetchall()
|
||||
existing = {row["id"]: row["parent_chapter_id"] for row in rows}
|
||||
if len(chapter_ids) != len(set(chapter_ids)):
|
||||
raise unprocessable("Dasselbe Kapitel steht mehrfach in der Ordnung.")
|
||||
if any(item not in set(existing) for item in chapter_ids):
|
||||
if any(item not in existing for item in chapter_ids):
|
||||
raise not_found()
|
||||
if set(chapter_ids) != set(existing):
|
||||
if not chapter_ids:
|
||||
if existing:
|
||||
raise unprocessable("Die Kapitelordnung ist unvollständig. Bitte neu laden.")
|
||||
audit(conn, actor, container["home_scope_id"], "chapters_reordered", kind, container_id, context)
|
||||
return version
|
||||
parents = {existing[item] for item in chapter_ids}
|
||||
if len(parents) != 1:
|
||||
raise unprocessable("Die Kapitelordnung mischt verschiedene Ebenen.")
|
||||
parent_id = next(iter(parents))
|
||||
siblings = {item for item, parent in existing.items() if parent == parent_id}
|
||||
if set(chapter_ids) != siblings:
|
||||
raise unprocessable("Die Kapitelordnung ist unvollständig. Bitte neu laden.")
|
||||
for index, chapter_id in enumerate(chapter_ids):
|
||||
conn.execute("UPDATE chapters SET position = %s WHERE id = %s", (index, chapter_id))
|
||||
_renumber_chapters(conn, chapter_ids)
|
||||
audit(conn, actor, container["home_scope_id"], "chapters_reordered", kind, container_id, context)
|
||||
return version
|
||||
|
||||
@@ -315,14 +425,96 @@ def rename_chapter(conn, actor: UUID, chapter_id: UUID, expected: int, title: st
|
||||
|
||||
|
||||
def delete_chapter(conn, actor: UUID, chapter_id: UUID, expected: int, context: Ctx | None) -> int:
|
||||
"""Dissolve one chapter. Direct bindings and direct children move to its parent."""
|
||||
chapter, container, kind = _chapter_container(conn, actor, chapter_id, context)
|
||||
version = bump_version(conn, "cases" if kind == "case" else "matters", container["id"], expected)
|
||||
conn.execute("UPDATE bindings SET chapter_id = NULL WHERE chapter_id = %s", (chapter["id"],))
|
||||
parent_id = chapter["parent_chapter_id"]
|
||||
children = _sibling_ids(conn, kind, container["id"], chapter["id"])
|
||||
siblings = _sibling_ids(conn, kind, container["id"], parent_id)
|
||||
merged: list[UUID] = []
|
||||
for item in siblings:
|
||||
if item == chapter["id"]:
|
||||
merged.extend(children)
|
||||
else:
|
||||
merged.append(item)
|
||||
if children:
|
||||
conn.execute(
|
||||
"UPDATE chapters SET parent_chapter_id = %s WHERE parent_chapter_id = %s",
|
||||
(parent_id, chapter["id"]),
|
||||
)
|
||||
_renumber_chapters(conn, merged)
|
||||
conn.execute("UPDATE bindings SET chapter_id = %s WHERE chapter_id = %s", (parent_id, chapter["id"]))
|
||||
conn.execute("DELETE FROM chapters WHERE id = %s", (chapter["id"],))
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_deleted", kind, container["id"], context)
|
||||
return version
|
||||
|
||||
|
||||
def move_chapter_parent(
|
||||
conn,
|
||||
actor: UUID,
|
||||
chapter_id: UUID,
|
||||
expected: int,
|
||||
parent_id: UUID | None,
|
||||
context: Ctx | None,
|
||||
) -> int:
|
||||
"""Reparent one chapter inside its own container. The subtree stays together."""
|
||||
chapter, container, kind = _chapter_container(conn, actor, chapter_id, context)
|
||||
if chapter["parent_chapter_id"] == parent_id:
|
||||
return bump_version(conn, "cases" if kind == "case" else "matters", container["id"], expected)
|
||||
_assert_chapter_parent(conn, kind, container["id"], parent_id, chapter["id"])
|
||||
version = bump_version(conn, "cases" if kind == "case" else "matters", container["id"], expected)
|
||||
old_siblings = [item for item in _sibling_ids(conn, kind, container["id"], chapter["parent_chapter_id"]) if item != chapter["id"]]
|
||||
_renumber_chapters(conn, old_siblings)
|
||||
position = len(_sibling_ids(conn, kind, container["id"], parent_id))
|
||||
conn.execute(
|
||||
"UPDATE chapters SET parent_chapter_id = %s, position = %s WHERE id = %s",
|
||||
(parent_id, position, chapter["id"]),
|
||||
)
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_moved", kind, container["id"], context)
|
||||
return version
|
||||
|
||||
|
||||
def move_bindings_chapter(
|
||||
conn,
|
||||
actor: UUID,
|
||||
kind: str,
|
||||
container_id: UUID,
|
||||
expected: int,
|
||||
binding_ids: list[UUID],
|
||||
chapter_id: UUID | None,
|
||||
context: Ctx | None,
|
||||
) -> int:
|
||||
"""Move visible bindings inside one container. One failure leaves every binding in place."""
|
||||
if kind == "case":
|
||||
container, rights = require_case(conn, actor, container_id, "edit")
|
||||
table = "cases"
|
||||
page = Ctx("case", container_id)
|
||||
else:
|
||||
container, rights = require_matter(conn, actor, container_id, context, "edit")
|
||||
table = "matters"
|
||||
page = context or Ctx("matter", container_id)
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
if len(binding_ids) != len(set(binding_ids)):
|
||||
raise unprocessable("Dieselbe Einbindung steht mehrfach in der Auswahl.")
|
||||
chapter_for(conn, chapter_id, kind, container_id)
|
||||
rows = conn.execute(
|
||||
"SELECT id, element_id FROM bindings WHERE context_kind = %s AND context_id = %s",
|
||||
(kind, container_id),
|
||||
).fetchall()
|
||||
by_id = {row["id"]: row for row in rows}
|
||||
for binding_id in binding_ids:
|
||||
row = by_id.get(binding_id)
|
||||
if row is None:
|
||||
raise not_found()
|
||||
require_element(conn, actor, row["element_id"], page, "read")
|
||||
version = bump_version(conn, table, container_id, expected)
|
||||
for binding_id in binding_ids:
|
||||
conn.execute("UPDATE bindings SET chapter_id = %s WHERE id = %s", (chapter_id, binding_id))
|
||||
audit(conn, actor, container["home_scope_id"], "binding_chapter", kind, container_id, page)
|
||||
return version
|
||||
|
||||
|
||||
def delete_case(conn, actor: UUID, case_id: UUID, expected: int) -> None:
|
||||
case, rights = require_case(conn, actor, case_id, "edit")
|
||||
if not rights.edit:
|
||||
|
||||
Reference in new issue
Block a user