refactoring

main
Сергей Ванюшкин 2024-03-23 16:29:06 +00:00
parent 15549f53d5
commit b22f6a3beb
14 changed files with 38 additions and 36 deletions

View File

@ -23,7 +23,7 @@ repos:
rev: v3.3.1 rev: v3.3.1
hooks: hooks:
- id: pyupgrade - id: pyupgrade
args: [ --py310-plus ] args: [ --py311-plus ]
# Форматирует код под PEP8 # Форматирует код под PEP8
- repo: https://github.com/pre-commit/mirrors-autopep8 - repo: https://github.com/pre-commit/mirrors-autopep8
@ -33,20 +33,20 @@ repos:
args: [ "-i", "--in-place", "--max-line-length=120" ] args: [ "-i", "--in-place", "--max-line-length=120" ]
# Сканер стилистических ошибок, нарушающие договоренности PEP8 # Сканер стилистических ошибок, нарушающие договоренности PEP8
- repo: https://github.com/PyCQA/flake8 # - repo: https://github.com/PyCQA/flake8
rev: 6.0.0 # rev: 6.0.0
hooks: # hooks:
- id: flake8 # - id: flake8
exclude: __init__.py # exclude: __init__.py
args: [ "--ignore=E501,F821", "--max-line-length=120" ] # args: [ "--ignore=E501,F821", "--max-line-length=120" ]
# Форматирует код под PEP8 c помощью black # Форматирует код под PEP8 c помощью black
- repo: https://github.com/psf/black # - repo: https://github.com/psf/black
rev: 23.1.0 # rev: 23.1.0
hooks: # hooks:
- id: black # - id: black
language_version: python3.10 # language_version: python3.10
args: [ "--line-length=120" ] # args: [ "--line-length=120" ]
# Проверка статических типов с помощью mypy # Проверка статических типов с помощью mypy
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy

View File

@ -2,8 +2,7 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI from fastapi import FastAPI
from .routers.auth import router as auth_router from .routers import auth_router, user_router
from .routers.user import router as user_router
@asynccontextmanager @asynccontextmanager

View File

@ -2,7 +2,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
from .config import get_settings from .config import get_settings
from .services import AuthService, UserService from .services import AuthService, UserService
from .uow.uow_base import UnitOfWork from .uow import UnitOfWork
async_engine = create_async_engine( async_engine = create_async_engine(
url=get_settings().db.get_db_url, url=get_settings().db.get_db_url,

View File

@ -1,5 +1,5 @@
from .ref_repo import RefRepository from .ref_repo import RefRepository
from .user import UserRepository from .user_repo import UserRepository
__all__ = ( __all__ = (
"UserRepository", "UserRepository",

View File

@ -1,18 +1,16 @@
from uuid import UUID
from sqlalchemy import insert from sqlalchemy import insert
from sqlalchemy.ext.asyncio.session import AsyncSession from sqlalchemy.ext.asyncio.session import AsyncSession
from ..models import RefModel from ..models import RefModel
from ..schemas import RefReadDTO from ..schemas import RefReadDTO, UserAuthDTO
class RefRepository: class RefRepository:
def __init__(self, session: AsyncSession): def __init__(self, session: AsyncSession):
self.session = session self.session = session
async def add_ref(self, referer: UUID) -> RefReadDTO: async def add_ref(self, user: UserAuthDTO) -> RefReadDTO:
stmt = insert(RefModel).values(referer=referer, is_active=True).returning(RefModel) stmt = insert(RefModel).values(referer=user.id, is_active=True, referals=[]).returning(RefModel)
res = await self.session.execute(stmt) res = await self.session.execute(stmt)
return RefReadDTO.model_validate(res.scalar_one()) return RefReadDTO.model_validate(res.scalar_one())

View File

@ -2,7 +2,7 @@ from sqlalchemy import insert, select
from sqlalchemy.ext.asyncio.session import AsyncSession from sqlalchemy.ext.asyncio.session import AsyncSession
from ..models import UserModel from ..models import UserModel
from ..schemas.user_schema import UserDBDTO, UserReadDTO from ..schemas import UserDBDTO, UserReadDTO
class UserRepository: class UserRepository:

View File

@ -0,0 +1,7 @@
from .auth_route import router as auth_router
from .user_route import router as user_router
__all__ = (
"auth_router",
"user_router",
)

View File

@ -22,7 +22,6 @@ async def create_refer_code(
user_service: UserService = Depends(get_user_service), user_service: UserService = Depends(get_user_service),
user: UserAuthDTO = Depends(get_current_user), user: UserAuthDTO = Depends(get_current_user),
): ):
print(user) res = await user_service.create_ref(user=user)
res = await user_service.create_ref(email=user.email)
return res return res

View File

@ -1,5 +1,5 @@
from .auth_service import AuthService, get_current_user from .auth_service import AuthService, get_current_user
from .user import UserService from .user_service import UserService
__all__ = ( __all__ = (
"UserService", "UserService",

View File

@ -6,9 +6,8 @@ from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt from jose import JWTError, jwt
from passlib.context import CryptContext from passlib.context import CryptContext
from test_api.uow.uow_base import UnitOfWork
from ..schemas import TokenSchema, UserAuthDTO from ..schemas import TokenSchema, UserAuthDTO
from ..uow import UnitOfWork
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@ -49,9 +48,7 @@ class AuthService:
self.uow = uow self.uow = uow
self.crypto_context = CryptContext(schemes="bcrypt") self.crypto_context = CryptContext(schemes="bcrypt")
async def authenticate( async def authenticate(self, login: OAuth2PasswordRequestForm = Depends()) -> TokenSchema | None:
self, login: OAuth2PasswordRequestForm = Depends()
) -> TokenSchema | None:
async with self.uow: async with self.uow:
user = await self.uow.users.find_one(filter={"email": login.username}) user = await self.uow.users.find_one(filter={"email": login.username})

View File

@ -1,7 +1,7 @@
from passlib.context import CryptContext from passlib.context import CryptContext
from ..schemas.user_schema import UserDBDTO, UserWriteDTO from ..schemas import UserAuthDTO, UserDBDTO, UserWriteDTO
from ..uow.uow_base import UnitOfWork from ..uow import UnitOfWork
class UserService: class UserService:
@ -22,8 +22,7 @@ class UserService:
user = await self.uow.users.find_one(filter={"email": email}) user = await self.uow.users.find_one(filter={"email": email})
return user return user
async def create_ref(self, email: str): async def create_ref(self, user: UserAuthDTO):
async with self.uow: async with self.uow:
user = await self.uow.users.find_one(filter={"email": email}) res = await self.uow.ref.add_ref(user=user)
res = await self.uow.ref.add_ref(referer=user.id)
return res return res

View File

@ -0,0 +1,3 @@
from .sqlalchemy_uow import UnitOfWork
__all__ = ("UnitOfWork",)