Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 53 additions & 31 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ async def get_comments(
user_id: int | None = None,
order_by: list[Literal["create_ts"]] = Query(default=[]),
unreviewed: bool = False,
review_mode: Literal["all", "pending"] = "all", # вот ключ
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А чем отличается от unreviewed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не стал переписывать unreviewd т.к возможно какаято логика завязана. Новый возвращает только pending без DISSMISED и еще чемто отлтичается

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше тогда, если уж так начал сделать, сделать явно запрос по любому статусу DISMISSED, PENDING, REVIEW

Если они не проставлены, то возвращаются все

Но, чтобы можно было вернуть, чисто по статусу одному, нужны скоупы

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

короче очень костыльно, если твоим способом

лучше так, как я предложил, но нужно это будет согласовать с фронтом

user=Depends(UnionAuth(scopes=["rating.comment.review"], auto_error=False, allow_none=True)),
) -> CommentGetAll:
"""
Expand All @@ -194,43 +195,64 @@ async def get_comments(

`unreviewed` - вернет все непроверенные комментарии, если True. По дефолту False.
"""
comments = Comment.query(session=db.session).all()
if not comments:
raise ObjectNotFound(Comment, 'all')
if "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
result = CommentGetAllWithAllInfo(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGetWithAllInfo
elif user.get('id') == user_id:
result = CommentGetAllWithStatus(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGetWithStatus

if review_mode == "pending": # Для бота либо без проблем получить все Pending
query = Comment.query(session=db.session)
# пусто или нет
query = query.filter(
Comment.review_status == ReviewStatus.PENDING,
)

comments = query.all()
if not comments:
raise ObjectNotFound(Comment, 'all')

paginated = comments[offset : offset + limit]
paginated.sort(key=lambda c: c.create_ts, reverse=True)

return CommentGetAll(comments=paginated, limit=limit, offset=offset, total=len(comments))

else:
result = CommentGetAll(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGet
result.comments = comments
if user_id is not None:
result.comments = [comment for comment in result.comments if comment.user_id == user_id]

if lecturer_id is not None:
result.comments = [comment for comment in result.comments if comment.lecturer_id == lecturer_id]

if unreviewed:
if not user:
raise ForbiddenAction(Comment)
comments = Comment.query(session=db.session).all()
if not comments:
raise ObjectNotFound(Comment, 'all')

if "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
result.comments = [comment for comment in result.comments if comment.review_status is ReviewStatus.PENDING]
result = CommentGetAllWithAllInfo(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGetWithAllInfo
elif user.get('id') == user_id:
result = CommentGetAllWithStatus(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGetWithStatus
else:
raise ForbiddenAction(Comment)
else:
result.comments = [comment for comment in result.comments if comment.review_status is ReviewStatus.APPROVED]
result = CommentGetAll(limit=limit, offset=offset, total=len(comments))
comment_validator = CommentGet
result.comments = comments
if user_id is not None:
result.comments = [comment for comment in result.comments if comment.user_id == user_id]

if lecturer_id is not None:
result.comments = [comment for comment in result.comments if comment.lecturer_id == lecturer_id]

if unreviewed:
if not user:
raise ForbiddenAction(Comment)
if "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
result.comments = [
comment for comment in result.comments if comment.review_status is ReviewStatus.PENDING
]
else:
raise ForbiddenAction(Comment)
else:
result.comments = [comment for comment in result.comments if comment.review_status is ReviewStatus.APPROVED]

result.comments = result.comments[offset : limit + offset]
result.comments = result.comments[offset : limit + offset]

if "create_ts" in order_by:
if "create_ts" in order_by:
result.comments.sort(key=lambda comment: comment.create_ts, reverse=True)
result.total = len(result.comments)
result.comments = [comment_validator.model_validate(comment) for comment in result.comments]
result.comments.sort(key=lambda comment: comment.create_ts, reverse=True)
result.total = len(result.comments)
result.comments = [comment_validator.model_validate(comment) for comment in result.comments]
result.comments.sort(key=lambda comment: comment.create_ts, reverse=True)
return result
return result


@comment.patch("/{uuid}/review", response_model=CommentGetWithAllInfo)
Expand Down
Loading