From eda6da347e15f97dbe6add31167cd1f1275d4c15 Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:35:11 +0300 Subject: [PATCH 1/4] comments import endpoint --- ...69e7ef_make_subject_in_comment_nullable.py | 28 +++++++++++++++++++ rating_api/models/db.py | 2 +- rating_api/routes/comment.py | 12 +++++++- rating_api/schemas/models.py | 6 +++- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py diff --git a/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py b/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py new file mode 100644 index 0000000..f6ae063 --- /dev/null +++ b/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py @@ -0,0 +1,28 @@ +"""make subject in comment nullable + +Revision ID: 933db669e7ef +Revises: 20181e0d6aab +Create Date: 2024-12-07 18:57:13.280516 + +""" +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = '933db669e7ef' +down_revision = '20181e0d6aab' +branch_labels = None +depends_on = None + + +def upgrade(): + op.alter_column('comment', 'subject', + existing_type=sa.VARCHAR(), + nullable=True) + + +def downgrade(): + op.alter_column('comment', 'subject', + existing_type=sa.VARCHAR(), + nullable=False) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index b36f783..b95ff10 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -79,7 +79,7 @@ class Comment(BaseDbModel): user_id: Mapped[int] = mapped_column(Integer, nullable=True) create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) - subject: Mapped[str] = mapped_column(String, nullable=False) + subject: Mapped[str] = mapped_column(String, nullable=True) text: Mapped[str] = mapped_column(String, nullable=True) mark_kindness: Mapped[int] = mapped_column(Integer, nullable=False) mark_freebie: Mapped[int] = mapped_column(Integer, nullable=False) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 1962433..737ee5d 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -9,7 +9,7 @@ from rating_api.exceptions import ForbiddenAction, ObjectNotFound, TooManyCommentRequests from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus from rating_api.schemas.base import StatusResponseModel -from rating_api.schemas.models import CommentGet, CommentGetAll, CommentPost +from rating_api.schemas.models import CommentGet, CommentGetAll, CommentPost, CommentImportAll from rating_api.settings import Settings, get_settings @@ -64,6 +64,16 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen ) return CommentGet.model_validate(new_comment) +@comment.post('/import', response_model=CommentGetAll) +async def import_comments(comments_info: CommentImportAll, _=Depends(UnionAuth(scopes=["rating.comment.import"]))) -> CommentGetAll: + number_of_comments = len(comments_info.comments) + result = CommentGetAll(limit=number_of_comments, offset=number_of_comments, total=number_of_comments) + for comment_info in comments_info.comments: + new_comment = Comment.create(session=db.session, + **comment_info.model_dump(exclude={"is_anonymous"}), + review_status=ReviewStatus.APPROVED,) + result.comments.append(new_comment) + return result @comment.get("/{uuid}", response_model=CommentGet) async def get_comment(uuid: UUID) -> CommentGet: diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 6cd1ac5..5b08c3d 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -22,7 +22,7 @@ class CommentGet(Base): class CommentPost(Base): - subject: str + subject: str | None text: str create_ts: datetime.datetime | None = None update_ts: datetime.datetime | None = None @@ -37,7 +37,11 @@ def validate_mark(cls, value): if value not in [-2, -1, 0, 1, 2]: raise WrongMark() return value +class CommentImport(CommentPost): + lecturer_id: int +class CommentImportAll(Base): + comments: list[CommentImport] class CommentGetAll(Base): comments: list[CommentGet] = [] From 20418c32154a8385c8550388942f949c27e2edb4 Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:38:38 +0300 Subject: [PATCH 2/4] adding comments --- rating_api/routes/comment.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 737ee5d..ca6fb83 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -65,7 +65,12 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen return CommentGet.model_validate(new_comment) @comment.post('/import', response_model=CommentGetAll) -async def import_comments(comments_info: CommentImportAll, _=Depends(UnionAuth(scopes=["rating.comment.import"]))) -> CommentGetAll: +async def import_comments(comments_info: CommentImportAll, + _=Depends(UnionAuth(scopes=["rating.comment.import"]))) -> CommentGetAll: + """ + Scopes: `["rating.comment.import"]` + Создает комментарии в базе данных RatingAPI + """ number_of_comments = len(comments_info.comments) result = CommentGetAll(limit=number_of_comments, offset=number_of_comments, total=number_of_comments) for comment_info in comments_info.comments: From b274c653bb7ca4bb2ce7e9f86237c6674e6a0eac Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:44:45 +0300 Subject: [PATCH 3/4] lint --- ...3db669e7ef_make_subject_in_comment_nullable.py | 9 +++------ rating_api/routes/comment.py | 15 ++++++++++----- rating_api/schemas/models.py | 4 ++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py b/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py index f6ae063..924a186 100644 --- a/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py +++ b/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py @@ -5,6 +5,7 @@ Create Date: 2024-12-07 18:57:13.280516 """ + import sqlalchemy as sa from alembic import op @@ -17,12 +18,8 @@ def upgrade(): - op.alter_column('comment', 'subject', - existing_type=sa.VARCHAR(), - nullable=True) + op.alter_column('comment', 'subject', existing_type=sa.VARCHAR(), nullable=True) def downgrade(): - op.alter_column('comment', 'subject', - existing_type=sa.VARCHAR(), - nullable=False) + op.alter_column('comment', 'subject', existing_type=sa.VARCHAR(), nullable=False) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index ca6fb83..f2c08dd 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -64,9 +64,11 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen ) return CommentGet.model_validate(new_comment) + @comment.post('/import', response_model=CommentGetAll) -async def import_comments(comments_info: CommentImportAll, - _=Depends(UnionAuth(scopes=["rating.comment.import"]))) -> CommentGetAll: +async def import_comments( + comments_info: CommentImportAll, _=Depends(UnionAuth(scopes=["rating.comment.import"])) +) -> CommentGetAll: """ Scopes: `["rating.comment.import"]` Создает комментарии в базе данных RatingAPI @@ -74,12 +76,15 @@ async def import_comments(comments_info: CommentImportAll, number_of_comments = len(comments_info.comments) result = CommentGetAll(limit=number_of_comments, offset=number_of_comments, total=number_of_comments) for comment_info in comments_info.comments: - new_comment = Comment.create(session=db.session, - **comment_info.model_dump(exclude={"is_anonymous"}), - review_status=ReviewStatus.APPROVED,) + new_comment = Comment.create( + session=db.session, + **comment_info.model_dump(exclude={"is_anonymous"}), + review_status=ReviewStatus.APPROVED, + ) result.comments.append(new_comment) return result + @comment.get("/{uuid}", response_model=CommentGet) async def get_comment(uuid: UUID) -> CommentGet: """ diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 5b08c3d..675a1be 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -37,12 +37,16 @@ def validate_mark(cls, value): if value not in [-2, -1, 0, 1, 2]: raise WrongMark() return value + + class CommentImport(CommentPost): lecturer_id: int + class CommentImportAll(Base): comments: list[CommentImport] + class CommentGetAll(Base): comments: list[CommentGet] = [] limit: int From dd56a93d62a3558974d8fc42c69c953d1fb0ba9e Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:47:07 +0300 Subject: [PATCH 4/4] lint --- 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 f2c08dd..b228216 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -9,7 +9,7 @@ from rating_api.exceptions import ForbiddenAction, ObjectNotFound, TooManyCommentRequests from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus from rating_api.schemas.base import StatusResponseModel -from rating_api.schemas.models import CommentGet, CommentGetAll, CommentPost, CommentImportAll +from rating_api.schemas.models import CommentGet, CommentGetAll, CommentImportAll, CommentPost from rating_api.settings import Settings, get_settings