diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index c0cbcf4..d3b5c78 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -40,9 +40,19 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen - datetime.datetime.utcnow() ) + # Сначала добавляем с user_id, который мы получили при авторизации, + # в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии 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) @@ -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) + return CommentGet.model_validate(Comment.update(session=db.session, id=uuid, review_status=review_status)) diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index de510a8..70fc495 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -26,6 +26,7 @@ class CommentPost(Base): mark_kindness: int mark_freebie: int mark_clarity: int + is_anonymous: bool @field_validator('mark_kindness', 'mark_freebie', 'mark_clarity') @classmethod diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 44c3462..a02f646 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -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 @@ -24,6 +24,7 @@ "mark_kindness": 1, "mark_freebie": 0, "mark_clarity": 0, + "is_anonymous": False, }, 0, status.HTTP_200_OK, @@ -35,6 +36,7 @@ "mark_kindness": -2, "mark_freebie": -2, "mark_clarity": -2, + "is_anonymous": False, }, 1, status.HTTP_200_OK, @@ -46,6 +48,7 @@ "mark_kindness": 5, "mark_freebie": -2, "mark_clarity": 0, + "is_anonymous": False, }, 2, status.HTTP_400_BAD_REQUEST, @@ -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): diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index 81b90cb..3bfb325 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -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