Files
Eurovision-25-backend/src/models/user.py
T
2025-05-16 12:18:10 +03:00

58 lines
2.0 KiB
Python

from pydantic import BaseModel, Field
from datetime import datetime
class UserBase(BaseModel):
username: str = Field(description="Username of the user", examples=["Kalle"])
class User(UserBase):
id: int = Field(description="ID of the user", examples=[1])
team_id: int = Field(description="ID of the team", examples=[1])
is_active: bool = Field(
default=True, description="Is the user active?", examples=[True]
)
avatar_id: int = Field(default=0, description="ID of the avatar", examples=[0])
is_admin: bool = Field(
default=False, description="Is the user an admin?", examples=[False]
)
last_login: datetime = Field(
description="Last login timestamp",
examples=["2025-01-01T00:00:00.000Z"],
)
created_at: datetime = Field(
description="Creation timestamp", examples=["2025-01-01T00:00:00.000Z"]
)
updated_at: datetime = Field(
description="Last update timestamp", examples=["2025-01-01T00:00:00.000Z"]
)
@property
def has_logged_in(self) -> bool:
"""Returns True if the user has ever logged in, False if last_login is the epoch date."""
epoch = datetime(1970, 1, 1, 0, 0, 0)
return self.last_login and self.last_login > epoch
class CreateUser(UserBase):
password: str = Field(description="Password of the user", examples=["sn1rbul4"])
team_id: int = Field(description="ID of the team", examples=[1])
is_admin: bool = Field(
default=False, description="Is the user an admin?", examples=[False]
)
class UserLogin(UserBase):
password: str = Field(description="Password of the user", examples=["sn1rbul4"])
class UpdateUser(BaseModel):
id: int = Field(description="ID of the user", examples=[1])
avatar_id: int | None = Field(None, description="ID of the avatar", examples=[1])
email: str | None = Field(
None, description="Email of the user", examples=["[email protected]"]
)
password: str | None = Field(
None, description="Password of the user", examples=["p1p4l1"]
)