diff --git a/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py b/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py new file mode 100644 index 0000000..8348edd --- /dev/null +++ b/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py @@ -0,0 +1,24 @@ +"""delete-subject-from-lecturer + +Revision ID: 5659e13277b6 +Revises: 656228b2d6e0 +Create Date: 2024-10-24 23:55:41.835641 + +""" + +import sqlalchemy as sa +from alembic import op + + +revision = '5659e13277b6' +down_revision = '656228b2d6e0' +branch_labels = None +depends_on = None + + +def upgrade(): + op.drop_column('lecturer', 'subject') + + +def downgrade(): + op.add_column('lecturer', sa.Column('subject', sa.VARCHAR(), autoincrement=False, nullable=True)) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index c39495d..21ad8d5 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -30,7 +30,6 @@ class Lecturer(BaseDbModel): first_name: Mapped[str] = mapped_column(String, nullable=False) last_name: Mapped[str] = mapped_column(String, nullable=False) middle_name: Mapped[str] = mapped_column(String, nullable=False) - subject: Mapped[str] = mapped_column(String, nullable=True) 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") diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 237b64b..f1821e6 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -6,14 +6,10 @@ from fastapi import APIRouter, Depends, Query from fastapi_sqlalchemy import db -from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus 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, CommentPost from rating_api.settings import Settings, get_settings diff --git a/rating_api/routes/lecturer.py b/rating_api/routes/lecturer.py index e370225..3a10e38 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -5,8 +5,8 @@ from fastapi_sqlalchemy import db from sqlalchemy import and_ -from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus from rating_api.exceptions import AlreadyExists, ObjectNotFound +from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus from rating_api.schemas.base import StatusResponseModel from rating_api.schemas.models import CommentGet, LecturerGet, LecturerGetAll, LecturerPatch, LecturerPost @@ -69,8 +69,8 @@ async def get_lecturer( result.mark_clarity = sum(comment.mark_clarity for comment in approved_comments) / len(approved_comments) general_marks = [result.mark_freebie, result.mark_kindness, result.mark_clarity] result.mark_general = sum(general_marks) / len(general_marks) - if not result.subject and approved_comments: - result.subject = approved_comments[-1].subject + if approved_comments: + result.subjects = list({comment.subject for comment in approved_comments}) return result @@ -98,7 +98,7 @@ async def get_lecturers( Если передано `'mark'`, то возвращаются общие средние оценки, а также суммарная средняя оценка по всем одобренным комментариям. `subject` - Если передано `subject` - возвращает всех преподавателей, для которых переданное значение совпадает с их предметом преподавания. + Если передано `subject` - возвращает всех преподавателей, для которых переданное значение совпадает с одним из их предметов преподавания. Также возвращает всех преподавателей, у которых есть комментарий с совпадающим с данным subject. """ lecturers = Lecturer.query(session=db.session).all() @@ -132,13 +132,13 @@ async def get_lecturers( lecturer_to_result.mark_clarity, ] lecturer_to_result.mark_general = sum(general_marks) / len(general_marks) - if not lecturer_to_result.subject and approved_comments: - lecturer_to_result.subject = approved_comments[-1].subject + if approved_comments: + lecturer_to_result.subjects = list({comment.subject for comment in approved_comments}) result.lecturers.append(lecturer_to_result) if "general" in order_by: result.lecturers.sort(key=lambda item: (item.mark_general is None, item.mark_general)) if subject: - result.lecturers = [lecturer for lecturer in result.lecturers if lecturer.subject == subject] + result.lecturers = [lecturer for lecturer in result.lecturers if subject in lecturer.subjects] result.total = len(result.lecturers) return result diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 90b8f0b..35d5ae2 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -42,7 +42,7 @@ class LecturerGet(Base): last_name: str middle_name: str avatar_link: str | None = None - subject: str | None = None + subjects: list[str] | None = None timetable_id: int mark_kindness: float | None = None mark_freebie: float | None = None @@ -62,7 +62,6 @@ class LecturerPost(Base): first_name: str last_name: str middle_name: str - subject: str | None = None avatar_link: str | None = None timetable_id: int @@ -71,6 +70,5 @@ class LecturerPatch(Base): first_name: str | None = None last_name: str | None = None middle_name: str | None = None - subject: str | None = None avatar_link: str | None = None timetable_id: int | None = None diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index 85146b8..81fefd8 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -99,7 +99,7 @@ def test_get_lecturer_with_comments(client, dbsession): assert json_response["mark_freebie"] == 0.5 assert json_response["mark_clarity"] == 0.5 assert json_response["mark_general"] == 0.5 - assert json_response["subject"] == "Физика" + assert "Физика" in json_response["subjects"] assert len(json_response["comments"]) != 0