2020-04-16 23:56:10 -06:00
|
|
|
import secrets
|
2024-03-11 16:34:18 +01:00
|
|
|
from typing import Annotated, Any, Literal
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2024-02-28 17:24:24 -05:00
|
|
|
from pydantic import (
|
2024-03-11 16:34:18 +01:00
|
|
|
AnyUrl,
|
|
|
|
BeforeValidator,
|
2024-02-28 17:24:24 -05:00
|
|
|
HttpUrl,
|
|
|
|
PostgresDsn,
|
2024-03-11 16:34:18 +01:00
|
|
|
computed_field,
|
|
|
|
model_validator,
|
2024-02-28 17:24:24 -05:00
|
|
|
)
|
2024-03-11 16:34:18 +01:00
|
|
|
from pydantic_core import MultiHostUrl
|
2024-02-28 17:24:24 -05:00
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
2024-03-11 16:34:18 +01:00
|
|
|
from typing_extensions import Self
|
|
|
|
|
|
|
|
|
|
|
|
def parse_cors(v: Any) -> 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)
|
2019-02-09 19:42:36 +04:00
|
|
|
|
2020-04-16 23:56:10 -06:00
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
2024-03-12 13:31:31 +01:00
|
|
|
model_config = SettingsConfigDict(
|
|
|
|
env_file=".env", env_ignore_empty=True, extra="ignore"
|
|
|
|
)
|
2020-04-16 23:56:10 -06:00
|
|
|
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
|
2024-03-11 16:34:18 +01:00
|
|
|
DOMAIN: str = "localhost"
|
|
|
|
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-03-11 16:34:18 +01:00
|
|
|
@computed_field # type: ignore[misc]
|
|
|
|
@property
|
|
|
|
def server_host(self) -> str:
|
|
|
|
# Use HTTPS for anything other than local development
|
|
|
|
if self.ENVIRONMENT == "local":
|
|
|
|
return f"http://{self.DOMAIN}"
|
|
|
|
return f"https://{self.DOMAIN}"
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-03-11 16:34:18 +01:00
|
|
|
BACKEND_CORS_ORIGINS: Annotated[
|
|
|
|
list[AnyUrl] | str, BeforeValidator(parse_cors)
|
|
|
|
] = []
|
2020-04-16 23:56:10 -06:00
|
|
|
|
2024-03-11 16:34:18 +01:00
|
|
|
PROJECT_NAME: str
|
|
|
|
SENTRY_DSN: HttpUrl | None = None
|
2020-04-16 23:56:10 -06:00
|
|
|
POSTGRES_SERVER: str
|
|
|
|
POSTGRES_USER: str
|
|
|
|
POSTGRES_PASSWORD: str
|
2024-03-11 16:34:18 +01:00
|
|
|
POSTGRES_DB: str = ""
|
|
|
|
|
|
|
|
@computed_field # type: ignore[misc]
|
|
|
|
@property
|
|
|
|
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
|
|
|
|
return MultiHostUrl.build(
|
2023-11-25 00:08:22 +01:00
|
|
|
scheme="postgresql+psycopg",
|
2024-03-11 16:34:18 +01:00
|
|
|
username=self.POSTGRES_USER,
|
|
|
|
password=self.POSTGRES_PASSWORD,
|
|
|
|
host=self.POSTGRES_SERVER,
|
|
|
|
path=self.POSTGRES_DB,
|
2020-04-16 23:56:10 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
SMTP_TLS: bool = True
|
2024-03-12 08:03:04 -05:00
|
|
|
SMTP_SSL: bool = False
|
2024-03-11 16:34:18 +01:00
|
|
|
SMTP_PORT: int = 587
|
2024-02-25 19:39:33 +01:00
|
|
|
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-03-11 16:34:18 +01:00
|
|
|
@model_validator(mode="after")
|
|
|
|
def set_default_emails_from(self) -> Self:
|
|
|
|
if not self.EMAILS_FROM_NAME:
|
|
|
|
self.EMAILS_FROM_NAME = self.PROJECT_NAME
|
|
|
|
return self
|
2020-04-16 23:56:10 -06:00
|
|
|
|
|
|
|
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
|
2024-03-11 16:34:18 +01:00
|
|
|
|
|
|
|
@computed_field # type: ignore[misc]
|
|
|
|
@property
|
|
|
|
def emails_enabled(self) -> bool:
|
|
|
|
return bool(self.SMTP_HOST and self.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
|
|
|
|
|
2020-04-17 08:20:48 -04:00
|
|
|
|
2024-03-10 14:47:21 -05:00
|
|
|
settings = Settings() # type: ignore
|