From e83a76f41d8a1f770a6c4defefc3c5dd86184aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2=20=D0=98=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Mon, 16 Dec 2024 11:06:31 +0000 Subject: [PATCH 1/2] fixing lecturer post route + fixing validation models --- rating_api/routes/comment.py | 35 +++++++++++++---------------------- rating_api/schemas/models.py | 14 +++++++++++++- tests/conftest.py | 2 +- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index b228216..8640344 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -20,33 +20,24 @@ @comment.post("", response_model=CommentGet) async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depends(UnionAuth())) -> CommentGet: """ - Scopes: `["rating.comment.import"]` Создает комментарий к преподавателю в базе данных RatingAPI Для создания комментария нужно быть авторизованным - - Для возможности создания комментария с указанием времени создания и изменения необходим скоуп ["rating.comment.import"] """ lecturer = Lecturer.get(session=db.session, id=lecturer_id) if not lecturer: raise ObjectNotFound(Lecturer, lecturer_id) - - has_create_scope = "rating.comment.import" in [scope['name'] for scope in user.get('session_scopes')] - if (comment_info.create_ts or comment_info.update_ts) and not has_create_scope: - raise ForbiddenAction(Comment) - - if not has_create_scope: - user_comments: list[LecturerUserComment] = ( - LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() - ) - for user_comment in user_comments: - if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( - minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES - ): - raise TooManyCommentRequests( - dtime=user_comment.update_ts - + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) - - datetime.datetime.utcnow() - ) + user_comments: list[LecturerUserComment] = ( + LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() + ) + for user_comment in user_comments: + if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( + minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES + ): + raise TooManyCommentRequests( + dtime=user_comment.update_ts + + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) + - datetime.datetime.utcnow() + ) # Сначала добавляем с user_id, который мы получили при авторизации, # в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии @@ -78,7 +69,7 @@ async def import_comments( for comment_info in comments_info.comments: new_comment = Comment.create( session=db.session, - **comment_info.model_dump(exclude={"is_anonymous"}), + **comment_info.model_dump(), review_status=ReviewStatus.APPROVED, ) result.comments.append(new_comment) diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 4cfa074..d3b9486 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -39,10 +39,22 @@ def validate_mark(cls, value): return value -class CommentImport(CommentPost): +class CommentImport(Base): lecturer_id: int subject: str | None = None + text: str + create_ts: datetime.datetime | None = None + update_ts: datetime.datetime | None = None + mark_kindness: int + mark_freebie: int + mark_clarity: int + @field_validator('mark_kindness', 'mark_freebie', 'mark_clarity') + @classmethod + def validate_mark(cls, value): + if value not in [-2, -1, 0, 1, 2]: + raise WrongMark() + return value class CommentImportAll(Base): comments: list[CommentImport] diff --git a/tests/conftest.py b/tests/conftest.py index f5d7f16..1b82f4f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -97,7 +97,7 @@ def lecturers(dbsession): for fname, lname, mname, timetable_id in lecturers_data ] lecturers.append( - Lecturer(first_name='test_fname3', last_name='test_lname3', middle_name='test_mname3', timetable_id=3) + Lecturer(first_name='test_fname3', last_name='test_lname3', middle_name='test_mname3', timetable_id=9903) ) lecturers[-1].is_deleted = True for lecturer in lecturers: From 4f51da18f2e8980b344f18c1cc542f04946ba8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2=20=D0=98=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Mon, 16 Dec 2024 11:08:40 +0000 Subject: [PATCH 2/2] lint --- rating_api/schemas/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index d3b9486..5b012f0 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -56,6 +56,7 @@ def validate_mark(cls, value): raise WrongMark() return value + class CommentImportAll(Base): comments: list[CommentImport]