Skip to content

Commit

Permalink
Merge pull request #32 from MrTrick/main
Browse files Browse the repository at this point in the history
Updated pydantic to v2, updated fastapi to v0.103, other small fixes
  • Loading branch information
rafsaf authored Sep 11, 2023
2 parents 407f88c + 48b5941 commit 91a3596
Show file tree
Hide file tree
Showing 7 changed files with 774 additions and 718 deletions.
1 change: 1 addition & 0 deletions {{cookiecutter.project_name}}/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
log.txt

# Translations
*.mo
Expand Down
58 changes: 32 additions & 26 deletions {{cookiecutter.project_name}}/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"""

import tomllib
from functools import cached_property
from pathlib import Path
from typing import Literal

from pydantic import AnyHttpUrl, BaseSettings, EmailStr, PostgresDsn, validator
from pydantic import AnyHttpUrl, EmailStr, PostgresDsn, computed_field
from pydantic_settings import BaseSettings, SettingsConfigDict

PROJECT_DIR = Path(__file__).parent.parent.parent
with open(f"{PROJECT_DIR}/pyproject.toml", "rb") as f:
Expand All @@ -51,47 +53,51 @@ class Settings(BaseSettings):
DEFAULT_DATABASE_HOSTNAME: str
DEFAULT_DATABASE_USER: str
DEFAULT_DATABASE_PASSWORD: str
DEFAULT_DATABASE_PORT: str
DEFAULT_DATABASE_PORT: int
DEFAULT_DATABASE_DB: str
DEFAULT_SQLALCHEMY_DATABASE_URI: str = ""

# POSTGRESQL TEST DATABASE
TEST_DATABASE_HOSTNAME: str = "postgres"
TEST_DATABASE_USER: str = "postgres"
TEST_DATABASE_PASSWORD: str = "postgres"
TEST_DATABASE_PORT: str = "5432"
TEST_DATABASE_PORT: int = 5432
TEST_DATABASE_DB: str = "postgres"
TEST_SQLALCHEMY_DATABASE_URI: str = ""

# FIRST SUPERUSER
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+asyncpg",
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']}",
@computed_field
@cached_property
def DEFAULT_SQLALCHEMY_DATABASE_URI(self) -> str:
return str(
PostgresDsn.build(
scheme="postgresql+asyncpg",
username=self.DEFAULT_DATABASE_USER,
password=self.DEFAULT_DATABASE_PASSWORD,
host=self.DEFAULT_DATABASE_HOSTNAME,
port=self.DEFAULT_DATABASE_PORT,
path=self.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+asyncpg",
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']}",
@computed_field
@cached_property
def TEST_SQLALCHEMY_DATABASE_URI(self) -> str:
return str(
PostgresDsn.build(
scheme="postgresql+asyncpg",
username=self.TEST_DATABASE_USER,
password=self.TEST_DATABASE_PASSWORD,
host=self.TEST_DATABASE_HOSTNAME,
port=self.TEST_DATABASE_PORT,
path=self.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
6 changes: 2 additions & 4 deletions {{cookiecutter.project_name}}/app/schemas/responses.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel, ConfigDict, EmailStr


class BaseResponse(BaseModel):
# may define additional fields or config shared across responses
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class AccessTokenResponse(BaseResponse):
Expand Down
Loading

0 comments on commit 91a3596

Please sign in to comment.