gateway/routes/attributes.py
2025-04-21 20:04:22 +02:00

74 lines
2.6 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Path, Response
from typing import List, Dict, Any
from fastapi import status
from modules.auth import get_current_active_user, get_user_context
# Importiere die Attributdefinition und Hilfsfunktionen
from gateway.modules.def_attributes import AttributeDefinition, get_model_attributes
# Importiere die Modellmodule (ohne spezifische Klassen)
import modules.gateway_model as gateway_model
import modules.lucydom_model as lucydom_model
model_classes = {
# Gateway model classes
"mandate": gateway_model.Mandate,
"user": gateway_model.User,
# LucyDOM model classes
"file": lucydom_model.FileItem,
"prompt": lucydom_model.Prompt,
"document_source": lucydom_model.DocumentSource,
"document_content": lucydom_model.DocumentContent,
"document": lucydom_model.Document,
"data_stats": lucydom_model.DataStats,
"message": lucydom_model.Message,
"workflow": lucydom_model.Workflow,
"workflow_create_request": lucydom_model.WorkflowCreateRequest,
"user_input_request": lucydom_model.UserInputRequest
}
# Erstelle einen Router für die Attribute-Endpunkte
router = APIRouter(
prefix="/api/attributes",
tags=["Attributes"],
responses={404: {"description": "Not found"}}
)
@router.get("/{entity_type}", response_model=List[AttributeDefinition])
async def get_entity_attributes(
entity_type: str = Path(..., description="Typ der Entität (z.B. prompt)"),
current_user: Dict[str, Any] = Depends(get_current_active_user)
):
"""
Ruft die Attributdefinitionen für eine bestimmte Entität ab.
Dies kann für die dynamische Generierung von Formularen verwendet werden.
"""
# Authentifizierung und Benutzerkontext
mandate_id, user_id = await get_user_context(current_user)
# Bevorzugte Sprache des Benutzers ermitteln
user_language = current_user.get("language", "de")
# Prüfen, ob Entitätstyp bekannt ist
if entity_type not in model_classes:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Entitätstyp '{entity_type}' nicht gefunden."
)
# Model-Klasse abrufen und Attribute daraus ableiten
model_class = model_classes[entity_type]
attributes = get_model_attributes(model_class, user_language)
# Nur bearbeitbare und sichtbare Attribute zurückgeben
visible_attributes = [attr for attr in attributes if attr.visible]
@router.options("/{entity_type}")
async def options_entity_attributes(
entity_type: str = Path(..., description="Typ der Entität (z.B. prompt)")
):
return Response(status_code=200)