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..924a186 --- /dev/null +++ b/migrations/versions/933db669e7ef_make_subject_in_comment_nullable.py @@ -0,0 +1,25 @@ +"""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..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 +from rating_api.schemas.models import CommentGet, CommentGetAll, CommentImportAll, CommentPost from rating_api.settings import Settings, get_settings @@ -65,6 +65,26 @@ 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: + """ + 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: + 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..675a1be 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 @@ -39,6 +39,14 @@ def validate_mark(cls, value): return value +class CommentImport(CommentPost): + lecturer_id: int + + +class CommentImportAll(Base): + comments: list[CommentImport] + + class CommentGetAll(Base): comments: list[CommentGet] = [] limit: int