From 59cb2d4728450cc7f63bb456c5cc12d7b0b36dce Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Tue, 9 Jan 2024 06:01:52 +0000 Subject: [PATCH] sync --- .../db_management_and_automation/pytest.ini | 2 +- .../servicemanager/backend/database.py | 25 --------- .../servicemanager/backend/queries/user.py | 25 --------- .../{servicemanager => src}/__init__.py | 0 .../{servicemanager => src}/api/__init__.py | 0 .../{servicemanager => src}/api/routes.py | 0 .../backend/__init__.py | 0 .../src/backend/database.py | 52 +++++++++++++++++++ .../backend/models/__init__.py | 0 .../backend/models/user.py | 16 +++--- .../backend/queries/__init__.py | 0 .../src/backend/queries/user.py | 18 +++++++ .../{servicemanager => src}/config.py | 2 +- .../frontend/__init__.py | 0 .../frontend/routes.py | 4 +- .../frontend/templates/test.html | 0 .../{servicemanager => src}/main.py | 7 ++- .../tests/test_frontend.py | 2 +- .../tests/test_main.py | 2 +- 19 files changed, 89 insertions(+), 66 deletions(-) delete mode 100644 information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/database.py delete mode 100644 information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/user.py rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/__init__.py (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/api/__init__.py (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/api/routes.py (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/backend/__init__.py (100%) create mode 100644 information_system_and_programming/term_5/db_management_and_automation/src/backend/database.py rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/backend/models/__init__.py (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/backend/models/user.py (79%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/backend/queries/__init__.py (100%) create mode 100644 information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/user.py rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/config.py (89%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/frontend/__init__.py (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/frontend/routes.py (76%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/frontend/templates/test.html (100%) rename information_system_and_programming/term_5/db_management_and_automation/{servicemanager => src}/main.py (91%) diff --git a/information_system_and_programming/term_5/db_management_and_automation/pytest.ini b/information_system_and_programming/term_5/db_management_and_automation/pytest.ini index 70ec218..f9965b9 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/pytest.ini +++ b/information_system_and_programming/term_5/db_management_and_automation/pytest.ini @@ -1,4 +1,4 @@ [pytest] -pythonpath = . servicemanager +pythonpath = . src ; env_files = ; .test.env diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/database.py b/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/database.py deleted file mode 100644 index 072fb8d..0000000 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/database.py +++ /dev/null @@ -1,25 +0,0 @@ -import datetime -from typing import Annotated - -from sqlalchemy import text -from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine -from sqlalchemy.orm import DeclarativeBase, mapped_column - -from config import settings - - -async_engine = create_async_engine(settings.DATABASE_URL_asyncpg) -async_session_factory = async_sessionmaker(async_engine) - - -intpk = Annotated[int, mapped_column(primary_key=True)] -created_at = Annotated[datetime.datetime, mapped_column(server_default=text("TIMEZONE('utc', now())"))] -updated_at = Annotated[datetime.datetime, mapped_column( - server_default=text("TIMEZONE('utc', now())"), - onupdate=datetime.datetime.utcnow, - )] - - -class Base(DeclarativeBase): - pass - diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/user.py b/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/user.py deleted file mode 100644 index 39becf2..0000000 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/user.py +++ /dev/null @@ -1,25 +0,0 @@ -from backend.database import Base, async_engine, async_session_factory - -from backend.models.user import User, Role - -class UserORM: - @staticmethod - async def create_tables(): - # Удаление и создание пустых таблиц в БД - async with async_engine.begin() as conn: - await conn.run_sync(Base.metadata.drop_all) - await conn.run_sync(Base.metadata.create_all) - - @staticmethod - async def insert_admin(): - async with async_session_factory() as session: - admin = User( - email="admin@mail.com", - hashed_password="sdfasdfasdf", - is_active=True, - is_superuser=True, - is_verified=True, - ) - session.add(admin) - # flush взаимодействует с БД, поэтому пишем await - await session.commit() diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/api/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/api/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/api/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/api/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/api/routes.py b/information_system_and_programming/term_5/db_management_and_automation/src/api/routes.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/api/routes.py rename to information_system_and_programming/term_5/db_management_and_automation/src/api/routes.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/backend/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/src/backend/database.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/database.py new file mode 100644 index 0000000..db45af2 --- /dev/null +++ b/information_system_and_programming/term_5/db_management_and_automation/src/backend/database.py @@ -0,0 +1,52 @@ +import datetime +from typing import Annotated, AsyncGenerator + +from fastapi import Depends +from fastapi_users.db import SQLAlchemyUserDatabase +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine +from sqlalchemy.orm import DeclarativeBase, mapped_column + +from config import settings + +async_engine = create_async_engine(settings.DATABASE_URL_asyncpg) +async_session_maker = async_sessionmaker( + async_engine, + class_=AsyncSession, + expire_on_commit=False, +) + +intpk = Annotated[int, mapped_column(primary_key=True)] +created_at = Annotated[ + datetime.datetime, + mapped_column(server_default=text("TIMEZONE('utc', now())")), +] +updated_at = Annotated[ + datetime.datetime, + mapped_column( + server_default=text("TIMEZONE('utc', now())"), + onupdate=datetime.datetime.utcnow, + ), +] + + +class Base(DeclarativeBase): + pass + + +from backend.models.user import User + + +async def create_db_and_tables(): + async with async_engine.begin() as conn: + await conn.run_sync(Base.metadata.drop_all) + await conn.run_sync(Base.metadata.create_all) + + +async def get_async_session() -> AsyncGenerator[AsyncSession, None]: + async with async_session_maker() as session: + yield session + + +async def get_user_db(session: AsyncSession = Depends(get_async_session)): + yield SQLAlchemyUserDatabase(session, User) diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/models/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/models/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/models/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/backend/models/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/models/user.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/models/user.py similarity index 79% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/models/user.py rename to information_system_and_programming/term_5/db_management_and_automation/src/backend/models/user.py index d350743..5548414 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/models/user.py +++ b/information_system_and_programming/term_5/db_management_and_automation/src/backend/models/user.py @@ -1,8 +1,8 @@ -from typing import Optional, List +from typing import List, Optional from fastapi_users.db import SQLAlchemyBaseUserTable from sqlalchemy import ForeignKey -from sqlalchemy.orm import Mapped, relationship, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, relationship from backend.database import Base, intpk @@ -11,30 +11,30 @@ class UserRoleAssociation(Base): """ Модель таблицы связи Many2Many для Role & User """ + __tablename__ = "user_role_association" user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True) - role_id: Mapped[int] = mapped_column( - ForeignKey("role.id"), primary_key=True - ) + role_id: Mapped[int] = mapped_column(ForeignKey("role.id"), primary_key=True) extra_data: Mapped[Optional[str]] child: Mapped["Role"] = relationship() - + class Role(Base): """ Модель таблицы role """ + __tablename__ = "role" id: Mapped[intpk] role: Mapped[str] - + class User(SQLAlchemyBaseUserTable[int], Base): """ Модель таблицы user """ + __tablename__ = "user" id: Mapped[intpk] children: Mapped[List["UserRoleAssociation"]] = relationship() - diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/backend/queries/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/user.py b/information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/user.py new file mode 100644 index 0000000..4c2a42e --- /dev/null +++ b/information_system_and_programming/term_5/db_management_and_automation/src/backend/queries/user.py @@ -0,0 +1,18 @@ +from backend.database import async_session_maker +from backend.models.user import Role, User + + +class UserORM: + @staticmethod + async def insert_admin(): + async with async_session_maker() as session: + admin = User( + email="admin@mail.com", + hashed_password="sdfasdfasdf", + is_active=True, + is_superuser=True, + is_verified=True, + ) + session.add(admin) + # flush взаимодействует с БД, поэтому пишем await + await session.commit() diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/config.py b/information_system_and_programming/term_5/db_management_and_automation/src/config.py similarity index 89% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/config.py rename to information_system_and_programming/term_5/db_management_and_automation/src/config.py index c088755..5d2fc68 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/config.py +++ b/information_system_and_programming/term_5/db_management_and_automation/src/config.py @@ -15,7 +15,7 @@ class Settings(BaseSettings): """ return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}" - model_config = SettingsConfigDict(env_file=".env") + model_config = SettingsConfigDict(env_file="src/.env") settings = Settings() diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/__init__.py b/information_system_and_programming/term_5/db_management_and_automation/src/frontend/__init__.py similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/__init__.py rename to information_system_and_programming/term_5/db_management_and_automation/src/frontend/__init__.py diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/routes.py b/information_system_and_programming/term_5/db_management_and_automation/src/frontend/routes.py similarity index 76% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/routes.py rename to information_system_and_programming/term_5/db_management_and_automation/src/frontend/routes.py index 513cd20..509084e 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/routes.py +++ b/information_system_and_programming/term_5/db_management_and_automation/src/frontend/routes.py @@ -9,12 +9,12 @@ site_router = APIRouter() site_router.mount( "/static", - StaticFiles(directory="servicemanager/frontend/static"), + StaticFiles(directory="src/frontend/static"), name="static", ) -templates = Jinja2Templates(directory="servicemanager/frontend/templates") +templates = Jinja2Templates(directory="src/frontend/templates") @site_router.get("/testpage", response_class=HTMLResponse) diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/templates/test.html b/information_system_and_programming/term_5/db_management_and_automation/src/frontend/templates/test.html similarity index 100% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/frontend/templates/test.html rename to information_system_and_programming/term_5/db_management_and_automation/src/frontend/templates/test.html diff --git a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/main.py b/information_system_and_programming/term_5/db_management_and_automation/src/main.py similarity index 91% rename from information_system_and_programming/term_5/db_management_and_automation/servicemanager/main.py rename to information_system_and_programming/term_5/db_management_and_automation/src/main.py index d5dc4ef..f0ba71e 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/servicemanager/main.py +++ b/information_system_and_programming/term_5/db_management_and_automation/src/main.py @@ -2,8 +2,9 @@ import asyncio import sys import uvicorn -from api.routes import api_router from fastapi import FastAPI + +from api.routes import api_router from frontend.routes import site_router @@ -11,8 +12,10 @@ async def generate_test_data(): """ Создание БД и наполнение ее данными """ + from backend.database import create_db_and_tables from backend.queries.user import UserORM - await UserORM.create_tables() + + await create_db_and_tables() await UserORM.insert_admin() diff --git a/information_system_and_programming/term_5/db_management_and_automation/tests/test_frontend.py b/information_system_and_programming/term_5/db_management_and_automation/tests/test_frontend.py index 842068d..1070d01 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/tests/test_frontend.py +++ b/information_system_and_programming/term_5/db_management_and_automation/tests/test_frontend.py @@ -1,6 +1,6 @@ from starlette.testclient import TestClient -from servicemanager.main import create_app +from src.main import create_app client = TestClient(create_app()) diff --git a/information_system_and_programming/term_5/db_management_and_automation/tests/test_main.py b/information_system_and_programming/term_5/db_management_and_automation/tests/test_main.py index 1ea533d..7b2c96c 100644 --- a/information_system_and_programming/term_5/db_management_and_automation/tests/test_main.py +++ b/information_system_and_programming/term_5/db_management_and_automation/tests/test_main.py @@ -1,6 +1,6 @@ from starlette.testclient import TestClient -from servicemanager.main import create_app +from src.main import create_app client = TestClient(create_app())