Skip to content
Merged
Show file tree
Hide file tree
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
37 changes: 28 additions & 9 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import re
from typing import Literal
from typing import Literal, Union
from uuid import UUID

import aiohttp
Expand All @@ -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


Expand Down Expand Up @@ -134,18 +141,18 @@ async def get_comment(uuid: UUID) -> CommentGet:
return CommentGet.model_validate(comment)


@comment.get("", response_model=CommentGetAll)
@comment.get("", response_model=Union[CommentGetAll, CommentGetAllWithStatus])
Comment thread
Temmmmmo marked this conversation as resolved.
async def get_comments(
limit: int = 10,
offset: int = 0,
lecturer_id: int | None = None,
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` - максимальное количество возвращаемых комментариев

Expand All @@ -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]
Expand All @@ -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:
Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down