123 lines
No EOL
4.5 KiB
Python
123 lines
No EOL
4.5 KiB
Python
from pydantic import BaseModel, Field
|
|
from typing import List, Dict, Any, Optional
|
|
|
|
# Definiere das Modell für Attributdefinitionen
|
|
class AttributeDefinition(BaseModel):
|
|
name: str
|
|
label: str
|
|
type: str
|
|
required: bool = False
|
|
placeholder: Optional[str] = None
|
|
default_value: Optional[Any] = None
|
|
options: Optional[List[Dict[str, Any]]] = None
|
|
editable: bool = True
|
|
visible: bool = True
|
|
order: int = 0
|
|
validation: Optional[Dict[str, Any]] = None
|
|
help_text: Optional[str] = None
|
|
|
|
# Hilfsklassen für Typzuordnung
|
|
type_mappings = {
|
|
"int": "number",
|
|
"str": "string",
|
|
"float": "number",
|
|
"bool": "boolean",
|
|
"List[int]": "array",
|
|
"List[str]": "array",
|
|
"Dict[str, Any]": "object",
|
|
"Optional[str]": "string",
|
|
"Optional[int]": "number",
|
|
"Optional[Dict[str, Any]]": "object"
|
|
}
|
|
|
|
# Spezielle Feldtypen basierend auf Namenskonventionen
|
|
special_field_types = {
|
|
"content": "textarea",
|
|
"description": "textarea",
|
|
"instructions": "textarea",
|
|
"password": "password",
|
|
"email": "email",
|
|
"workspace_id": "select",
|
|
"agent_id": "select",
|
|
"type": "select"
|
|
}
|
|
|
|
# Funktion zum Konvertieren eines Pydantic-Modells in Attributdefinitionen
|
|
def get_model_attributes(model_class, user_language="de"):
|
|
"""
|
|
Konvertiert ein Pydantic-Modell in eine Liste von AttributeDefinition-Objekten
|
|
"""
|
|
attributes = []
|
|
|
|
# Gehe alle Felder im Modell durch
|
|
for i, (field_name, field) in enumerate(model_class.__fields__.items()):
|
|
# Überspringe interne Felder
|
|
if field_name.startswith('_') or field_name in ["label", "field_labels"]:
|
|
continue
|
|
|
|
# Bestimme den Feldtyp
|
|
field_type = type_mappings.get(str(field.type_), "string")
|
|
|
|
# Prüfe auf spezielle Feldtypen
|
|
if field_name in special_field_types:
|
|
field_type = special_field_types[field_name]
|
|
|
|
# Hole das Label (falls vorhanden)
|
|
field_label = field_name.replace('_', ' ').capitalize()
|
|
if hasattr(model_class, 'field_labels') and field_name in model_class.field_labels:
|
|
label_obj = model_class.field_labels[field_name]
|
|
field_label = label_obj.get_label(user_language)
|
|
|
|
# Standardwerte und Required-Status ermitteln
|
|
required = field.required
|
|
default_value = field.default if not field.required else None
|
|
|
|
# Hinweise auf Validierungsregeln
|
|
validation = None
|
|
if field.validators:
|
|
validation = {"has_validators": True}
|
|
|
|
# Platzhaltertext
|
|
placeholder = f"Bitte {field_label} eingeben"
|
|
|
|
# Spezielle Optionen für Select-Felder
|
|
options = None
|
|
if field_type == "select":
|
|
if field_name == "type" and model_class.__name__ == "Agent":
|
|
options = [
|
|
{"value": "Analyse", "label": "Analyse"},
|
|
{"value": "Transformation", "label": "Transformation"},
|
|
{"value": "Generierung", "label": "Generierung"},
|
|
{"value": "Klassifikation", "label": "Klassifikation"},
|
|
{"value": "Benutzerdefiniert", "label": "Benutzerdefiniert"}
|
|
]
|
|
|
|
# Extrahiere die Beschreibung aus dem Field-Objekt
|
|
description = None
|
|
# Versuche, die description aus verschiedenen möglichen Quellen zu holen
|
|
if hasattr(field, 'field_info') and hasattr(field.field_info, 'description'):
|
|
description = field.field_info.description
|
|
elif hasattr(field, 'description'):
|
|
description = field.description
|
|
elif hasattr(field, 'schema') and hasattr(field.schema, 'description'):
|
|
description = field.schema.description
|
|
|
|
# Attributdefinition erstellen
|
|
attr_def = AttributeDefinition(
|
|
name=field_name,
|
|
label=field_label,
|
|
type=field_type,
|
|
required=required,
|
|
placeholder=placeholder,
|
|
default_value=default_value,
|
|
options=options,
|
|
editable=field_name not in ["id", "mandate_id", "user_id", "created_at", "upload_date"],
|
|
visible=field_name not in ["hashed_password", "mandate_id", "user_id"],
|
|
order=i,
|
|
validation=validation,
|
|
help_text=description or "" # Setze leeren String als Standardwert, wenn keine Beschreibung gefunden wurde
|
|
)
|
|
|
|
attributes.append(attr_def)
|
|
|
|
return attributes |