151 lines
No EOL
7.3 KiB
Python
151 lines
No EOL
7.3 KiB
Python
"""
|
|
Data models for the gateway system.
|
|
"""
|
|
from pydantic import BaseModel, Field
|
|
from typing import List, Dict, Any, Optional
|
|
from datetime import datetime
|
|
import uuid
|
|
|
|
# Get all attributes of the model
|
|
def getModelAttributes(modelClass):
|
|
return [attr for attr in dir(modelClass)
|
|
if not callable(getattr(modelClass, attr))
|
|
and not attr.startswith('_')
|
|
and attr not in ('metadata', 'query', 'query_class', 'label', 'field_labels')]
|
|
|
|
class Label(BaseModel):
|
|
"""Label for an attribute or a class with support for multiple languages"""
|
|
default: str = Field(..., description="Default label text")
|
|
translations: Dict[str, str] = Field(default_factory=dict, description="Translations for different languages")
|
|
|
|
class Config:
|
|
title = "Label"
|
|
description = "A label with support for multiple languages"
|
|
schema_extra = {
|
|
"example": {
|
|
"default": "User",
|
|
"translations": {
|
|
"en": "User",
|
|
"fr": "Utilisateur"
|
|
}
|
|
}
|
|
}
|
|
|
|
def getLabel(self, language: str = None):
|
|
"""Returns the label in the specified language, or the default value if not available"""
|
|
if language and language in self.translations:
|
|
return self.translations[language]
|
|
return self.default
|
|
|
|
|
|
class Mandate(BaseModel):
|
|
"""Data model for a mandate"""
|
|
id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Unique ID of the mandate")
|
|
name: str = Field(description="Name of the mandate")
|
|
language: str = Field(description="Default language of the mandate")
|
|
|
|
label: Label = Field(
|
|
default=Label(default="Mandate", translations={"en": "Mandate", "fr": "Mandat"}),
|
|
description="Label for the class"
|
|
)
|
|
|
|
# Labels for attributes
|
|
fieldLabels: Dict[str, Label] = {
|
|
"id": Label(default="ID", translations={}),
|
|
"name": Label(default="Name of the mandate", translations={"en": "Mandate name", "fr": "Nom du mandat"}),
|
|
"language": Label(default="Language", translations={"en": "Language", "fr": "Langue"})
|
|
}
|
|
|
|
class UserConnection(BaseModel):
|
|
"""Data model for a user's connection to an external service"""
|
|
id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Unique ID of the connection")
|
|
authority: str = Field(description="Authentication authority (microsoft, google, etc.)")
|
|
externalId: str = Field(description="User ID in the external system")
|
|
externalUsername: str = Field(description="Username in the external system")
|
|
externalEmail: Optional[str] = Field(None, description="Email in the external system")
|
|
connectedAt: datetime = Field(default_factory=datetime.now, description="When the connection was established")
|
|
|
|
label: Label = Field(
|
|
default=Label(default="User Connection", translations={"en": "User Connection", "fr": "Connexion utilisateur"}),
|
|
description="Label for the class"
|
|
)
|
|
|
|
# Labels for attributes
|
|
fieldLabels: Dict[str, Label] = {
|
|
"id": Label(default="ID", translations={}),
|
|
"authority": Label(default="Authority", translations={"en": "Authority", "fr": "Autorité"}),
|
|
"externalId": Label(default="External ID", translations={"en": "External ID", "fr": "ID externe"}),
|
|
"externalUsername": Label(default="External Username", translations={"en": "External Username", "fr": "Nom d'utilisateur externe"}),
|
|
"externalEmail": Label(default="External Email", translations={"en": "External Email", "fr": "Email externe"}),
|
|
"connectedAt": Label(default="Connected At", translations={"en": "Connected At", "fr": "Connecté le"})
|
|
}
|
|
|
|
class User(BaseModel):
|
|
"""Data model for a user"""
|
|
id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Unique ID of the user")
|
|
username: str = Field(description="Username for login")
|
|
email: Optional[str] = Field(None, description="Email address of the user")
|
|
fullName: Optional[str] = Field(None, description="Full name of the user")
|
|
language: str = Field(description="Preferred language of the user")
|
|
disabled: Optional[bool] = Field(False, description="Indicates whether the user is disabled")
|
|
privilege: str = Field(description="Permission level") #sysadmin,admin,user
|
|
authenticationAuthority: str = Field(default="local", description="Primary authentication authority (local, microsoft)")
|
|
mandateId: str = Field(description="ID of the mandate this user belongs to")
|
|
connections: List[UserConnection] = Field(default_factory=list, description="List of external service connections")
|
|
|
|
label: Label = Field(
|
|
default=Label(default="User", translations={"en": "User", "fr": "Utilisateur"}),
|
|
description="Label for the class"
|
|
)
|
|
|
|
# Labels for attributes
|
|
fieldLabels: Dict[str, Label] = {
|
|
"id": Label(default="ID", translations={}),
|
|
"mandateId": Label(default="Mandate ID", translations={"en": "Mandate ID", "fr": "ID de mandat"}),
|
|
"username": Label(default="Username", translations={"en": "Username", "fr": "Nom d'utilisateur"}),
|
|
"email": Label(default="Email", translations={"en": "Email", "fr": "E-mail"}),
|
|
"fullName": Label(default="Full name", translations={"en": "Full name", "fr": "Nom complet"}),
|
|
"language": Label(default="Language", translations={"en": "Language", "fr": "Langue"}),
|
|
"disabled": Label(default="Disabled", translations={"en": "Disabled", "fr": "Désactivé"}),
|
|
"privilege": Label(default="Permission level", translations={"en": "Access level", "fr": "Niveau d'accès"}),
|
|
"authenticationAuthority": Label(default="Authentication Authority", translations={"en": "Authentication Authority", "fr": "Autorité d'authentification"}),
|
|
"connections": Label(default="External Connections", translations={"en": "External Connections", "fr": "Connexions externes"})
|
|
}
|
|
|
|
|
|
class UserInDB(User):
|
|
"""Extended user class with password hash"""
|
|
hashedPassword: str = Field(description="Hash of the user password")
|
|
|
|
label: Label = Field(
|
|
default=Label(default="User Access", translations={"en": "User Access", "fr": "Accès de l'utilisateur"}),
|
|
description="Label for the class"
|
|
)
|
|
|
|
# Additional label for the password field
|
|
fieldLabels: Dict[str, Label] = {
|
|
"hashedPassword": Label(default="Password hash", translations={"en": "Password hash", "fr": "Hachage de mot de passe"})
|
|
}
|
|
|
|
|
|
class Token(BaseModel):
|
|
"""Data model for an authentication token"""
|
|
accessToken: str = Field(description="The issued access token")
|
|
tokenType: str = Field(description="Type of token (usually 'bearer')")
|
|
label: Label = Field(
|
|
default=Label(default="Token", translations={"en": "Token", "fr": "Jeton"}),
|
|
description="Label for the class"
|
|
)
|
|
|
|
# Labels for attributes
|
|
fieldLabels: Dict[str, Label] = {
|
|
"accessToken": Label(default="Access token", translations={"en": "Access token", "fr": "Jeton d'accès"}),
|
|
"tokenType": Label(default="Token type", translations={"en": "Token type", "fr": "Type de jeton"})
|
|
}
|
|
|
|
|
|
class TokenData(BaseModel):
|
|
"""Data for token decoding and validation"""
|
|
username: Optional[str] = None
|
|
mandateId: Optional[str] = None
|
|
exp: Optional[datetime] = None |