gateway/modules/interfaces/interfaceComponentModel.py

171 lines
5.6 KiB
Python

"""
Service Management model classes for the service management system.
Updated to match the Entity Relation Diagram structure.
"""
from pydantic import BaseModel, Field
from typing import List, Dict, Any, Optional, Union
from datetime import datetime
import uuid
# Import for label registration
from modules.shared.attributeUtils import register_model_labels, ModelMixin
from modules.shared.timezoneUtils import get_utc_timestamp
# CORE MODELS
class FileItem(BaseModel, ModelMixin):
"""Data model for a file item"""
id: str = Field(
default_factory=lambda: str(uuid.uuid4()),
description="Primary key",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
mandateId: str = Field(
description="ID of the mandate this file belongs to",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
fileName: str = Field(
description="Name of the file",
frontend_type="text",
frontend_readonly=False,
frontend_required=True
)
mimeType: str = Field(
description="MIME type of the file",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
fileHash: str = Field(
description="Hash of the file",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
fileSize: int = Field(
description="Size of the file in bytes",
frontend_type="integer",
frontend_readonly=True,
frontend_required=False
)
creationDate: float = Field(
default_factory=get_utc_timestamp,
description="Date when the file was created (UTC timestamp in seconds)",
frontend_type="timestamp",
frontend_readonly=True,
frontend_required=False
)
def to_dict(self) -> Dict[str, Any]:
"""Convert model to dictionary"""
return super().to_dict()
# Register labels for FileItem
register_model_labels(
"FileItem",
{"en": "File Item", "fr": "Élément de fichier"},
{
"id": {"en": "ID", "fr": "ID"},
"mandateId": {"en": "Mandate ID", "fr": "ID du mandat"},
"fileName": {"en": "fileName", "fr": "Nom de fichier"},
"mimeType": {"en": "MIME Type", "fr": "Type MIME"},
"fileHash": {"en": "File Hash", "fr": "Hash du fichier"},
"fileSize": {"en": "File Size", "fr": "Taille du fichier"},
"creationDate": {"en": "Creation Date", "fr": "Date de création"}
}
)
class FilePreview(BaseModel, ModelMixin):
"""Data model for file preview"""
content: Union[str, bytes] = Field(description="File content (text or binary)")
mimeType: str = Field(description="MIME type of the file")
fileName: str = Field(description="Original fileName")
isText: bool = Field(description="Whether the content is text (True) or binary (False)")
encoding: Optional[str] = Field(None, description="Text encoding if content is text")
size: int = Field(description="Size of the content in bytes")
def to_dict(self) -> Dict[str, Any]:
"""Convert model to dictionary with proper content handling"""
data = super().to_dict()
# Convert bytes to base64 string if content is binary
if isinstance(data.get("content"), bytes):
import base64
data["content"] = base64.b64encode(data["content"]).decode('utf-8')
return data
# Register labels for FilePreview
register_model_labels(
"FilePreview",
{"en": "File Preview", "fr": "Aperçu du fichier"},
{
"content": {"en": "Content", "fr": "Contenu"},
"mimeType": {"en": "MIME Type", "fr": "Type MIME"},
"fileName": {"en": "fileName", "fr": "Nom de fichier"},
"isText": {"en": "Is Text", "fr": "Est du texte"},
"encoding": {"en": "Encoding", "fr": "Encodage"},
"size": {"en": "Size", "fr": "Taille"}
}
)
class FileData(BaseModel, ModelMixin):
"""Data model for file data"""
id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Primary key")
data: str = Field(description="File data content")
base64Encoded: bool = Field(description="Whether the data is base64 encoded")
# Register labels for FileData
register_model_labels(
"FileData",
{"en": "File Data", "fr": "Données de fichier"},
{
"id": {"en": "ID", "fr": "ID"},
"data": {"en": "Data", "fr": "Données"},
"base64Encoded": {"en": "Base64 Encoded", "fr": "Encodé en Base64"}
}
)
class Prompt(BaseModel, ModelMixin):
"""Data model for a prompt"""
id: str = Field(
default_factory=lambda: str(uuid.uuid4()),
description="Primary key",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
mandateId: str = Field(
description="ID of the mandate this prompt belongs to",
frontend_type="text",
frontend_readonly=True,
frontend_required=False
)
content: str = Field(
description="Content of the prompt",
frontend_type="textarea",
frontend_readonly=False,
frontend_required=True
)
name: str = Field(
description="Name of the prompt",
frontend_type="text",
frontend_readonly=False,
frontend_required=True
)
# Register labels for Prompt
register_model_labels(
"Prompt",
{"en": "Prompt", "fr": "Invite"},
{
"id": {"en": "ID", "fr": "ID"},
"mandateId": {"en": "Mandate ID", "fr": "ID du mandat"},
"content": {"en": "Content", "fr": "Contenu"},
"name": {"en": "Name", "fr": "Nom"}
}
)