gateway/routes/routeAttributes.py
2025-04-26 02:13:22 +02:00

74 lines
No EOL
2.5 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Path, Response
from typing import List, Dict, Any
from fastapi import status
from modules.auth import getCurrentActiveUser, getUserContext
# Import the attribute definition and helper functions
from modules.defAttributes import AttributeDefinition, getModelAttributes
# Import the model modules (without specific classes)
import modules.gatewayModel as gatewayModel
import modules.lucydomModel as lucydomModel
modelClasses = {
# Gateway model classes
"mandate": gatewayModel.Mandate,
"user": gatewayModel.User,
# LucyDOM model classes - admin
"file": lucydomModel.FileItem,
"prompt": lucydomModel.Prompt,
# LucyDOM model classes - chat
"documentContent": lucydomModel.DocumentContent,
"document": lucydomModel.Document,
"dataStats": lucydomModel.DataStats,
"userInputRequest": lucydomModel.UserInputRequest,
"workflow": lucydomModel.Workflow,
"workflowMessage": lucydomModel.WorkflowMessage,
"workflowLog": lucydomModel.WorkflowLog,
}
# Create a router for the attribute endpoints
router = APIRouter(
prefix="/api/attributes",
tags=["Attributes"],
responses={404: {"description": "Not found"}}
)
@router.get("/{entityType}", response_model=List[AttributeDefinition])
async def getEntityAttributes(
entityType: str = Path(..., description="Type of entity (e.g. prompt)"),
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
):
"""
Retrieves the attribute definitions for a specific entity.
This can be used for dynamic form generation.
"""
# Authentication and user context
mandateId, userId = await getUserContext(currentUser)
# Determine preferred language of the user
userLanguage = currentUser.get("language", "de")
# Check if entity type is known
if entityType not in modelClasses:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Entity type '{entityType}' not found."
)
# Get model class and derive attributes from it
modelClass = modelClasses[entityType]
attributes = getModelAttributes(modelClass, userLanguage)
# Return only visible attributes
return [attr for attr in attributes if attr.visible]
@router.options("/{entityType}")
async def optionsEntityAttributes(
entityType: str = Path(..., description="Type of entity (e.g. prompt)")
):
"""Handle OPTIONS request for CORS preflight"""
return Response(status_code=200)