From 74c95e66d895a4fd486c60eedba33918c6b2c971 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sat, 16 Nov 2024 13:55:20 +0300 Subject: [PATCH 01/10] Modify .gitignore for .vscode --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b6e4761..244427d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.vscode/ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From 1ebc428543d98cd2c9bc684f3cd4fc458a4c5b9f Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sat, 16 Nov 2024 13:58:02 +0300 Subject: [PATCH 02/10] Add user_id to Comment when created POST/Comment --- rating_api/routes/comment.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index c0cbcf4..7a39df6 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -42,7 +42,7 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=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(), lecturer_id=lecturer_id, user_id=user.get('id'), review_status=ReviewStatus.PENDING ) return CommentGet.model_validate(new_comment) @@ -112,7 +112,7 @@ async def get_comments( async def review_comment( uuid: UUID, review_status: Literal[ReviewStatus.APPROVED, ReviewStatus.DISMISSED] = ReviewStatus.DISMISSED, - _=Depends(UnionAuth(scopes=["rating.comment.review"], allow_none=False, auto_error=True)), + user=Depends(UnionAuth(scopes=["rating.comment.review"], allow_none=False, auto_error=True)), ) -> CommentGet: """ Scopes: `["rating.comment.review"]` @@ -122,9 +122,13 @@ 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) + + #if user.get('id') == Comment.user_id and 'rating.comment.selfupdate' in user.scopes: + return CommentGet.model_validate(Comment.update(session=db.session, id=uuid, review_status=review_status)) From 9fec39669ac9b7c813936b19818e54895a0a3992 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sat, 16 Nov 2024 14:57:39 +0300 Subject: [PATCH 03/10] Realized anonymity of a comment - Add atribute "is_anonymous" to schema Comment --- rating_api/routes/comment.py | 13 ++++++++++++- rating_api/schemas/models.py | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 7a39df6..94017d2 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -40,9 +40,20 @@ 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')) + + # Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД + if hasattr(comment_info, 'is_anonymous'): + user_id = None if comment_info.is_anonymous == True else user.get('id') + del comment_info.is_anonymous + else: + user_id=user.get('id') + new_comment = Comment.create( - session=db.session, **comment_info.model_dump(), lecturer_id=lecturer_id, user_id=user.get('id'), review_status=ReviewStatus.PENDING + session=db.session, **comment_info.model_dump(), lecturer_id=lecturer_id, + user_id=user_id, review_status=ReviewStatus.PENDING ) return CommentGet.model_validate(new_comment) 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 From cb9e9b9fa21acf51e1cc86cdaf573af304549429 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sat, 16 Nov 2024 15:17:36 +0300 Subject: [PATCH 04/10] Reformate code --- rating_api/routes/comment.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 94017d2..a0733ce 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -40,7 +40,7 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen - datetime.datetime.utcnow() ) - # Сначала добавляем с user_id, который мы получили при авторизации, + # Сначала добавляем с user_id, который мы получили при авторизации, # в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=user.get('id')) @@ -49,11 +49,14 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen user_id = None if comment_info.is_anonymous == True else user.get('id') del comment_info.is_anonymous else: - user_id=user.get('id') - + user_id = user.get('id') + new_comment = Comment.create( - session=db.session, **comment_info.model_dump(), lecturer_id=lecturer_id, - user_id=user_id, review_status=ReviewStatus.PENDING + session=db.session, + **comment_info.model_dump(), + lecturer_id=lecturer_id, + user_id=user_id, + review_status=ReviewStatus.PENDING, ) return CommentGet.model_validate(new_comment) @@ -137,8 +140,8 @@ async def review_comment( check_comment: Comment = Comment.query(session=db.session).filter(Comment.uuid == uuid).one_or_none() if not check_comment: raise ObjectNotFound(Comment, uuid) - - #if user.get('id') == Comment.user_id and 'rating.comment.selfupdate' in user.scopes: + + # if user.get('id') == Comment.user_id and 'rating.comment.selfupdate' in user.scopes: return CommentGet.model_validate(Comment.update(session=db.session, id=uuid, review_status=review_status)) From c1e1989a6fd583f240ebf0f004c98b01a8ac5356 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 20:30:36 +0300 Subject: [PATCH 05/10] Reverse .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 244427d..b6e4761 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -*.vscode/ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From 6525012e093819fdd5837a942729edf44e995d2d Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 20:42:11 +0300 Subject: [PATCH 06/10] Some fix_ups --- rating_api/routes/comment.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index a0733ce..fef7751 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -45,12 +45,9 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=user.get('id')) # Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД - if hasattr(comment_info, 'is_anonymous'): - user_id = None if comment_info.is_anonymous == True else user.get('id') - del comment_info.is_anonymous - else: - user_id = user.get('id') - + user_id = None if comment_info.is_anonymous else user.get('id') + del comment_info.is_anonymous + new_comment = Comment.create( session=db.session, **comment_info.model_dump(), @@ -126,7 +123,7 @@ async def get_comments( async def review_comment( uuid: UUID, review_status: Literal[ReviewStatus.APPROVED, ReviewStatus.DISMISSED] = ReviewStatus.DISMISSED, - user=Depends(UnionAuth(scopes=["rating.comment.review"], allow_none=False, auto_error=True)), + _=Depends(UnionAuth(scopes=["rating.comment.review"], allow_none=False, auto_error=True)), ) -> CommentGet: """ Scopes: `["rating.comment.review"]` @@ -141,8 +138,6 @@ async def review_comment( if not check_comment: raise ObjectNotFound(Comment, uuid) - # if user.get('id') == Comment.user_id and 'rating.comment.selfupdate' in user.scopes: - return CommentGet.model_validate(Comment.update(session=db.session, id=uuid, review_status=review_status)) From ed7366a5b607c76d6fe77eafe9bb9db4b9b634a5 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 20:46:05 +0300 Subject: [PATCH 07/10] Changed del to exclude --- rating_api/routes/comment.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index fef7751..cf5ce1f 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -46,11 +46,10 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen # Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД user_id = None if comment_info.is_anonymous else user.get('id') - del comment_info.is_anonymous new_comment = Comment.create( session=db.session, - **comment_info.model_dump(), + **comment_info.model_dump(exclude={"is_anonymous"}), lecturer_id=lecturer_id, user_id=user_id, review_status=ReviewStatus.PENDING, From 81f97309c8ee53f13b3c007c6749b4d31bfa4e5c Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 21:46:35 +0300 Subject: [PATCH 08/10] Fix and Add tests --- tests/test_routes/test_comment.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 44c3462..4121503 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -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): From 7305b604661b7127df52b0388e5d72d696b2ec10 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 21:49:43 +0300 Subject: [PATCH 09/10] Formate code --- rating_api/routes/comment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index cf5ce1f..d3b5c78 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -46,7 +46,7 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen # Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД user_id = None if comment_info.is_anonymous else user.get('id') - + new_comment = Comment.create( session=db.session, **comment_info.model_dump(exclude={"is_anonymous"}), From 0772eb41aaa7d2d78a752b08fb6978d9c1068791 Mon Sep 17 00:00:00 2001 From: DR0P-database Date: Sun, 17 Nov 2024 21:52:47 +0300 Subject: [PATCH 10/10] Formate code for tests --- tests/test_routes/test_comment.py | 18 +++++++++--------- tests/test_routes/test_lecturer.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 4121503..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,7 +24,7 @@ "mark_kindness": 1, "mark_freebie": 0, "mark_clarity": 0, - "is_anonymous": False + "is_anonymous": False, }, 0, status.HTTP_200_OK, @@ -36,7 +36,7 @@ "mark_kindness": -2, "mark_freebie": -2, "mark_clarity": -2, - "is_anonymous": False + "is_anonymous": False, }, 1, status.HTTP_200_OK, @@ -48,7 +48,7 @@ "mark_kindness": 5, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": False + "is_anonymous": False, }, 2, status.HTTP_400_BAD_REQUEST, @@ -60,7 +60,7 @@ "mark_kindness": 1, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": False + "is_anonymous": False, }, 3, status.HTTP_404_NOT_FOUND, @@ -72,7 +72,7 @@ "mark_kindness": 1, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": True + "is_anonymous": True, }, 0, status.HTTP_200_OK, @@ -84,7 +84,7 @@ "mark_kindness": 1, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": False + "is_anonymous": False, }, 0, status.HTTP_200_OK, @@ -96,7 +96,7 @@ "mark_kindness": 1, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": 'asd' + "is_anonymous": 'asd', }, 0, status.HTTP_422_UNPROCESSABLE_ENTITY, @@ -108,7 +108,7 @@ "mark_kindness": 1, "mark_freebie": -2, "mark_clarity": 0, - "is_anonymous": 'asd' + "is_anonymous": 'asd', }, 0, status.HTTP_422_UNPROCESSABLE_ENTITY, 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