diff --git a/migrations/versions/0c117913717b_adding_is_deleted_fields.py b/migrations/versions/0c117913717b_adding_is_deleted_fields.py new file mode 100644 index 0000000..cf026a1 --- /dev/null +++ b/migrations/versions/0c117913717b_adding_is_deleted_fields.py @@ -0,0 +1,31 @@ +"""adding_is_deleted_fields + +Revision ID: 0c117913717b +Revises: 656228b2d6e0 +Create Date: 2024-10-24 06:59:27.285029 + +""" + +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = '0c117913717b' +down_revision = '656228b2d6e0' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false())) + op.add_column('lecturer', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false())) + op.add_column( + 'lecturer_user_comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false()) + ) + + +def downgrade(): + op.drop_column('lecturer_user_comment', 'is_deleted') + op.drop_column('lecturer', 'is_deleted') + op.drop_column('comment', 'is_deleted') diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 7851235..55d5429 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -33,7 +33,12 @@ class Lecturer(BaseDbModel): middle_name: Mapped[str] = mapped_column(String, nullable=False) avatar_link: Mapped[str] = mapped_column(String, nullable=True) timetable_id: Mapped[int] = mapped_column(Integer, unique=True, nullable=False) - comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer") + comments: Mapped[list[Comment]] = relationship( + "Comment", + back_populates="lecturer", + primaryjoin="and_(Lecturer.id == Comment.lecturer_id, not_(Comment.is_deleted))", + ) + is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) @hybrid_method def search(self, query: str) -> bool: @@ -56,14 +61,24 @@ class Comment(BaseDbModel): mark_kindness: Mapped[int] = mapped_column(Integer, nullable=False) mark_freebie: Mapped[int] = mapped_column(Integer, nullable=False) mark_clarity: Mapped[int] = mapped_column(Integer, nullable=False) - lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) + lecturer_id: Mapped[int] = mapped_column( + Integer, + ForeignKey("lecturer.id"), + primary_join="and_(Comment.lecturer_id==Lecturer.id, not_(Lecturer.is_deleted))", + ) lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments") review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) class LecturerUserComment(BaseDbModel): id: Mapped[int] = mapped_column(Integer, primary_key=True) - lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) + lecturer_id: Mapped[int] = mapped_column( + Integer, + ForeignKey("lecturer.id"), + primary_join="and_(LecturerUserComment.lecturer_id==Lecturer.id),not_(Lecturer.is_deleted))", + ) user_id: Mapped[int] = mapped_column(Integer, nullable=False) 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) + is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index c0cbcf4..4e1ca34 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -141,7 +141,6 @@ async def delete_comment( if check_comment is None: raise ObjectNotFound(Comment, uuid) Comment.delete(session=db.session, id=uuid) - return StatusResponseModel( status="Success", message="Comment has been deleted", ru="Комментарий удален из RatingAPI" ) diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index e933026..e56bf11 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -125,7 +125,9 @@ def test_delete_comment(client, dbsession): random_uuid = uuid.uuid4() response = client.delete(f'{url}/{random_uuid}') assert response.status_code == status.HTTP_404_NOT_FOUND - comment = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none() - assert comment is None + comment1 = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none() + assert comment1 == None + comment.is_deleted = True + dbsession.delete(comment) dbsession.delete(lecturer) dbsession.commit() diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index e98858a..542cd4d 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -107,7 +107,6 @@ def test_get_lecturer_with_comments(client, dbsession): dbsession.delete(lecturer) dbsession.commit() - def test_get_lecturers_by_name(client, dbsession): body_list = [ {"first_name": 'Алиса', "last_name": 'Селезнёва', "middle_name": 'Ивановна', "timetable_id": 0}, @@ -174,7 +173,8 @@ def test_update_lecturer(client, dbsession): assert json_response["first_name"] == 'Иван' assert json_response["last_name"] == 'Иванов' assert json_response["middle_name"] == "Иванович" - dbsession.delete(lecturer) + db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none() + dbsession.delete(db_lecturer) dbsession.commit() @@ -195,8 +195,11 @@ def test_delete_lecturer(client, dbsession): comment: Comment = Comment.create(session=dbsession, **comment) dbsession.commit() lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() - assert lecturer is not None + assert not lecturer is None response = client.delete(f"{url}/{lecturer.id}") assert response.status_code == status.HTTP_200_OK response = client.delete(f"{url}/{lecturer.id}") assert response.status_code == status.HTTP_404_NOT_FOUND + lecturer.is_deleted = True + dbsession.delete(lecturer) + dbsession.commit()