diff --git a/migrations/versions/d322e8331f91_add_user_full_name_to_comment_table.py b/migrations/versions/d322e8331f91_add_user_full_name_to_comment_table.py new file mode 100644 index 0000000..d7b089e --- /dev/null +++ b/migrations/versions/d322e8331f91_add_user_full_name_to_comment_table.py @@ -0,0 +1,29 @@ +"""Add user_full_name to Comment table + +Revision ID: d322e8331f91 +Revises: beb11fd89531 +Create Date: 2025-11-18 23:16:20.029675 + +""" + +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = 'd322e8331f91' +down_revision = 'beb11fd89531' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('comment', sa.Column('user_fullname', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('comment', 'user_fullname') + # ### end Alembic commands ### diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 4724411..6929947 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -135,6 +135,7 @@ def order_by_name( class Comment(BaseDbModel): uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4) user_id: Mapped[int] = mapped_column(Integer, nullable=True) + user_fullname: Mapped[str | None] = mapped_column(String, 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=True) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 9011277..2d5579e 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -102,11 +102,18 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen # Обрабатываем анонимность комментария, и удаляем этот флаг чтобы добавить запись в БД user_id = None if comment_info.is_anonymous else user.get('id') + fullname = None + if not comment_info.is_anonymous: + userdata_info = user.get("userdata", []) + fullname_info = list(filter(lambda x: "Полное имя" == x['param'], userdata_info)) + fullname = fullname_info[0]["value"] if len(fullname_info) != 0 else None + new_comment = Comment.create( session=db.session, **comment_info.model_dump(exclude={"is_anonymous"}), lecturer_id=lecturer_id, user_id=user_id, + user_fullname=fullname, review_status=ReviewStatus.PENDING, ) diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index ad1b0a0..b99fede 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -25,6 +25,7 @@ class CommentGet(Base): lecturer_id: int like_count: int dislike_count: int + user_fullname: str | None = None class CommentGetWithStatus(CommentGet):