From d12b0a5665dd3e8514cc02ef37e205e9f34c6c1d Mon Sep 17 00:00:00 2001 From: KyryloKireiev Date: Wed, 24 Apr 2024 10:48:35 +0300 Subject: [PATCH] refactor: [AXM-288] Refactor get_course_assignments method --- .../mobile_api/users/serializers.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lms/djangoapps/mobile_api/users/serializers.py b/lms/djangoapps/mobile_api/users/serializers.py index 4e89c339d3f8..82389199ef72 100644 --- a/lms/djangoapps/mobile_api/users/serializers.py +++ b/lms/djangoapps/mobile_api/users/serializers.py @@ -267,29 +267,28 @@ def get_course_assignments(self, model: CourseEnrollment) -> Dict[str, Optional[ self.context.get('request'), include_past_dates=True ) - next_assignment_all = [] - next_assignment = [] - past_assignment = [] + past_assignments = [] + future_assignments = [] timezone = get_user_timezone_or_last_seen_timezone_or_utc(model.user) for assignment in sorted(assignments, key=lambda x: x.date): if assignment.date < datetime.now(timezone): - past_assignment.append(assignment) + past_assignments.append(assignment) else: if not assignment.complete: - next_assignment_all.append(assignment) + future_assignments.append(assignment) -if future_assignments: - future_assignment_date = future_assignments[0].date.date() - next_assignments = [ - assignment for assignment in future_assignments if assignment.date.date() == future_assignment_date - ] - else: - next_assignments = [] + if future_assignments: + future_assignment_date = future_assignments[0].date.date() + next_assignments = [ + assignment for assignment in future_assignments if assignment.date.date() == future_assignment_date + ] + else: + next_assignments = [] return { - 'future_assignments': DateSummarySerializer(next_assignment, many=True).data, - 'past_assignments': DateSummarySerializer(past_assignment, many=True).data, + 'future_assignments': DateSummarySerializer(next_assignments, many=True).data, + 'past_assignments': DateSummarySerializer(past_assignments, many=True).data, } class Meta: