Skip to content
Merged
14 changes: 13 additions & 1 deletion rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen
- datetime.datetime.utcnow()
)

# Сначала добавляем с user_id, который мы получили при авторизации,
# в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии
Comment thread
parfenovma marked this conversation as resolved.
LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=user.get('id'))

# Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД
user_id = None if comment_info.is_anonymous else user.get('id')

new_comment = Comment.create(
session=db.session, **comment_info.model_dump(), lecturer_id=lecturer_id, review_status=ReviewStatus.PENDING
session=db.session,
**comment_info.model_dump(exclude={"is_anonymous"}),
lecturer_id=lecturer_id,
user_id=user_id,
review_status=ReviewStatus.PENDING,
)
return CommentGet.model_validate(new_comment)

Expand Down Expand Up @@ -122,9 +132,11 @@ async def review_comment(
`approved` - комментарий одобрен и возвращается при запросе лектора
`dismissed` - комментарий отклонен, не отображается в запросе лектора
"""

check_comment: Comment = Comment.query(session=db.session).filter(Comment.uuid == uuid).one_or_none()
if not check_comment:
raise ObjectNotFound(Comment, uuid)

Comment thread
DR0P-database marked this conversation as resolved.
return CommentGet.model_validate(Comment.update(session=db.session, id=uuid, review_status=review_status))


Expand Down
1 change: 1 addition & 0 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CommentPost(Base):
mark_kindness: int
mark_freebie: int
mark_clarity: int
is_anonymous: bool
Comment thread
parfenovma marked this conversation as resolved.

@field_validator('mark_kindness', 'mark_freebie', 'mark_clarity')
@classmethod
Expand Down
54 changes: 53 additions & 1 deletion tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from starlette import status

from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
from rating_api.models import Comment, LecturerUserComment, ReviewStatus
from rating_api.settings import get_settings


Expand All @@ -24,6 +24,7 @@
"mark_kindness": 1,
"mark_freebie": 0,
"mark_clarity": 0,
"is_anonymous": False,
},
0,
status.HTTP_200_OK,
Expand All @@ -35,6 +36,7 @@
"mark_kindness": -2,
"mark_freebie": -2,
"mark_clarity": -2,
"is_anonymous": False,
},
1,
status.HTTP_200_OK,
Expand All @@ -46,6 +48,7 @@
"mark_kindness": 5,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": False,
},
2,
status.HTTP_400_BAD_REQUEST,
Expand All @@ -57,10 +60,59 @@
"mark_kindness": 1,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": False,
},
3,
status.HTTP_404_NOT_FOUND,
),
( # Anonymous comment
{
"subject": "test_subject",
"text": "test_text",
"mark_kindness": 1,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": True,
},
0,
status.HTTP_200_OK,
),
( # NotAnonymous comment
{
"subject": "test_subject",
"text": "test_text",
"mark_kindness": 1,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": False,
},
0,
status.HTTP_200_OK,
),
( # Bad anonymity
{
"subject": "test_subject",
"text": "test_text",
"mark_kindness": 1,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": 'asd',
},
0,
status.HTTP_422_UNPROCESSABLE_ENTITY,
),
( # Not provided anonymity
{
"subject": "test_subject",
"text": "test_text",
"mark_kindness": 1,
"mark_freebie": -2,
"mark_clarity": 0,
"is_anonymous": 'asd',
},
0,
status.HTTP_422_UNPROCESSABLE_ENTITY,
),
],
)
def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response_status):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from starlette import status

from rating_api.models import Comment, Lecturer, ReviewStatus
from rating_api.models import Lecturer
from rating_api.settings import get_settings


Expand Down