Skip to content

Commit

Permalink
Pydantic v2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Temmmmmo committed Jul 19, 2023
1 parent c429bd4 commit 0bbd357
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions marketing_api/routes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def write_action(
"""Создать действие"""
user_id = user.get("id") if user else None
logger.debug(f"write_action by {user_id=}")
ai = ActionsInfo(**user_action_info.dict())
ai = ActionsInfo(**user_action_info.model_dump())
db.session.add(ai)
db.session.flush()
if ai.user:
Expand Down Expand Up @@ -95,7 +95,7 @@ async def http_error_handler(req, exc):

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
19 changes: 9 additions & 10 deletions marketing_api/routes/models.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class Base(BaseModel):
class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)


class ActionInfo(Base):
user_id: int | None
user_id: int | None = None
action: str
additional_data: str | None
path_from: str | None
path_to: str | None
additional_data: str | None = None
path_from: str | None = None
path_to: str | None = None


class User(Base):
id: int
union_number: str | None
union_number: str | None = None


class UserPatch(Base):
union_number: str | None
auth_user_id: int | None
union_number: str | None = None
auth_user_id: int | None = None
9 changes: 3 additions & 6 deletions marketing_api/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from functools import lru_cache

from pydantic import BaseSettings, PostgresDsn
from pydantic import ConfigDict, PostgresDsn
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
Expand All @@ -15,11 +16,7 @@ class Settings(BaseSettings):
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
2 changes: 1 addition & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion migrations/versions/0ea7185ac58b_useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Create Date: 2023-05-05 12:25:03.383848
"""
from alembic import op
import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ SQLAlchemy
gunicorn
auth-lib-profcomff[fastapi]
logging-profcomff
pydantic-settings
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def client():
@pytest.fixture(scope='session')
def dbsession():
settings = get_settings()
engine = create_engine(settings.DB_DSN)
engine = create_engine(str(settings.DB_DSN))
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
yield TestingSessionLocal()
Expand Down

0 comments on commit 0bbd357

Please sign in to comment.