diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 44d9cc4..2786db9 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -1,6 +1,6 @@ import datetime import re -from typing import Literal +from typing import Literal, Union from uuid import UUID import aiohttp @@ -19,7 +19,14 @@ ) from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus from rating_api.schemas.base import StatusResponseModel -from rating_api.schemas.models import CommentGet, CommentGetAll, CommentImportAll, CommentPost, CommentUpdate +from rating_api.schemas.models import ( + CommentGet, + CommentGetAll, + CommentGetAllWithStatus, + CommentGetWithStatus, + CommentImportAll, + CommentPost, +) from rating_api.settings import Settings, get_settings @@ -134,7 +141,7 @@ async def get_comment(uuid: UUID) -> CommentGet: return CommentGet.model_validate(comment) -@comment.get("", response_model=CommentGetAll) +@comment.get("", response_model=Union[CommentGetAll, CommentGetAllWithStatus]) async def get_comments( limit: int = 10, offset: int = 0, @@ -142,10 +149,10 @@ async def get_comments( user_id: int | None = None, order_by: list[Literal["create_ts"]] = Query(default=[]), unreviewed: bool = False, - user=Depends(UnionAuth(scopes=['rating.comment.review'], auto_error=False, allow_none=True)), + user=Depends(UnionAuth(scopes=["rating.comment.user"], auto_error=False, allow_none=True)), ) -> CommentGetAll: """ - Scopes: `["rating.comment.review"]` + Scopes: `["rating.comment.user"]` `limit` - максимальное количество возвращаемых комментариев @@ -164,7 +171,12 @@ async def get_comments( comments = Comment.query(session=db.session).all() if not comments: raise ObjectNotFound(Comment, 'all') - result = CommentGetAll(limit=limit, offset=offset, total=len(comments)) + if "rating.comment.user" in [scope['name'] for scope in user.get('session_scopes')] or user.get('id') == user_id: + result = CommentGetAllWithStatus(limit=limit, offset=offset, total=len(comments)) + comment_validator = CommentGetWithStatus + 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] @@ -175,8 +187,15 @@ async def get_comments( 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] + if ( + "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')] + or user.get('id') == user_id + ): + result.comments = [ + comment + for comment in result.comments + if comment.review_status is ReviewStatus.PENDING or ReviewStatus.DISMISSED + ] else: raise ForbiddenAction(Comment) else: @@ -187,7 +206,7 @@ async def get_comments( if "create_ts" in order_by: result.comments.sort(key=lambda comment: comment.create_ts, reverse=True) result.total = len(result.comments) - result.comments = [CommentGet.model_validate(comment) for comment in 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 diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index e4d8427..c591fdf 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -4,6 +4,7 @@ from pydantic import field_validator from rating_api.exceptions import WrongMark +from rating_api.models import ReviewStatus from rating_api.schemas.base import Base @@ -21,6 +22,21 @@ class CommentGet(Base): lecturer_id: int +class CommentGetWithStatus(Base): + uuid: UUID + user_id: int | None = None + create_ts: datetime.datetime + update_ts: datetime.datetime + subject: str | None = None + text: str + mark_kindness: int + mark_freebie: int + mark_clarity: int + mark_general: float + lecturer_id: int + review_status: ReviewStatus + + class CommentPost(Base): subject: str text: str @@ -83,6 +99,13 @@ class CommentGetAll(Base): total: int +class CommentGetAllWithStatus(Base): + comments: list[CommentGetWithStatus] = [] + limit: int + offset: int + total: int + + class LecturerUserCommentPost(Base): lecturer_id: int user_id: int