
- Add ClassVar type annotation to DB_DIR to fix the Pydantic validation error - Update Config to model_config for Pydantic v2 compatibility - Ensure the Settings class properly handles the database directory path
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
from pathlib import Path
|
|
from typing import ClassVar, List
|
|
|
|
from pydantic import AnyHttpUrl, EmailStr, validator
|
|
from pydantic_settings import BaseSettings
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
PROJECT_NAME: str = "Small Business Inventory Management System"
|
|
DESCRIPTION: str = "API for managing inventory for small businesses"
|
|
VERSION: str = "0.1.0"
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
# SECURITY
|
|
SECRET_KEY: str = "CHANGE_ME_IN_PRODUCTION"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7 # 7 days
|
|
|
|
# Database setup
|
|
DB_DIR: ClassVar[Path] = Path("/app") / "storage" / "db"
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
# CORS
|
|
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
|
|
|
@validator("BACKEND_CORS_ORIGINS", pre=True)
|
|
def assemble_cors_origins(cls, v: str | List[str]) -> List[str] | str:
|
|
if isinstance(v, str) and not v.startswith("["):
|
|
return [i.strip() for i in v.split(",")]
|
|
elif isinstance(v, (list, str)):
|
|
return v
|
|
raise ValueError(v)
|
|
|
|
# Email
|
|
SMTP_TLS: bool = True
|
|
SMTP_PORT: int | None = None
|
|
SMTP_HOST: str | None = None
|
|
SMTP_USER: str | None = None
|
|
SMTP_PASSWORD: str | None = None
|
|
EMAILS_FROM_EMAIL: EmailStr | None = None
|
|
EMAILS_FROM_NAME: str | None = None
|
|
|
|
@validator("EMAILS_FROM_NAME")
|
|
def get_project_name(cls, v: str | None, values: dict) -> str:
|
|
if not v:
|
|
return values["PROJECT_NAME"]
|
|
return v
|
|
|
|
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
|
|
EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build"
|
|
EMAILS_ENABLED: bool = False
|
|
|
|
@validator("EMAILS_ENABLED", pre=True)
|
|
def get_emails_enabled(cls, v: bool, values: dict) -> bool:
|
|
return bool(
|
|
values.get("SMTP_HOST")
|
|
and values.get("SMTP_PORT")
|
|
and values.get("EMAILS_FROM_EMAIL")
|
|
)
|
|
|
|
model_config = {
|
|
"case_sensitive": True,
|
|
"env_file": ".env"
|
|
}
|
|
|
|
|
|
settings = Settings() |