Skip to content

Commit

Permalink
Merge pull request #3 from profcomff/Pydantic-2.0
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Temmmmmo committed Jul 31, 2023
2 parents c901f16 + 89c5d4c commit 1d57353
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
8 changes: 4 additions & 4 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from sqlalchemy import engine_from_config, pool

from rating_api.models.base import Base
from rating_api.settings import get_settings


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
Expand Down Expand Up @@ -61,7 +61,7 @@ def run_migrations_online():
"""
configuration = config.get_section(config.config_ini_section)
configuration['sqlalchemy.url'] = settings.DB_DSN
configuration['sqlalchemy.url'] = str(settings.DB_DSN)
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
Expand Down
1 change: 1 addition & 0 deletions rating_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os


__version__ = os.getenv('APP_VERSION', 'dev')
5 changes: 3 additions & 2 deletions rating_api/routes/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi_sqlalchemy import DBSessionMiddleware

from rating_api import __version__
from rating_api.settings import get_settings


settings = get_settings()
app = FastAPI(
title='Рейтинг преподавателей',
description='Хранение и работа с рейтингом преподавателей и отзывами на них.',
version=__version__,

# Отключаем нелокальную документацию
root_path=settings.ROOT_PATH if __version__ != 'dev' else '/',
docs_url=None if __version__ != 'dev' else '/docs',
Expand All @@ -19,7 +20,7 @@

app.add_middleware(
DBSessionMiddleware,
db_url=settings.DB_DSN,
db_url=str(settings.DB_DSN),
engine_args={"pool_pre_ping": True, "isolation_level": "AUTOCOMMIT"},
)

Expand Down
7 changes: 3 additions & 4 deletions rating_api/routes/models/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class Base(BaseModel):
def __repr__(self) -> str:
attrs = []
for k, v in self.__class__.schema().items():
for k, v in self.__class__.model_json_schema().items():
attrs.append(f"{k}={v}")
return "{}({})".format(self.__class__.__name__, ', '.join(attrs))

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True, extra="ignore")
9 changes: 3 additions & 6 deletions rating_api/settings.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import os
from functools import lru_cache

from pydantic import ConfigDict, PostgresDsn
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
"""Application settings"""

DB_DSN: str = 'postgresql://postgres@localhost:5432/postgres'
DB_DSN: PostgresDsn = 'postgresql://postgres@localhost:5432/postgres'
ROOT_PATH: str = '/' + os.getenv("APP_NAME", "")

CORS_ALLOW_ORIGINS: list[str] = ['*']
CORS_ALLOW_CREDENTIALS: bool = True
CORS_ALLOW_METHODS: list[str] = ['*']
CORS_ALLOW_HEADERS: list[str] = ['*']

class Config:
"""Pydantic BaseSettings config"""

case_sensitive = True
env_file = ".env"
model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="ignore")


@lru_cache
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fastapi-sqlalchemy
gunicorn
logging-profcomff
psycopg2-binary
pydantic[dotenv]
pydantic
pydantic-settings
SQLAlchemy
uvicorn

0 comments on commit 1d57353

Please sign in to comment.