03dd477899
Check Cross-Plugin Imports / check (push) Has been cancelled
- Scope-Deklarationen: tasks only_mine, kommunikation conversation_ids, wiki category_ids (NEUE contracts.py), report_generator template_ids, automation agent_ids (module_key agents), tags tag_ids, unified_search entity_types dynamisch aus Provider-Registry - Core-Beiträge: navigation default_route (Startseite) + dashboard widget_app_ids (Widget-TYP-Angebot, Layout bleibt Phase M) - Backend-Filter (additive UND): /tasks (only_mine), /comm/conversations, /wiki/articles+/categories (Subtree), /reports/print-templates, /agents, /tags, /search GET+POST (entity_types-Schnitt), /miniapps?host=dashboard - apply_entity_type_scope-Helper (requested ∧ scope) - Frontend: WorkspaceSwitcher default_route-Navigation, Sidebar workspace-menu_order-Sortierung, workspaceStore moduleMenuOrder() - Tests: 18/18 Deklarationen + 11/11 Filter (TDD), Frontend 2/2 + Store 18/18, tsc clean, Build OK - Regression 64 passed (4 Kombi-Failures = Suite-Isolation, solo-bewiesen); Checker 0; Ruff = Vorbestand (Stash-bewiesen)
172 lines
6.4 KiB
Python
172 lines
6.4 KiB
Python
"""Wiki plugin routes — articles CRUD, categories, versioning."""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.audit import log_audit
|
|
from app.core.db import get_db
|
|
from app.deps import require_permission, require_workspace_scope
|
|
from app.plugins.builtins.wiki import services
|
|
from app.plugins.builtins.wiki.schemas import (
|
|
ArticleCreate,
|
|
ArticleUpdate,
|
|
CategoryCreate,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/v1/wiki", tags=["wiki"])
|
|
|
|
|
|
@router.get("/articles")
|
|
async def list_articles(
|
|
page: int = Query(1, ge=1),
|
|
page_size: int = Query(20, ge=1, le=100),
|
|
category_id: str | None = None,
|
|
status: str | None = None,
|
|
search: str | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
workspace_scope: dict | None = Depends(require_workspace_scope("wiki")),
|
|
):
|
|
"""List wiki articles.
|
|
|
|
Phase N4: an active workspace scope (X-Workspace-ID) restricts articles
|
|
to the category subtree (category_ids incl. children — pure AND).
|
|
"""
|
|
scoped_category_ids: set | None = None
|
|
if workspace_scope:
|
|
from app.plugins.builtins.wiki.models import WikiCategory
|
|
from app.services.workspace_scope_service import expand_folder_scope
|
|
|
|
raw_ids = workspace_scope.get("category_ids")
|
|
if isinstance(raw_ids, list) and raw_ids:
|
|
scoped_category_ids = await expand_folder_scope(db, WikiCategory, raw_ids)
|
|
|
|
result = await services.list_articles(
|
|
db, uuid.UUID(current_user["tenant_id"]),
|
|
page=page, page_size=page_size, category_id=category_id, status=status, search=search,
|
|
)
|
|
# Phase N4: filter to the scoped category subtree (post-fetch AND filter)
|
|
if scoped_category_ids is not None:
|
|
items = [
|
|
a for a in result["items"]
|
|
if a.get("category_id") and uuid.UUID(a["category_id"]) in scoped_category_ids
|
|
]
|
|
result["items"] = items
|
|
result["total"] = len(items)
|
|
return result
|
|
|
|
|
|
@router.post("/articles", status_code=status.HTTP_201_CREATED)
|
|
async def create_article(
|
|
body: ArticleCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
result = await services.create_article(db, tenant_id, user_id, body.model_dump())
|
|
if result and result.get("id"):
|
|
await log_audit(db, tenant_id, user_id, "create", "wiki_article", uuid.UUID(result["id"]), changes={"title": body.title})
|
|
return result
|
|
|
|
|
|
@router.get("/articles/{article_id}")
|
|
async def get_article(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
result = await services.get_article(db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(article_id))
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.patch("/articles/{article_id}")
|
|
async def update_article(
|
|
article_id: str,
|
|
body: ArticleUpdate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
result = await services.update_article(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]),
|
|
uuid.UUID(article_id), body.model_dump(exclude_unset=True),
|
|
)
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.delete("/articles/{article_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_article(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:delete")),
|
|
):
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
ok = await services.delete_article(db, tenant_id, uuid.UUID(article_id))
|
|
if not ok:
|
|
raise HTTPException(status_code=404, detail={"detail": "Article not found", "code": "not_found"})
|
|
await log_audit(db, tenant_id, user_id, "delete", "wiki_article", uuid.UUID(article_id))
|
|
|
|
|
|
@router.get("/articles/{article_id}/versions")
|
|
async def list_versions(
|
|
article_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
):
|
|
return {"items": await services.list_versions(db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(article_id))}
|
|
|
|
|
|
@router.post("/articles/{article_id}/versions/{version}/restore")
|
|
async def restore_version(
|
|
article_id: str,
|
|
version: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
result = await services.restore_version(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]),
|
|
uuid.UUID(article_id), version,
|
|
)
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail={"detail": "Version not found", "code": "not_found"})
|
|
return result
|
|
|
|
|
|
@router.get("/categories")
|
|
async def list_categories(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:read")),
|
|
workspace_scope: dict | None = Depends(require_workspace_scope("wiki")),
|
|
):
|
|
"""List wiki categories (Phase N4: scope reduces to the category subtree)."""
|
|
items = await services.list_categories(db, uuid.UUID(current_user["tenant_id"]))
|
|
if workspace_scope:
|
|
from app.plugins.builtins.wiki.models import WikiCategory
|
|
from app.services.workspace_scope_service import expand_folder_scope
|
|
|
|
raw_ids = workspace_scope.get("category_ids")
|
|
if isinstance(raw_ids, list) and raw_ids:
|
|
subtree = await expand_folder_scope(db, WikiCategory, raw_ids)
|
|
allowed = subtree or set()
|
|
items = [c for c in items if uuid.UUID(c["id"]) in allowed]
|
|
return {"items": items}
|
|
|
|
|
|
@router.post("/categories", status_code=status.HTTP_201_CREATED)
|
|
async def create_category(
|
|
body: CategoryCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("wiki:write")),
|
|
):
|
|
return await services.create_category(
|
|
db, uuid.UUID(current_user["tenant_id"]), uuid.UUID(current_user["user_id"]), body.model_dump(),
|
|
)
|