Skip to content

Commit

Permalink
Merge pull request #25 from profcomff/1-бэк-рейтинга-преподов
Browse files Browse the repository at this point in the history
Удаление поля subject из Lecturer.
  • Loading branch information
Temmmmmo authored Oct 24, 2024
2 parents 5979594 + 3d76c78 commit ca54781
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
24 changes: 24 additions & 0 deletions migrations/versions/5659e13277b6_delete_subject_from_lecturer.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 0 additions & 1 deletion rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 2 additions & 6 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
14 changes: 7 additions & 7 deletions rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -98,7 +98,7 @@ async def get_lecturers(
Если передано `'mark'`, то возвращаются общие средние оценки, а также суммарная средняя оценка по всем одобренным комментариям.
`subject`
Если передано `subject` - возвращает всех преподавателей, для которых переданное значение совпадает с их предметом преподавания.
Если передано `subject` - возвращает всех преподавателей, для которых переданное значение совпадает с одним из их предметов преподавания.
Также возвращает всех преподавателей, у которых есть комментарий с совпадающим с данным subject.
"""
lecturers = Lecturer.query(session=db.session).all()
Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit ca54781

Please sign in to comment.