|
|
@@ -11,11 +11,6 @@ Pydantic priority ordering:
|
|
|
For project name, version, description we use pyproject.toml
|
|
|
For the rest, we use file `.env` (gitignored), see `.env.example`
|
|
|
|
|
|
-`DEFAULT_SQLALCHEMY_DATABASE_URI` and `TEST_SQLALCHEMY_DATABASE_URI`:
|
|
|
-Both are ment to be validated at the runtime, do not change unless you know
|
|
|
-what are you doing. All the two validators do is to build full URI (TCP protocol)
|
|
|
-to databases to avoid typo bugs.
|
|
|
-
|
|
|
See https://pydantic-docs.helpmanual.io/usage/settings/
|
|
|
|
|
|
Note, complex types like lists are read as json-encoded strings.
|
|
|
@@ -25,13 +20,21 @@ import tomllib
|
|
|
from pathlib import Path
|
|
|
from typing import Literal
|
|
|
|
|
|
-from pydantic import AnyHttpUrl, BaseSettings, EmailStr, PostgresDsn, validator
|
|
|
+from pydantic import AnyHttpUrl, EmailStr, PostgresDsn, field_validator, validator
|
|
|
+from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
PROJECT_DIR = Path(__file__).parent.parent.parent
|
|
|
with open(f"{PROJECT_DIR}/pyproject.toml", "rb") as f:
|
|
|
PYPROJECT_CONTENT = tomllib.load(f)["tool"]["poetry"]
|
|
|
|
|
|
|
|
|
+def build_postgreuri(
|
|
|
+ scheme: str, username: str, password: str, host: str, port: int = 0, path: str = ""
|
|
|
+):
|
|
|
+ fullhost = host + (f":{port}" if port > 0 else "")
|
|
|
+ return f"{scheme}://{username}:{password}@{fullhost}{path}"
|
|
|
+
|
|
|
+
|
|
|
class Settings(BaseSettings):
|
|
|
# CORE SETTINGS
|
|
|
SECRET_KEY: str
|
|
|
@@ -53,7 +56,17 @@ class Settings(BaseSettings):
|
|
|
DEFAULT_DATABASE_PASSWORD: str
|
|
|
DEFAULT_DATABASE_PORT: str
|
|
|
DEFAULT_DATABASE_DB: str
|
|
|
- DEFAULT_SQLALCHEMY_DATABASE_URI: str = ""
|
|
|
+
|
|
|
+ @property
|
|
|
+ def DEFAULT_SQLALCHEMY_DATABASE_URI(self) -> str:
|
|
|
+ return build_postgreuri(
|
|
|
+ scheme="postgresql",
|
|
|
+ username=self.DEFAULT_DATABASE_USER,
|
|
|
+ password=self.DEFAULT_DATABASE_PASSWORD,
|
|
|
+ host=self.DEFAULT_DATABASE_HOSTNAME,
|
|
|
+ port=int(self.DEFAULT_DATABASE_PORT),
|
|
|
+ path=f"/{self.DEFAULT_DATABASE_DB}",
|
|
|
+ )
|
|
|
|
|
|
# POSTGRESQL TEST DATABASE
|
|
|
TEST_DATABASE_HOSTNAME: str = "postgres"
|
|
|
@@ -61,7 +74,17 @@ class Settings(BaseSettings):
|
|
|
TEST_DATABASE_PASSWORD: str = "postgres"
|
|
|
TEST_DATABASE_PORT: str = "5432"
|
|
|
TEST_DATABASE_DB: str = "postgres"
|
|
|
- TEST_SQLALCHEMY_DATABASE_URI: str = ""
|
|
|
+
|
|
|
+ @property
|
|
|
+ def TEST_SQLALCHEMY_DATABASE_URI(self) -> str:
|
|
|
+ return build_postgreuri(
|
|
|
+ scheme="postgresql",
|
|
|
+ username=self.TEST_DATABASE_USER,
|
|
|
+ password=self.TEST_DATABASE_PASSWORD,
|
|
|
+ host=self.TEST_DATABASE_HOSTNAME,
|
|
|
+ port=int(self.TEST_DATABASE_PORT),
|
|
|
+ path=f"/{self.TEST_DATABASE_DB}",
|
|
|
+ )
|
|
|
|
|
|
# SMS batch
|
|
|
BATCH_SMS_PHONE_NUMBER: str = ""
|
|
|
@@ -70,31 +93,7 @@ class Settings(BaseSettings):
|
|
|
FIRST_SUPERUSER_EMAIL: EmailStr
|
|
|
FIRST_SUPERUSER_PASSWORD: str
|
|
|
|
|
|
- @validator("DEFAULT_SQLALCHEMY_DATABASE_URI")
|
|
|
- def _assemble_default_db_connection(cls, v: str, values: dict[str, str]) -> str:
|
|
|
- return PostgresDsn.build(
|
|
|
- scheme="postgresql",
|
|
|
- user=values["DEFAULT_DATABASE_USER"],
|
|
|
- password=values["DEFAULT_DATABASE_PASSWORD"],
|
|
|
- host=values["DEFAULT_DATABASE_HOSTNAME"],
|
|
|
- port=values["DEFAULT_DATABASE_PORT"],
|
|
|
- path=f"/{values['DEFAULT_DATABASE_DB']}",
|
|
|
- )
|
|
|
-
|
|
|
- @validator("TEST_SQLALCHEMY_DATABASE_URI")
|
|
|
- def _assemble_test_db_connection(cls, v: str, values: dict[str, str]) -> str:
|
|
|
- return PostgresDsn.build(
|
|
|
- scheme="postgresql",
|
|
|
- user=values["TEST_DATABASE_USER"],
|
|
|
- password=values["TEST_DATABASE_PASSWORD"],
|
|
|
- host=values["TEST_DATABASE_HOSTNAME"],
|
|
|
- port=values["TEST_DATABASE_PORT"],
|
|
|
- path=f"/{values['TEST_DATABASE_DB']}",
|
|
|
- )
|
|
|
-
|
|
|
- class Config:
|
|
|
- env_file = f"{PROJECT_DIR}/.env"
|
|
|
- case_sensitive = True
|
|
|
+ model_config = SettingsConfigDict(env_file=f"{PROJECT_DIR}/.env", case_sensitive=True)
|
|
|
|
|
|
|
|
|
settings: Settings = Settings() # type: ignore
|