From 79c77229ca1abee078ab8edc1e65b3908bdb4ad3 Mon Sep 17 00:00:00 2001 From: Artem Morozov Date: Fri, 25 Oct 2024 00:11:14 +0300 Subject: [PATCH 1/4] Delete subject-field from lecturer --- rating_api/models/db.py | 1 - rating_api/routes/lecturer.py | 12 ++++++------ rating_api/schemas/models.py | 4 +--- 3 files changed, 7 insertions(+), 10 deletions(-) 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/lecturer.py b/rating_api/routes/lecturer.py index e370225..272f597 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -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 From 969162059115c94edb4388ad2bf59fcac62330ad Mon Sep 17 00:00:00 2001 From: Artem Morozov Date: Fri, 25 Oct 2024 00:13:09 +0300 Subject: [PATCH 2/4] Linting --- ...59e13277b6_delete_subject_from_lecturer.py | 23 +++++++++++++++++++ rating_api/routes/comment.py | 8 ++----- rating_api/routes/lecturer.py | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 migrations/versions/5659e13277b6_delete_subject_from_lecturer.py 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..e3d8eb5 --- /dev/null +++ b/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py @@ -0,0 +1,23 @@ +"""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/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 272f597..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 From 6f890853eaa91226bc81150b306be85099ae9324 Mon Sep 17 00:00:00 2001 From: Artem Morozov Date: Fri, 25 Oct 2024 00:19:03 +0300 Subject: [PATCH 3/4] Fix tests --- tests/test_routes/test_lecturer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 3d76c78da5440a558b1f8ae5f2708fdc0f0f94a1 Mon Sep 17 00:00:00 2001 From: Artem Morozov Date: Fri, 25 Oct 2024 00:19:37 +0300 Subject: [PATCH 4/4] Linting --- migrations/versions/5659e13277b6_delete_subject_from_lecturer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py b/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py index e3d8eb5..8348edd 100644 --- a/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py +++ b/migrations/versions/5659e13277b6_delete_subject_from_lecturer.py @@ -5,6 +5,7 @@ Create Date: 2024-10-24 23:55:41.835641 """ + import sqlalchemy as sa from alembic import op