diff --git a/print_service/__main__.py b/print_service/__main__.py index c9aaab6..8f9a7e6 100644 --- a/print_service/__main__.py +++ b/print_service/__main__.py @@ -1,7 +1,5 @@ import uvicorn -from print_service.routes.base import app - if __name__ == '__main__': - uvicorn.run(app) + uvicorn.run("print_service.routes.base:app", reload=True) diff --git a/print_service/routes/file.py b/print_service/routes/file.py index 5205e0f..21e3570 100644 --- a/print_service/routes/file.py +++ b/print_service/routes/file.py @@ -4,6 +4,7 @@ import aiofiles import aiofiles.os +from auth_lib.fastapi import UnionAuth from fastapi import APIRouter, File, UploadFile from fastapi.exceptions import HTTPException from fastapi.params import Depends @@ -59,11 +60,13 @@ def validate_pages(cls, value: str): class SendInput(BaseModel): - surname: str = Field( + surname: str | None = Field( + default=None, description='Фамилия', example='Иванов', ) - number: str = Field( + number: str | None = Field( + default=None, description='Номер профсоюзного или студенческого билетов', example='1015000', ) @@ -107,7 +110,11 @@ class ReceiveOutput(BaseModel): }, response_model=SendOutput, ) -async def send(inp: SendInput, settings: Settings = Depends(get_settings)): +async def send( + inp: SendInput, + user_auth=Depends(UnionAuth(allow_none=True)), + settings: Settings = Depends(get_settings), +): """Получить пин код для загрузки и скачивания файла. Полученный пин-код можно использовать в методах POST и GET `/file/{pin}`. @@ -115,14 +122,23 @@ async def send(inp: SendInput, settings: Settings = Depends(get_settings)): user = db.session.query(UnionMember) if not settings.ALLOW_STUDENT_NUMBER: user = user.filter(UnionMember.union_number != None) - user = user.filter( - or_( - func.upper(UnionMember.student_number) == inp.number.upper(), - func.upper(UnionMember.union_number) == inp.number.upper(), - ), - func.upper(UnionMember.surname) == inp.surname.upper(), - ).one_or_none() - if not user: + + if (inp.number is not None) and (inp.surname is not None): + user = user.filter( + or_( + func.upper(UnionMember.student_number) == inp.number.upper(), + func.upper(UnionMember.union_number) == inp.number.upper(), + ), + func.upper(UnionMember.surname) == inp.surname.upper(), + ) + + else: + if not "print.file.send" in [scope["name"] for scope in user_auth.get('session_scopes')]: + raise NotInUnion() + + user = user.one_or_none() + + if user is None: raise NotInUnion() try: pin = generate_pin(db.session) diff --git a/print_service/routes/user.py b/print_service/routes/user.py index 7cd7743..34a3a3c 100644 --- a/print_service/routes/user.py +++ b/print_service/routes/user.py @@ -50,6 +50,7 @@ async def check_union_member( v: Optional[str] = __version__, ): """Проверяет наличие пользователя в списке.""" + surname = surname.upper() user = db.session.query(UnionMember) if not settings.ALLOW_STUDENT_NUMBER: