diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 392675a..2a6d5aa 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -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", # вот ключ user=Depends(UnionAuth(scopes=["rating.comment.review"], auto_error=False, allow_none=True)), ) -> CommentGetAll: """ @@ -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)