Skip to content

Commit

Permalink
Вернул check_union_member и изменил file_send
Browse files Browse the repository at this point in the history
В POST запросе check_union_member вернул код в состояние до первого комита.
Для функции send написал одну дополнительную функцию, чтоб проверять наличие скоупа через Depends, добавил опциональный ввод поля number и проверку его наличия уже в самой функции send
  • Loading branch information
BombinBM committed Nov 15, 2024
1 parent 7e393cb commit 0806500
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
35 changes: 25 additions & 10 deletions print_service/routes/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,7 +64,8 @@ class SendInput(BaseModel):
description='Фамилия',
example='Иванов',
)
number: str = Field(
number: str | None = Field(
default=None,
description='Номер профсоюзного или студенческого билетов',
example='1015000',
)
Expand Down Expand Up @@ -96,7 +98,8 @@ class ReceiveOutput(BaseModel):


# endregion

def has_send_scope(union_auth: UnionAuth = Depends(UnionAuth(scopes=["print.file.send"], allow_none=True))):
return union_auth is not None

# region handlers
@router.post(
Expand All @@ -107,21 +110,33 @@ class ReceiveOutput(BaseModel):
},
response_model=SendOutput,
)
async def send(inp: SendInput, settings: Settings = Depends(get_settings)):
async def send(inp: SendInput,
has_send_scope: bool = Depends(has_send_scope),
settings: Settings = Depends(get_settings),
):
"""Получить пин код для загрузки и скачивания файла.
Полученный пин-код можно использовать в методах POST и GET `/file/{pin}`.
"""
if not has_send_scope and inp.number is None:
raise HTTPException(status_code=400, detail="Поле number обязательно для пользователей без скоупа print.file.send")


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 inp.number 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(),
).one_or_none()
else:
user = user.filter(
func.upper(UnionMember.surname) == inp.surname.upper(),
)
if not user:
raise NotInUnion()
try:
Expand Down
50 changes: 22 additions & 28 deletions print_service/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,34 @@ class UpdateUserList(BaseModel):
async def check_union_member(
surname: constr(strip_whitespace=True, to_upper=True, min_length=1),
number: Optional[str] = constr(strip_whitespace=True, to_upper=True, min_length=1),
# scope: scope = Depends(UnionAuth(scopes=["print.file.send"], allow_none=True)),
user = Depends(UnionAuth(scopes=["print.file.send"], allow_none=True)),
v: Optional[str] = __version__,
):
"""Проверяет наличие пользователя в списке."""
if "print.file.send" in [scope["name"] for scope in user.get('session_scopes')]:

surname = surname.upper()
user = db.session.query(UnionMember)
if not settings.ALLOW_STUDENT_NUMBER:
user = user.filter(UnionMember.union_number != None)
user: UnionMember = user.filter(
or_(
func.upper(UnionMember.student_number) == number,
func.upper(UnionMember.union_number) == number,
),
func.upper(UnionMember.surname) == surname,
).one_or_none()

if v == '1':
return bool(user)

if not user:
raise UserNotFound()
else:
return {
'surname': user.surname,
'number': number,
'student_number': user.student_number,
'union_number': user.union_number,
}
else:
surname = surname.upper()
user = db.session.query(UnionMember)
if not settings.ALLOW_STUDENT_NUMBER:
user = user.filter(UnionMember.union_number != None)
user: UnionMember = user.filter(
or_(
func.upper(UnionMember.student_number) == number,
func.upper(UnionMember.union_number) == number,
),
func.upper(UnionMember.surname) == surname,
).one_or_none()

if v == '1':
return bool(user)

if not user:
raise UserNotFound()
else:
return {
'surname': user.surname,
'number': number,
'student_number': user.student_number,
'union_number': user.union_number,
}


@router.post('/is_union_member')
Expand Down

0 comments on commit 0806500

Please sign in to comment.