Skip to content

Commit

Permalink
feat: [AXM-288] Change response to represent Future assignments (#2550)
Browse files Browse the repository at this point in the history
* feat: [AXM-288] Change response to represent Future assignments the same way as past assignments

* refactor: [AXM-288] Refactor get_course_assignments

Co-authored-by: monteri <[email protected]>

* refactor: [AXM-288] Refactor get_course_assignments method

---------

Co-authored-by: monteri <[email protected]>
  • Loading branch information
KyryloKireiev and monteri committed Jun 21, 2024
1 parent 3a1ff13 commit 8944d1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lms/djangoapps/mobile_api/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -267,20 +267,28 @@ def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str,
self.context.get('request'),
include_past_dates=True
)
next_assignment = None
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:
next_assignment = DateSummarySerializer(assignment).data
break
if not assignment.complete:
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 = []

return {
'future_assignment': next_assignment,
'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:
Expand Down
15 changes: 15 additions & 0 deletions lms/djangoapps/mobile_api/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,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):
Expand Down

0 comments on commit 8944d1e

Please sign in to comment.