diff --git a/backend/src/auth/__init__.py b/backend/src/auth/__init__.py index ec6df94..8b5520d 100644 --- a/backend/src/auth/__init__.py +++ b/backend/src/auth/__init__.py @@ -8,10 +8,12 @@ from jwt.exceptions import InvalidTokenError from pydantic import BaseModel from auth.config import AuthConfig -from database import SessionDependency +from database import DbSessionDependency from database.users import User +# TODO: evaluate FASTAPI-Users package + class Token(BaseModel): access_token: str token_type: str @@ -28,7 +30,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/token") router = APIRouter() -async def get_current_user(db: SessionDependency, token: str = Depends(oauth2_scheme)) -> User: +async def get_current_user(db: DbSessionDependency, token: str = Depends(oauth2_scheme)) -> User: credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", diff --git a/backend/src/auth/password.py b/backend/src/auth/password.py index 673751f..7e7537f 100644 --- a/backend/src/auth/password.py +++ b/backend/src/auth/password.py @@ -6,7 +6,7 @@ from fastapi.security import OAuth2PasswordRequestForm from sqlmodel import select from auth import Token, create_access_token, router -from database import SessionDependency +from database import DbSessionDependency from database.users import User @@ -21,7 +21,7 @@ def get_password_hash(password: str) -> str: return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") -def authenticate_user(db: SessionDependency, email: str, password: str) -> bool | User: +def authenticate_user(db: DbSessionDependency, email: str, password: str) -> bool | User: """ :param email: email of the USER @@ -39,7 +39,7 @@ def authenticate_user(db: SessionDependency, email: str, password: str) -> bool @router.post("/token") async def login_for_access_token( form_data: Annotated[OAuth2PasswordRequestForm, Depends()], - db: SessionDependency, + db: DbSessionDependency, ) -> Token: user = authenticate_user(db,form_data.username, form_data.password) if not user: diff --git a/backend/src/database/__init__.py b/backend/src/database/__init__.py index 93eb361..7ad61e9 100644 --- a/backend/src/database/__init__.py +++ b/backend/src/database/__init__.py @@ -23,5 +23,5 @@ def get_session() -> Generator[Session, Any, None]: with Session(engine) as session: yield session -SessionDependency = Annotated[Session, Depends(get_session)] +DbSessionDependency = Annotated[Session, Depends(get_session)] diff --git a/backend/src/indexer/generic.py b/backend/src/indexer/generic.py index 8eef87f..086fd93 100644 --- a/backend/src/indexer/generic.py +++ b/backend/src/indexer/generic.py @@ -7,6 +7,7 @@ from pydantic import BaseModel, computed_field from database.torrents import QualityMixin, Torrent +# TODO: use something like strategy pattern to make sorting more user customizable class IndexerQueryResult(BaseModel, QualityMixin): id: UUID = pydantic.Field(default_factory=uuid4) title: str diff --git a/backend/src/tv/router.py b/backend/src/tv/router.py index 879d4e2..dbd4396 100644 --- a/backend/src/tv/router.py +++ b/backend/src/tv/router.py @@ -12,7 +12,7 @@ import auth import dowloadClients import indexer import metadataProvider -from database import SessionDependency +from database import DbSessionDependency from database.torrents import Torrent from database.tv import Season, Show from indexer import IndexerQueryResult @@ -34,7 +34,7 @@ class ShowDetails(BaseModel): status.HTTP_201_CREATED: {"model": Show, "description": "Successfully created show"}, status.HTTP_409_CONFLICT: {"model": Message, "description": "Show already exists"}, }) -def add_show(db: SessionDependency, show_id: int, metadata_provider: str = "tmdb", version: str = ""): +def add_show(db: DbSessionDependency, show_id: int, metadata_provider: str = "tmdb", version: str = ""): res = db.exec(select(Show). where(Show.external_id == show_id). where(Show.metadata_provider == metadata_provider). @@ -59,14 +59,14 @@ def add_show(db: SessionDependency, show_id: int, metadata_provider: str = "tmdb @router.delete("/{show_id}", status_code=status.HTTP_200_OK) -def delete_show(db: SessionDependency, show_id: UUID): +def delete_show(db: DbSessionDependency, show_id: UUID): db.delete(db.get(Show, show_id)) db.commit() @router.patch("/{show_id}/{season_id}", status_code=status.HTTP_200_OK, dependencies=[Depends(auth.get_current_user)], response_model=Season) -def add_season(db: SessionDependency, season_id: UUID): +def add_season(db: DbSessionDependency, season_id: UUID): """ adds requested flag to a season """ @@ -81,7 +81,7 @@ def add_season(db: SessionDependency, season_id: UUID): @router.delete("/{show_id}/{season_id}", status_code=status.HTTP_200_OK, dependencies=[Depends(auth.get_current_user)], response_model=Show) -def delete_season(db: SessionDependency, show_id: UUID, season: int): +def delete_season(db: DbSessionDependency, show_id: UUID, season: int): """ removes requested flag from a season """ @@ -96,7 +96,7 @@ def delete_season(db: SessionDependency, show_id: UUID, season: int): @router.get("/{show_id}/{season_id}/torrent", status_code=status.HTTP_200_OK, dependencies=[Depends( auth.get_current_user)], response_model=list[IndexerQueryResult]) -def get_season_torrents(db: SessionDependency, show_id: UUID, season_id: UUID): +def get_season_torrents(db: DbSessionDependency, show_id: UUID, season_id: UUID): season = db.get(Season, season_id) if season is None: @@ -121,7 +121,7 @@ def get_season_torrents(db: SessionDependency, show_id: UUID, season_id: UUID): @router.post("/{show_id}/torrent", status_code=status.HTTP_200_OK, dependencies=[Depends( auth.get_current_user)], response_model=list[Season]) -def download_seasons_torrent(db: SessionDependency, show_id: UUID, torrent_id: UUID): +def download_seasons_torrent(db: DbSessionDependency, show_id: UUID, torrent_id: UUID): """ downloads torrents for a show season, links the torrent for all seasons the torrent contains @@ -153,7 +153,7 @@ def download_seasons_torrent(db: SessionDependency, show_id: UUID, torrent_id: U @router.post("/{show_id}/{season_id}/torrent", status_code=status.HTTP_200_OK, dependencies=[Depends( auth.get_current_user)], response_model=list[Season]) -def delete_seasons_torrent(db: SessionDependency, show_id: UUID, season_id: UUID, torrent_id: UUID): +def delete_seasons_torrent(db: DbSessionDependency, show_id: UUID, season_id: UUID, torrent_id: UUID): """ downloads torrents for a season, links the torrent only to the specified season this means that multiple torrents can contain a season but you can choose from one which the content should be @@ -186,13 +186,13 @@ def delete_seasons_torrent(db: SessionDependency, show_id: UUID, season_id: UUID @router.get("/", dependencies=[Depends(auth.get_current_user)], response_model=list[Show]) -def get_shows(db: SessionDependency): +def get_shows(db: DbSessionDependency): """""" return db.exec(select(Show)).unique().fetchall() @router.get("/{show_id}", dependencies=[Depends(auth.get_current_user)], response_model=ShowDetails) -def get_show(db: SessionDependency, show_id: UUID): +def get_show(db: DbSessionDependency, show_id: UUID): """ :param show_id: diff --git a/backend/src/users/routers.py b/backend/src/users/routers.py index 471d875..8046f3b 100644 --- a/backend/src/users/routers.py +++ b/backend/src/users/routers.py @@ -5,7 +5,7 @@ from starlette.responses import JSONResponse from auth import get_current_user from auth.password import get_password_hash -from database import SessionDependency +from database import DbSessionDependency from database.users import User, UserCreate, UserPublic from users import log @@ -24,7 +24,7 @@ class Message(BaseModel): 201: {"model": UserPublic, "description": "User created successfully"} }) async def create_user( - db: SessionDependency, + db: DbSessionDependency, user: UserCreate = Depends(UserCreate), ): internal_user = User(name=user.name, lastname=user.lastname, email=user.email,