65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
import uuid
|
|
|
|
from datetime import datetime
|
|
|
|
from pydantic import (
|
|
BaseModel,
|
|
ConfigDict,
|
|
Field,
|
|
FieldSerializationInfo,
|
|
field_serializer,
|
|
field_validator,
|
|
)
|
|
|
|
from . import to_camel
|
|
from .role import RoleItem
|
|
|
|
|
|
class UserIn(BaseModel):
|
|
name: str = Field(..., min_length=1)
|
|
password: str
|
|
locked_out: bool
|
|
roles: list[RoleItem]
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|
|
|
|
|
|
class User(UserIn):
|
|
id_: uuid.UUID
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|
|
|
|
|
|
class UserList(BaseModel):
|
|
id_: uuid.UUID
|
|
name: str
|
|
locked_out: bool
|
|
roles: list[str]
|
|
last_device: str
|
|
last_date: datetime | None = None
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|
|
|
|
@field_validator("last_date", mode="before")
|
|
@classmethod
|
|
def parse_last_edit_date(cls, value: datetime | str | None) -> datetime | None:
|
|
if value is None:
|
|
return value
|
|
if isinstance(value, datetime):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y %H:%M")
|
|
|
|
@field_serializer("last_date")
|
|
def serialize_last_edit_date(self, value: datetime, info: FieldSerializationInfo) -> str:
|
|
return None if value is None else value.strftime("%d-%b-%Y %H:%M")
|
|
|
|
|
|
class UserToken(BaseModel):
|
|
id_: uuid.UUID
|
|
name: str
|
|
locked_out: bool
|
|
password: str
|
|
permissions: list[str]
|
|
|
|
|
|
class UserBlank(UserIn):
|
|
name: str
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|