diff --git a/rating_api/routes/lecturer.py b/rating_api/routes/lecturer.py index 1bdd563..d069acb 100644 --- a/rating_api/routes/lecturer.py +++ b/rating_api/routes/lecturer.py @@ -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 @@ -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 ) @@ -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)