Skip to content

Commit

Permalink
Merge branch 'main' into fix-total-lecturer-num
Browse files Browse the repository at this point in the history
  • Loading branch information
DaymasS authored Nov 10, 2024
2 parents 279e6e5 + c3378e8 commit c158f44
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
24 changes: 24 additions & 0 deletions migrations/versions/7388a2c219d2_add_user_id_to_lecturer_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""add-user-id-to-lecturer-user
Revision ID: 7388a2c219d2
Revises: fee34ac4fcab
Create Date: 2024-11-10 04:07:42.997861
"""

import sqlalchemy as sa
from alembic import op


revision = '7388a2c219d2'
down_revision = 'fee34ac4fcab'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('lecturer_user_comment', sa.Column('user_id', sa.Integer(), nullable=False))


def downgrade():
op.drop_column('lecturer_user_comment', 'user_id')
31 changes: 31 additions & 0 deletions migrations/versions/edcc1a448ffb_soft_deletes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""soft-deletes
Revision ID: edcc1a448ffb
Revises: 7388a2c219d2
Create Date: 2024-11-10 09:23:32.307828
"""

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
revision = 'edcc1a448ffb'
down_revision = '7388a2c219d2'
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')
12 changes: 10 additions & 2 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from enum import Enum

from sqlalchemy import UUID, DateTime
from sqlalchemy import UUID, Boolean, DateTime
from sqlalchemy import Enum as DbEnum
from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true
from sqlalchemy.ext.hybrid import hybrid_method
Expand Down Expand Up @@ -34,6 +34,7 @@ class Lecturer(BaseDbModel):
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")
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

@hybrid_method
def search_by_name(self, query: str) -> bool:
Expand Down Expand Up @@ -65,12 +66,19 @@ class Comment(BaseDbModel):
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: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments")
lecturer: Mapped[Lecturer] = relationship(
"Lecturer",
back_populates="comments",
primaryjoin="and_(Comment.lecturer_id == Lecturer.id, not_(Lecturer.is_deleted))",
)
review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)


class LecturerUserComment(BaseDbModel):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(Integer, nullable=False)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
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, nullable=False, default=False)
4 changes: 2 additions & 2 deletions rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ async def get_lecturers(
`name`
Поле для ФИО. Если передано `name` - возвращает всех преподователей, для которых нашлись совпадения с переданной строкой
"""
lecturers_query = Lecturer.query(session=db.session)
lecturers_query = (
lecturers_query.outerjoin(Lecturer.comments)
Lecturer.query(session=db.session)
.outerjoin(Lecturer.comments)
.group_by(Lecturer.id)
.filter(Lecturer.search_by_subject(subject))
.filter(Lecturer.search_by_name(name))
Expand Down
8 changes: 6 additions & 2 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ def test_delete_comment(client, dbsession):
dbsession.commit()
response = client.delete(f'{url}/{comment.uuid}')
assert response.status_code == status.HTTP_200_OK
response = client.get(f'{url}/{comment.uuid}')
assert response.status_code == status.HTTP_404_NOT_FOUND
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 is None
comment.is_deleted = True
dbsession.delete(comment)
dbsession.delete(lecturer)
dbsession.commit()
5 changes: 5 additions & 0 deletions tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,8 @@ def test_delete_lecturer(client, dbsession):
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
comment.is_deleted = True
dbsession.delete(comment)
dbsession.delete(lecturer)
dbsession.commit()

0 comments on commit c158f44

Please sign in to comment.