|
|
@ -1,7 +1,14 @@ |
|
|
|
import secrets |
|
|
|
from typing import Any |
|
|
|
|
|
|
|
from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator |
|
|
|
from pydantic import ( |
|
|
|
AnyHttpUrl, |
|
|
|
HttpUrl, |
|
|
|
PostgresDsn, |
|
|
|
ValidationInfo, |
|
|
|
field_validator, |
|
|
|
) |
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict |
|
|
|
|
|
|
|
|
|
|
|
class Settings(BaseSettings): |
|
|
@ -16,7 +23,8 @@ class Settings(BaseSettings): |
|
|
|
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]' |
|
|
|
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] = [] |
|
|
|
|
|
|
|
@validator("BACKEND_CORS_ORIGINS", pre=True) |
|
|
|
@field_validator("BACKEND_CORS_ORIGINS", mode="before") |
|
|
|
@classmethod |
|
|
|
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(",")] |
|
|
@ -27,7 +35,8 @@ class Settings(BaseSettings): |
|
|
|
PROJECT_NAME: str |
|
|
|
SENTRY_DSN: HttpUrl | None = None |
|
|
|
|
|
|
|
@validator("SENTRY_DSN", pre=True) |
|
|
|
@field_validator("SENTRY_DSN", mode="before") |
|
|
|
@classmethod |
|
|
|
def sentry_dsn_can_be_blank(cls, v: str) -> str | None: |
|
|
|
if len(v) == 0: |
|
|
|
return None |
|
|
@ -39,16 +48,16 @@ class Settings(BaseSettings): |
|
|
|
POSTGRES_DB: str |
|
|
|
SQLALCHEMY_DATABASE_URI: PostgresDsn | None = None |
|
|
|
|
|
|
|
@validator("SQLALCHEMY_DATABASE_URI", pre=True) |
|
|
|
def assemble_db_connection(cls, v: str | None, values: dict[str, Any]) -> Any: |
|
|
|
@field_validator("SQLALCHEMY_DATABASE_URI", mode="before") |
|
|
|
def assemble_db_connection(cls, v: str | None, info: ValidationInfo) -> Any: |
|
|
|
if isinstance(v, str): |
|
|
|
return v |
|
|
|
return PostgresDsn.build( |
|
|
|
scheme="postgresql+psycopg", |
|
|
|
user=values.get("POSTGRES_USER"), |
|
|
|
password=values.get("POSTGRES_PASSWORD"), |
|
|
|
host=values.get("POSTGRES_SERVER"), |
|
|
|
path=f"/{values.get('POSTGRES_DB') or ''}", |
|
|
|
username=info.data.get("POSTGRES_USER"), |
|
|
|
password=info.data.get("POSTGRES_PASSWORD"), |
|
|
|
host=info.data.get("POSTGRES_SERVER"), |
|
|
|
path=f"{info.data.get('POSTGRES_DB') or ''}", |
|
|
|
) |
|
|
|
|
|
|
|
SMTP_TLS: bool = True |
|
|
@ -56,34 +65,32 @@ class Settings(BaseSettings): |
|
|
|
SMTP_HOST: str | None = None |
|
|
|
SMTP_USER: str | None = None |
|
|
|
SMTP_PASSWORD: str | None = None |
|
|
|
EMAILS_FROM_EMAIL: EmailStr | None = None |
|
|
|
EMAILS_FROM_EMAIL: str | None = None #TODO: update type to EmailStr when sqlmodel supports it |
|
|
|
EMAILS_FROM_NAME: str | None = None |
|
|
|
|
|
|
|
@validator("EMAILS_FROM_NAME") |
|
|
|
def get_project_name(cls, v: str | None, values: dict[str, Any]) -> str: |
|
|
|
@field_validator("EMAILS_FROM_NAME") |
|
|
|
def get_project_name(cls, v: str | None, info: ValidationInfo) -> str: |
|
|
|
if not v: |
|
|
|
return values["PROJECT_NAME"] |
|
|
|
return info.data["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[str, Any]) -> bool: |
|
|
|
@field_validator("EMAILS_ENABLED", mode="before") |
|
|
|
def get_emails_enabled(cls, v: bool, info: ValidationInfo) -> bool: |
|
|
|
return bool( |
|
|
|
values.get("SMTP_HOST") |
|
|
|
and values.get("SMTP_PORT") |
|
|
|
and values.get("EMAILS_FROM_EMAIL") |
|
|
|
info.data.get("SMTP_HOST") |
|
|
|
and info.data.get("SMTP_PORT") |
|
|
|
and info.data.get("EMAILS_FROM_EMAIL") |
|
|
|
) |
|
|
|
|
|
|
|
EMAIL_TEST_USER: EmailStr = "test@example.com" # type: ignore |
|
|
|
FIRST_SUPERUSER: EmailStr |
|
|
|
EMAIL_TEST_USER: str = "test@example.com" #TODO: update type to EmailStr when sqlmodel supports it |
|
|
|
FIRST_SUPERUSER: str #TODO: update type to EmailStr when sqlmodel supports it |
|
|
|
FIRST_SUPERUSER_PASSWORD: str |
|
|
|
USERS_OPEN_REGISTRATION: bool = False |
|
|
|
|
|
|
|
class Config: |
|
|
|
case_sensitive = True |
|
|
|
model_config = SettingsConfigDict(case_sensitive=True) |
|
|
|
|
|
|
|
|
|
|
|
settings = Settings() |
|
|
|