-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⬆ Upgrade code to support pydantic V2 (#615)
- Loading branch information
1 parent
2346b81
commit 0839575
Showing
7 changed files
with
47 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 @@ def assemble_cors_origins(cls, v: str | list[str]) -> list[str] | str: | |
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,51 +48,49 @@ def sentry_dsn_can_be_blank(cls, v: str) -> str | None: | |
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 | ||
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_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 = "[email protected]" # type: ignore | ||
FIRST_SUPERUSER: EmailStr | ||
EMAIL_TEST_USER: str = "[email protected]" #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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters