Files
tsumugu/backend/app/content/documents.py
T
LarsandCursor ad6240a5da
Deploy Development / deploy (push) Successful in 26s
Test Suite / pytest-backend (push) Successful in 1m4s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 8s
Unterscheide Eingang und Ausgang an der Einbindung und ermoegliche Umbenennen, Archivieren, Loeschen und Verschieben.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-17 18:30:26 +02:00

31 lines
1.4 KiB
Python

"""Bounded note documents. Paragraphs or one Markdown block are stored."""
from __future__ import annotations
from app.core.errors import unprocessable
def clean_document(document: dict) -> dict:
blocks = document.get("blocks")
if not isinstance(blocks, list) or len(blocks) > 400:
raise unprocessable("Die Notiz hat kein gültiges Dokument.")
if len(blocks) == 1 and isinstance(blocks[0], dict) and blocks[0].get("type") == "markdown":
text = blocks[0].get("text")
if not isinstance(text, str) or len(text) > 100_000:
raise unprocessable("Die Notiz enthält einen nicht unterstützten Block.")
return {"version": 1, "blocks": [{"type": "markdown", "text": text}]}
clean = []
for block in blocks:
if not isinstance(block, dict) or set(block) - {"type", "text"}:
raise unprocessable("Die Notiz enthält einen nicht unterstützten Block.")
if block.get("type") != "paragraph" or not isinstance(block.get("text"), str):
raise unprocessable("Die Notiz enthält einen nicht unterstützten Block.")
if len(block["text"]) > 20000:
raise unprocessable("Ein Absatz ist zu lang.")
clean.append({"type": "paragraph", "text": block["text"]})
return {"version": 1, "blocks": clean}
def document_text(document: dict) -> str:
return "\n".join(block["text"] for block in document.get("blocks", []))