2020-04-16 23:56:10 -06:00
|
|
|
import secrets
|
2024-02-25 19:39:33 +01:00
|
|
|
from typing import Any
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
from pydantic import (
|
|
|
|
AnyHttpUrl,
|
|
|
|
HttpUrl,
|
|
|
|
PostgresDsn,
|
|
|
|
ValidationInfo,
|
|
|
|
field_validator,
|
|
|
|
)
|
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2020-04-16 23:56:10 -06:00
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
SECRET_KEY: str = secrets.token_urlsafe(32)
|
:recycle: Refactor backend, settings, DB sessions, types, configs, plugins (#158)
* :recycle: Refactor backend, update DB session handling
* :sparkles: Add mypy config and plugins
* :heavy_plus_sign: Use Python-jose instead of PyJWT
as it has some extra functionalities and features
* :sparkles: Add/update scripts for test, lint, format
* :wrench: Update lint and format configs
* :art: Update import format, comments, and types
* :art: Add types to config
* :sparkles: Add types for all the code, and small fixes
* :art: Use global imports to simplify exploring with Jupyter
* :recycle: Import schemas and models, instead of each class
* :truck: Rename db_session to db for simplicity
* :pushpin: Update dependencies installation for testing
2020-04-20 19:03:13 +02:00
|
|
|
# 60 minutes * 24 hours * 8 days = 8 days
|
|
|
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
2020-04-16 23:56:10 -06:00
|
|
|
SERVER_HOST: AnyHttpUrl
|
2024-02-29 02:03:23 +01:00
|
|
|
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] | str = []
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
@field_validator("BACKEND_CORS_ORIGINS", mode="before")
|
|
|
|
@classmethod
|
2024-02-25 19:39:33 +01:00
|
|
|
def assemble_cors_origins(cls, v: str | list[str]) -> list[str] | str:
|
2020-04-16 23:56:10 -06:00
|
|
|
if isinstance(v, str) and not v.startswith("["):
|
|
|
|
return [i.strip() for i in v.split(",")]
|
2024-02-25 19:39:33 +01:00
|
|
|
elif isinstance(v, list | str):
|
:recycle: Refactor backend, settings, DB sessions, types, configs, plugins (#158)
* :recycle: Refactor backend, update DB session handling
* :sparkles: Add mypy config and plugins
* :heavy_plus_sign: Use Python-jose instead of PyJWT
as it has some extra functionalities and features
* :sparkles: Add/update scripts for test, lint, format
* :wrench: Update lint and format configs
* :art: Update import format, comments, and types
* :art: Add types to config
* :sparkles: Add types for all the code, and small fixes
* :art: Use global imports to simplify exploring with Jupyter
* :recycle: Import schemas and models, instead of each class
* :truck: Rename db_session to db for simplicity
* :pushpin: Update dependencies installation for testing
2020-04-20 19:03:13 +02:00
|
|
|
return v
|
|
|
|
raise ValueError(v)
|
2020-04-16 23:56:10 -06:00
|
|
|
|
|
|
|
PROJECT_NAME: str
|
2024-02-25 19:39:33 +01:00
|
|
|
SENTRY_DSN: HttpUrl | None = None
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
@field_validator("SENTRY_DSN", mode="before")
|
|
|
|
@classmethod
|
2024-02-25 19:39:33 +01:00
|
|
|
def sentry_dsn_can_be_blank(cls, v: str) -> str | None:
|
2024-02-29 02:03:23 +01:00
|
|
|
if not v:
|
2020-04-16 23:56:10 -06:00
|
|
|
return None
|
|
|
|
return v
|
|
|
|
|
|
|
|
POSTGRES_SERVER: str
|
|
|
|
POSTGRES_USER: str
|
|
|
|
POSTGRES_PASSWORD: str
|
|
|
|
POSTGRES_DB: str
|
2024-02-25 19:39:33 +01:00
|
|
|
SQLALCHEMY_DATABASE_URI: PostgresDsn | None = None
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
@field_validator("SQLALCHEMY_DATABASE_URI", mode="before")
|
|
|
|
def assemble_db_connection(cls, v: str | None, info: ValidationInfo) -> Any:
|
2020-04-16 23:56:10 -06:00
|
|
|
if isinstance(v, str):
|
|
|
|
return v
|
|
|
|
return PostgresDsn.build(
|
2023-11-25 00:08:22 +01:00
|
|
|
scheme="postgresql+psycopg",
|
2024-02-28 17:24:24 -05:00
|
|
|
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 ''}",
|
2020-04-16 23:56:10 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
SMTP_TLS: bool = True
|
2024-02-25 19:39:33 +01:00
|
|
|
SMTP_PORT: int | None = None
|
|
|
|
SMTP_HOST: str | None = None
|
|
|
|
SMTP_USER: str | None = None
|
|
|
|
SMTP_PASSWORD: str | None = None
|
2024-02-29 02:03:23 +01:00
|
|
|
# TODO: update type to EmailStr when sqlmodel supports it
|
|
|
|
EMAILS_FROM_EMAIL: str | None = None
|
2024-02-25 19:39:33 +01:00
|
|
|
EMAILS_FROM_NAME: str | None = None
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
@field_validator("EMAILS_FROM_NAME")
|
|
|
|
def get_project_name(cls, v: str | None, info: ValidationInfo) -> str:
|
2020-04-16 23:56:10 -06:00
|
|
|
if not v:
|
2024-02-28 17:24:24 -05:00
|
|
|
return info.data["PROJECT_NAME"]
|
2020-04-16 23:56:10 -06:00
|
|
|
return v
|
|
|
|
|
|
|
|
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
|
|
|
|
EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build"
|
|
|
|
EMAILS_ENABLED: bool = False
|
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
@field_validator("EMAILS_ENABLED", mode="before")
|
|
|
|
def get_emails_enabled(cls, v: bool, info: ValidationInfo) -> bool:
|
2020-04-16 23:56:10 -06:00
|
|
|
return bool(
|
2024-02-28 17:24:24 -05:00
|
|
|
info.data.get("SMTP_HOST")
|
|
|
|
and info.data.get("SMTP_PORT")
|
|
|
|
and info.data.get("EMAILS_FROM_EMAIL")
|
2020-04-16 23:56:10 -06:00
|
|
|
)
|
|
|
|
|
2024-02-29 02:03:23 +01:00
|
|
|
# TODO: update type to EmailStr when sqlmodel supports it
|
|
|
|
EMAIL_TEST_USER: str = "test@example.com"
|
|
|
|
# TODO: update type to EmailStr when sqlmodel supports it
|
|
|
|
FIRST_SUPERUSER: str
|
2020-04-16 23:56:10 -06:00
|
|
|
FIRST_SUPERUSER_PASSWORD: str
|
|
|
|
USERS_OPEN_REGISTRATION: bool = False
|
2024-02-28 17:24:24 -05:00
|
|
|
model_config = SettingsConfigDict(case_sensitive=True)
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2020-04-17 08:20:48 -04:00
|
|
|
|
2020-04-16 23:56:10 -06:00
|
|
|
settings = Settings()
|