Skip to content

Commit

Permalink
Merge pull request #67 from profcomff/comment_dump
Browse files Browse the repository at this point in the history
comments import endpoint
  • Loading branch information
Temmmmmo authored Dec 10, 2024
2 parents 626545f + dd56a93 commit 9404aba
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
"""
Expand Down
10 changes: 9 additions & 1 deletion rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9404aba

Please sign in to comment.