Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize getlecture #53

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions rating_api/routes/lecturer.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Также выкатили пр на тесты, так что стоит написать тесты на:

  1. Запрос всех лекторов с комментами
  2. Запрос одного лектора с комментами
  3. Запрос всех лекторов с оценками
  4. Запрос одного лектора с оценками

Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,36 @@ async def get_lecturer(id: int, info: list[Literal["comments", "mark"]] = Query(
if lecturer is None:
raise ObjectNotFound(Lecturer, id)
result = LecturerGet.model_validate(lecturer)
result.comments = None
if lecturer.comments:
approved_comments: list[CommentGet] = [
CommentGet.model_validate(comment)
for comment in lecturer.comments
if comment.review_status is ReviewStatus.APPROVED
]
if "comments" in info and approved_comments:
result.comments = approved_comments
if "mark" in info and approved_comments:
result.mark_freebie = sum(comment.mark_freebie for comment in approved_comments) / len(approved_comments)
result.mark_kindness = sum(comment.mark_kindness for comment in approved_comments) / len(approved_comments)
result.mark_clarity = sum(comment.mark_clarity for comment in approved_comments) / len(approved_comments)
result.mark_general = sum(comment.mark_general for comment in approved_comments) / len(approved_comments)
if approved_comments:
result.subjects = list({comment.subject for comment in approved_comments})
result.comments = list()

subjects_set = set() # множество для предметов
if "mark" in info:
result.mark_freebie, result.mark_clarity, result.mark_kindness, result.mark_general = 0, 0, 0, 0
aproved_comment_count = 0
for comment in lecturer.comments:
if comment.review_status is ReviewStatus.APPROVED:
comment = CommentGet.model_validate(comment)
else:
continue
if "comments" in info:
result.comments.append(comment)
if "mark" in info:
aproved_comment_count += 1
result.mark_freebie += comment.mark_freebie
result.mark_clarity += comment.mark_clarity
result.mark_kindness += comment.mark_kindness
result.mark_general += comment.mark_general
if comment.subject is not None:
subjects_set.add(comment.subject)
if "mark" in info:
result.mark_clarity /= aproved_comment_count
result.mark_freebie /= aproved_comment_count
result.mark_kindness /= aproved_comment_count
result.mark_general /= aproved_comment_count
if len(result.comments) == 0:
result.comments = None
if len(subjects_set) != 0:
result.subjects = list(subjects_set)
return result


Expand Down Expand Up @@ -137,6 +151,7 @@ async def get_lecturers(
if "comments" in info and approved_comments:
lecturer_to_result.comments = approved_comments
if "mark" in info and approved_comments:

lecturer_to_result.mark_freebie = sum([comment.mark_freebie for comment in approved_comments]) / len(
approved_comments
)
Expand All @@ -149,6 +164,7 @@ async def get_lecturers(
lecturer_to_result.mark_general = sum(comment.mark_general for comment in approved_comments) / len(
approved_comments
)

if approved_comments:
lecturer_to_result.subjects = list({comment.subject for comment in approved_comments})
result.lecturers.append(lecturer_to_result)
Expand Down
Loading