74 lines
No EOL
2.5 KiB
Python
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 get_current_active_user, get_user_context
|
|
|
|
# Import the attribute definition and helper functions
|
|
from modules.def_attributes import AttributeDefinition, get_model_attributes
|
|
|
|
# Import the model modules (without specific classes)
|
|
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 - admin
|
|
"file": lucydom_model.FileItem,
|
|
"prompt": lucydom_model.Prompt,
|
|
|
|
# LucyDOM model classes - chat
|
|
"document_content": lucydom_model.DocumentContent,
|
|
"document": lucydom_model.Document,
|
|
"data_stats": lucydom_model.DataStats,
|
|
"user_input_request": lucydom_model.UserInputRequest,
|
|
"workflow": lucydom_model.Workflow,
|
|
"workflow_message": lucydom_model.WorkflowMessage,
|
|
"workflow_log": lucydom_model.WorkflowLog,
|
|
}
|
|
|
|
|
|
# Create a router for the attribute endpoints
|
|
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="Type of entity (e.g. prompt)"),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""
|
|
Retrieves the attribute definitions for a specific entity.
|
|
This can be used for dynamic form generation.
|
|
"""
|
|
# Authentication and user context
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# Determine preferred language of the user
|
|
user_language = current_user.get("language", "de")
|
|
|
|
# Check if entity type is known
|
|
if entity_type not in model_classes:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Entity type '{entity_type}' not found."
|
|
)
|
|
|
|
# Get model class and derive attributes from it
|
|
model_class = model_classes[entity_type]
|
|
attributes = get_model_attributes(model_class, user_language)
|
|
|
|
# Return only editable and visible attributes
|
|
visible_attributes = [attr for attr in attributes if attr.visible]
|
|
|
|
@router.options("/{entity_type}")
|
|
async def options_entity_attributes(
|
|
entity_type: str = Path(..., description="Type of entity (e.g. prompt)")
|
|
):
|
|
return Response(status_code=200) |