Files
tsumugu/backend/app/library.py
T
LarsandClaude Sonnet 5.5 d39c7d7999 Liefere Zähler, Rauminhalt und Teilen im Stapel für die neue Ablage.
Akten und Vorgänge melden Unterakten, Vorgänge und die Räume, mit denen sie geteilt sind, jeweils nur aus Lesbarem. Neue Endpunkte zeigen die Räume als Kacheln, den Inhalt eines Raums und teilen mehrere Einträge auf einmal oder gar nicht. Beim Anlegen lässt sich ein Eintrag gleich in einen Raum legen.

Die lokale Suite läuft mit 148 Tests grün.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 11:38:02 +02:00

127 lines
4.8 KiB
Python

"""Overviews for the library: cases and matters as tiles, and what lies in a room.
A room is a virtual folder. Cases and matters stay where they are; a share puts them into a room.
Counts only include what the person may read. Hidden entries are neither named nor counted.
"""
from __future__ import annotations
from collections import Counter
from uuid import UUID
from app.access.policy import (
ROOM_RIGHTS,
Ctx,
Rights,
case_rights,
get_case,
list_cases,
list_matters,
matter_rights,
room_name,
)
from app.present import case_summary, matter_summary
def shared_rooms(conn, kind: str, resource_id: UUID) -> list[dict]:
rows = conn.execute(
"""
SELECT r.id, r.name, s.access FROM room_shares s JOIN data_rooms r ON r.id = s.room_id
WHERE s.resource_kind = %s AND s.resource_id = %s
ORDER BY r.name, r.id
""",
(kind, resource_id),
).fetchall()
return [{"id": str(row["id"]), "name": row["name"], "access": row["access"]} for row in rows]
def readable_matter_count(conn, actor: UUID, case: dict) -> int:
rows = conn.execute(
"""
SELECT m.* FROM matter_case_links l JOIN matters m ON m.id = l.matter_id
WHERE l.case_id = %s AND m.deleted_at IS NULL AND m.parent_matter_id IS NULL
""",
(case["id"],),
).fetchall()
context = Ctx("case", case["id"])
return sum(1 for matter in rows if matter_rights(conn, actor, matter, context).read)
def case_items(conn, actor: UUID, visible: list[tuple[dict, Rights]]) -> list[dict]:
children = Counter(case["parent_case_id"] for case, _ in visible if case["parent_case_id"])
items = []
for case, rights in visible:
summary = case_summary(case, rights)
owner = case["owner_subject_id"] == actor
summary["home_scope_name"] = room_name(conn, case["home_scope_id"])
summary["kind_label"] = "Unterakte" if case["parent_case_id"] else "Akte"
summary["parent_title"] = None
summary["is_owner"] = owner
summary["shared_with"] = shared_rooms(conn, "case", case["id"]) if owner else []
summary["child_cases"] = children.get(case["id"], 0)
summary["matter_count"] = readable_matter_count(conn, actor, case)
if case["parent_case_id"]:
parent = get_case(conn, case["parent_case_id"])
if parent is not None and case_rights(conn, actor, parent).read:
summary["parent_title"] = parent["title"]
items.append(summary)
return items
def matter_items(conn, actor: UUID, visible: list[tuple[dict, list[dict]]]) -> list[dict]:
children = Counter(matter["parent_matter_id"] for matter, _ in visible if matter["parent_matter_id"])
items = []
for matter, _contexts in visible:
summary = matter_summary(conn, actor, matter)
summary["child_matters"] = children.get(matter["id"], 0)
items.append(summary)
return items
def room_contents(conn, actor: UUID, room_id: UUID) -> tuple[list[tuple[dict, Rights]], list[tuple[dict, list[dict]]]]:
"""Cases and matters shared with the room that the person may read, with their readable sub-entries."""
rows = conn.execute(
"SELECT resource_kind, resource_id FROM room_shares WHERE room_id = %s",
(room_id,),
).fetchall()
case_ids = {row["resource_id"] for row in rows if row["resource_kind"] == "case"}
matter_ids = {row["resource_id"] for row in rows if row["resource_kind"] == "matter"}
cases = [
(case, rights)
for case, rights in list_cases(conn, actor)
if case["id"] in case_ids
or (case["rights_mode"] == "inherit_parent" and case["parent_case_id"] in case_ids)
]
matters = [
(matter, contexts)
for matter, contexts in list_matters(conn, actor)
if matter["id"] in matter_ids or matter["parent_matter_id"] in matter_ids
]
return cases, matters
def room_effect(conn, room_id: UUID, actor: UUID, access: str) -> list[dict]:
"""Who gets which rights through a share with this room."""
rows = conn.execute(
"""
SELECT m.subject_id, m.role, a.display_name
FROM room_memberships m JOIN auth_accounts a ON a.id = m.subject_id
WHERE m.room_id = %s AND m.status = 'active' AND a.status = 'active' AND m.subject_id <> %s
ORDER BY a.display_name, a.id
""",
(room_id, actor),
).fetchall()
effect = []
for row in rows:
rights = ROOM_RIGHTS.get(row["role"], Rights())
if access == "read":
rights = Rights(read=rights.read)
effect.append(
{
"display_name": row["display_name"],
"role": row["role"],
"rights": {"read": rights.read, "edit": rights.edit},
}
)
return effect