Skip to content

Commit f3645d8

Browse files
pydantic2 (#42)
1 parent c80d862 commit f3645d8

File tree

10 files changed

+30
-32
lines changed

10 files changed

+30
-32
lines changed

.github/workflows/checks.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818
docker run -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust --name db-test postgres:15-alpine
1919
- uses: actions/setup-python@v4
2020
with:
21-
python-version: '3.10'
21+
python-version: '3.11'
2222
- name: Install dependencies
2323
run: |
2424
python -m ensurepip
2525
python -m pip install --upgrade pip
26-
pip install -r requirements.txt -r requirements.dev.txt
26+
pip install --upgrade -r requirements.txt -r requirements.dev.txt
2727
- name: Migrate DB
2828
run: |
2929
DB_DSN=postgresql://postgres@localhost:5432/postgres alembic upgrade head

migrations/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def run_migrations_online():
5050
5151
"""
5252
configuration = config.get_section(config.config_ini_section)
53-
configuration['sqlalchemy.url'] = settings.DB_DSN
53+
configuration['sqlalchemy.url'] = str(settings.DB_DSN)
5454
connectable = engine_from_config(
5555
configuration,
5656
prefix="sqlalchemy.",

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ SQLAlchemy
88
gunicorn
99
logging-profcomff
1010
auth-lib-profcomff[fastapi]
11+
pydantic_settings

services_backend/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import uvicorn
22

3-
from .routes.base import app
3+
from services_backend.routes.base import app
44

55

66
if __name__ == '__main__':

services_backend/routes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
app.add_middleware(
2828
DBSessionMiddleware,
29-
db_url=settings.DB_DSN,
29+
db_url=str(settings.DB_DSN),
3030
engine_args={"pool_pre_ping": True, "isolation_level": "AUTOCOMMIT"},
3131
)
3232

services_backend/routes/button.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ class ButtonCreate(Base):
2323

2424

2525
class ButtonUpdate(Base):
26-
category_id: int | None = Field(description='Айди категории')
27-
icon: str | None = Field(description='Иконка кнопки')
28-
name: str | None = Field(description='Название кнопки')
29-
order: int | None = Field(description='Порядок, в котором отображаются кнопки')
30-
link: str | None = Field(description='Ссылка, на которую перенаправляет кнопка')
31-
type: Type | None = Field(description='Тип открываемой ссылки (Ссылка приложения/Браузер в приложении/Браузер')
26+
category_id: int | None = Field(description='Айди категории', default=None)
27+
icon: str | None = Field(description='Иконка кнопки', default=None)
28+
name: str | None = Field(description='Название кнопки', default=None)
29+
order: int | None = Field(description='Порядок, в котором отображаются кнопки', default=None)
30+
link: str | None = Field(description='Ссылка, на которую перенаправляет кнопка', default=None)
31+
type: Type | None = Field(
32+
description='Тип открываемой ссылки (Ссылка приложения/Браузер в приложении/Браузер', default=None
33+
)
3234

3335

3436
class ButtonGet(Base):
@@ -41,7 +43,7 @@ class ButtonGet(Base):
4143

4244

4345
class ButtonsGet(Base):
44-
buttons: list[ButtonGet] | None
46+
buttons: list[ButtonGet] | None = None
4547

4648

4749
# endregion

services_backend/routes/category.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ class ButtonGet(Base):
2929
class CategoryCreate(Base):
3030
type: str = Field(description='Тип отображения категории')
3131
name: str = Field(description='Имя категории')
32-
scopes: set[str] | None = Field(description='Каким пользователям будет видна категория')
32+
scopes: set[str] | None = Field(description='Каким пользователям будет видна категория', default=None)
3333

3434

3535
class CategoryUpdate(Base):
36-
order: int | None = Field(description='На какую позицию перенести категорию')
37-
type: str | None = Field(description='Тип отображения категории')
38-
name: str | None = Field(description='Имя категории')
39-
scopes: set[str] | None = Field(description='Каким пользователям будет видна категория')
36+
order: int | None = Field(description='На какую позицию перенести категорию', default=None)
37+
type: str | None = Field(description='Тип отображения категории', default=None)
38+
name: str | None = Field(description='Имя категории', default=None)
39+
scopes: set[str] | None = Field(description='Каким пользователям будет видна категория', default=None)
4040

4141

4242
class CategoryGet(Base):
4343
id: int
4444
order: int
45-
type: str | None
46-
name: str | None
47-
buttons: list[ButtonGet] | None
48-
scopes: list[str] | None
45+
type: str | None = None
46+
name: str | None = None
47+
buttons: list[ButtonGet] | None = None
48+
scopes: list[str] | None = None
4949

5050

5151
# endregion

services_backend/schemas.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydantic import BaseModel
1+
from pydantic import BaseModel, ConfigDict
22

33

44
class Base(BaseModel):
@@ -8,5 +8,4 @@ def __repr__(self) -> str:
88
attrs.append(f"{k}={v}")
99
return "{}({})".format(self.__class__.__name__, ', '.join(attrs))
1010

11-
class Config:
12-
orm_mode = True
11+
model_config = ConfigDict(from_attributes=True, extra="ignore")

services_backend/settings.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
from functools import lru_cache
33

4-
from pydantic import BaseSettings, PostgresDsn
4+
from pydantic import ConfigDict, PostgresDsn
5+
from pydantic_settings import BaseSettings
56

67

78
class Settings(BaseSettings):
@@ -14,12 +15,7 @@ class Settings(BaseSettings):
1415
CORS_ALLOW_CREDENTIALS: bool = True
1516
CORS_ALLOW_METHODS: list[str] = ['*']
1617
CORS_ALLOW_HEADERS: list[str] = ['*']
17-
18-
class Config:
19-
"""Pydantic BaseSettings config"""
20-
21-
case_sensitive = True
22-
env_file = ".env"
18+
model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="ignore")
2319

2420

2521
@lru_cache

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def client(mocker: MockerFixture):
2727
@pytest.fixture(scope='session')
2828
def dbsession() -> Session:
2929
settings = get_settings()
30-
engine = create_engine(settings.DB_DSN, execution_options={"isolation_level": "AUTOCOMMIT"})
30+
engine = create_engine(str(settings.DB_DSN), execution_options={"isolation_level": "AUTOCOMMIT"})
3131
TestingSessionLocal = sessionmaker(bind=engine)
3232
Base.metadata.drop_all(bind=engine)
3333
Base.metadata.create_all(bind=engine)

0 commit comments

Comments
 (0)