From f40af61a1a67c155b9bf8b5754db1e01aaaf9a6f Mon Sep 17 00:00:00 2001 From: Artem Bratyashin Date: Tue, 11 Nov 2025 23:26:38 +0300 Subject: [PATCH] Added user full name for comments --- rating_api/routes/comment.py | 13 +++++++++++++ rating_api/schemas/models.py | 1 + tests/test_routes/test_comment.py | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 9011277..771c960 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -246,6 +246,19 @@ async def get_comments( result.total = len(result.comments) result.comments = [comment_validator.model_validate(comment) for comment in result.comments] + # Получаем ФИО пользователя + comments_with_usernames = [] + for comment in result.comments: + if user and comment.user_id == user.get("id"): + userdata_info = user.get("userdata", []) + full_name_info = list(filter(lambda x: x.get('param') == "Полное имя", userdata_info)) + comment.user_full_name = full_name_info[0].get("value") if full_name_info else None + else: + comment.user_full_name = None + comments_with_usernames.append(comment) + + result.comments = comments_with_usernames + return result diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index ad1b0a0..1084abc 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -25,6 +25,7 @@ class CommentGet(Base): lecturer_id: int like_count: int dislike_count: int + user_full_name: str | None = None class CommentGetWithStatus(CommentGet): diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 5f62bba..2513162 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -455,6 +455,33 @@ def test_update_comment(client, dbsession, nonanonymous_comment, body, response_ assert getattr(nonanonymous_comment, k, None) == v # Есть ли изменения в БД +@pytest.mark.parametrize("is_own_comment", [True, False]) +def test_user_full_name_field(client, dbsession, lecturers, user, is_own_comment): + """Проверяет, что user_full_name заполняется только для комментариев текущего пользователя""" + lecturer = lecturers[0] + comment_data = { + "user_id": user.get("id") if is_own_comment else 9999, + "lecturer_id": lecturer.id, + "subject": "Test subject", + "text": "Test text", + "mark_kindness": 1, + "mark_freebie": 0, + "mark_clarity": 0, + "review_status": ReviewStatus.APPROVED, + } + comment = Comment(**comment_data) + dbsession.add(comment) + dbsession.commit() + + response = client.get(f_url(comment.uuid)) + + assert response.status_code == status.HTTP_200_OK + if is_own_comment: + assert response.json()["user_full_name"] is not None + else: + assert response.json()["user_full_name"] is None + + # TODO: переписать под новую логику # def test_delete_comment(client, dbsession, comment): # response = client.delete(f'{url}/{comment.uuid}')