spark / cbh /api /account /models.py
brestok's picture
Add support for anonymous call purchases and account registration
63e1f68
"""
Account models.
"""
from datetime import datetime
from passlib.context import CryptContext
from pydantic import Field, field_validator
from cbh.api.account.dto import AccountStatus, AccountType, CoachOpportunity
from cbh.core.database import MongoBaseModel, MongoBaseShortenModel
class AccountModel(MongoBaseModel):
"""
Account model class.
"""
name: str | None = None
email: str
phone: str | None = None
password: str | None = Field(exclude=True, default=None)
status: AccountStatus = AccountStatus.ACTIVE
accountType: AccountType = Field(default=AccountType.USER)
opportunity: CoachOpportunity | None = None
datetimeInserted: datetime = Field(default_factory=datetime.now)
datetimeUpdated: datetime = Field(default_factory=datetime.now)
@field_validator("password", mode="before", check_fields=False)
@classmethod
def set_password_hash(cls, v: str | None) -> str | None:
"""
Set the password hash.
Args:
v (str): The password to hash.
Returns:
str: The hashed password.
"""
if isinstance(v, str) and not v.startswith("$2b$"):
return CryptContext(schemes=["bcrypt"], deprecated="auto").hash(v)
return v
class Config: # pylint: disable=R0903
"""
Config for the AccountModel class.
"""
arbitrary_types_allowed = True
populate_by_name = True
json_encoders = {datetime: lambda dt: dt.isoformat()}
class AccountShorten(MongoBaseShortenModel):
"""
Account shorten model.
"""
name: str | None = None
email: str
status: AccountStatus
accountType: AccountType