From f04695d174c9703b96a69e535851d4cc92b2039c Mon Sep 17 00:00:00 2001 From: KyryloKireiev Date: Fri, 19 Apr 2024 15:59:50 +0300 Subject: [PATCH 1/3] feat: [AXM-288] Change response to represent Future assignments the same way as past assignments --- lms/djangoapps/mobile_api/users/serializers.py | 17 ++++++++++++----- lms/djangoapps/mobile_api/users/tests.py | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/mobile_api/users/serializers.py b/lms/djangoapps/mobile_api/users/serializers.py index 76453dc4a6b4..b14719a7cb5c 100644 --- a/lms/djangoapps/mobile_api/users/serializers.py +++ b/lms/djangoapps/mobile_api/users/serializers.py @@ -257,7 +257,7 @@ def get_progress(self, model: CourseEnrollment) -> Dict[str, int]: """ return self.calculate_progress(model) - def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, List[Dict[str, str]]]]: + def get_course_assignments(self, model: CourseEnrollment) -> Dict[str, Optional[List[Dict[str, str]]]]: """ Returns the future assignment data and past assignments data for the user in the course. """ @@ -267,7 +267,8 @@ def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, self.context.get('request'), include_past_dates=True ) - next_assignment = None + next_assignment_all = [] + next_assignment = [] past_assignment = [] timezone = get_user_timezone_or_last_seen_timezone_or_utc(model.user) @@ -275,11 +276,17 @@ def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, if assignment.date < datetime.now(timezone): past_assignment.append(assignment) else: - next_assignment = DateSummarySerializer(assignment).data - break + if not assignment.complete: + next_assignment_all.append(assignment) + + if next_assignment_all: + future_assignment_date = next_assignment_all[0].date.date() + next_assignment = [ + assignment for assignment in next_assignment_all if assignment.date.date() == future_assignment_date + ] return { - 'future_assignment': next_assignment, + 'future_assignments': DateSummarySerializer(next_assignment, many=True).data, 'past_assignments': DateSummarySerializer(past_assignment, many=True).data, } diff --git a/lms/djangoapps/mobile_api/users/tests.py b/lms/djangoapps/mobile_api/users/tests.py index 31ff2a059ef9..0cd959570c50 100644 --- a/lms/djangoapps/mobile_api/users/tests.py +++ b/lms/djangoapps/mobile_api/users/tests.py @@ -903,6 +903,21 @@ def test_user_enrollment_api_v4_status_all(self): self.assertEqual(enrollments['results'][2]['course']['id'], str(old_course.id)) self.assertNotIn('primary', response.data) + @patch('lms.djangoapps.mobile_api.users.serializers.cache.set', return_value=None) + def test_response_contains_primary_enrollment_assignments_info(self, cache_mock: MagicMock): + self.login() + course = CourseFactory.create(org='edx', mobile_available=True) + self.enroll(course.id) + + response = self.api_response(api_version=API_V4) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIn('course_assignments', response.data['primary']) + self.assertIn('past_assignments', response.data['primary']['course_assignments']) + self.assertIn('future_assignments', response.data['primary']['course_assignments']) + self.assertListEqual(response.data['primary']['course_assignments']['past_assignments'], []) + self.assertListEqual(response.data['primary']['course_assignments']['future_assignments'], []) + @override_settings(MKTG_URLS={'ROOT': 'dummy-root'}) class TestUserEnrollmentCertificates(UrlResetMixin, MobileAPITestCase, MilestonesTestCaseMixin): From 282cb21213082a12e891d922832a96cec51a3566 Mon Sep 17 00:00:00 2001 From: Kyrylo Kireiev <90455454+KyryloKireiev@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:27:58 +0300 Subject: [PATCH 2/3] refactor: [AXM-288] Refactor get_course_assignments Co-authored-by: monteri <36768631+monteri@users.noreply.github.com> --- lms/djangoapps/mobile_api/users/serializers.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/mobile_api/users/serializers.py b/lms/djangoapps/mobile_api/users/serializers.py index b14719a7cb5c..4e89c339d3f8 100644 --- a/lms/djangoapps/mobile_api/users/serializers.py +++ b/lms/djangoapps/mobile_api/users/serializers.py @@ -279,11 +279,13 @@ def get_course_assignments(self, model: CourseEnrollment) -> Dict[str, Optional[ if not assignment.complete: next_assignment_all.append(assignment) - if next_assignment_all: - future_assignment_date = next_assignment_all[0].date.date() - next_assignment = [ - assignment for assignment in next_assignment_all if assignment.date.date() == future_assignment_date - ] +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, From 01e7625b87787774a714b0c2e4c4d80a8255d829 Mon Sep 17 00:00:00 2001 From: KyryloKireiev Date: Wed, 24 Apr 2024 10:48:35 +0300 Subject: [PATCH 3/3] 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: