From 75e45bc2ee328bdd947939553a553919480705fa Mon Sep 17 00:00:00 2001 From: stephensanchez Date: Mon, 17 Nov 2014 15:00:50 +0000 Subject: [PATCH] Update the API and Data Layer to get the student from the models. --- common/djangoapps/enrollment/api.py | 21 +++++-------------- common/djangoapps/enrollment/serializers.py | 7 ++++++- .../djangoapps/enrollment/tests/test_data.py | 1 + 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/common/djangoapps/enrollment/api.py b/common/djangoapps/enrollment/api.py index 76ab99e1ab..e5511989bc 100644 --- a/common/djangoapps/enrollment/api.py +++ b/common/djangoapps/enrollment/api.py @@ -103,10 +103,7 @@ def get_enrollments(student_id): ] """ - enrollments = _data_api().get_course_enrollments(student_id) - for enrollment in enrollments: - enrollment['student'] = student_id - return enrollments + return _data_api().get_course_enrollments(student_id) def get_enrollment(student_id, course_id): @@ -148,9 +145,7 @@ def get_enrollment(student_id, course_id): } """ - enrollment = _data_api().get_course_enrollment(student_id, course_id) - enrollment['student'] = student_id - return enrollment + return _data_api().get_course_enrollment(student_id, course_id) def add_enrollment(student_id, course_id, mode='honor', is_active=True): @@ -196,9 +191,7 @@ def add_enrollment(student_id, course_id, mode='honor', is_active=True): } """ _validate_course_mode(course_id, mode) - enrollment = _data_api().update_course_enrollment(student_id, course_id, mode=mode, is_active=is_active) - enrollment['student'] = student_id - return enrollment + return _data_api().update_course_enrollment(student_id, course_id, mode=mode, is_active=is_active) def deactivate_enrollment(student_id, course_id): @@ -246,9 +239,7 @@ def deactivate_enrollment(student_id, course_id): u"No enrollment was found for student {student} in course {course}" .format(student=student_id, course=course_id) ) - enrollment = _data_api().update_course_enrollment(student_id, course_id, is_active=False) - enrollment['student'] = student_id - return enrollment + return _data_api().update_course_enrollment(student_id, course_id, is_active=False) def update_enrollment(student_id, course_id, mode): @@ -292,9 +283,7 @@ def update_enrollment(student_id, course_id, mode): """ _validate_course_mode(course_id, mode) - enrollment = _data_api().update_course_enrollment(student_id, course_id, mode) - enrollment['student'] = student_id - return enrollment + return _data_api().update_course_enrollment(student_id, course_id, mode) def get_course_enrollment_details(course_id): diff --git a/common/djangoapps/enrollment/serializers.py b/common/djangoapps/enrollment/serializers.py index 5306f228cd..7ec8b6bc16 100644 --- a/common/djangoapps/enrollment/serializers.py +++ b/common/djangoapps/enrollment/serializers.py @@ -54,10 +54,15 @@ class CourseEnrollmentSerializer(serializers.ModelSerializer): """ course = CourseField() + student = serializers.SerializerMethodField('get_username') + + def get_username(self, model): + """Retrieves the username from the associated model.""" + return model.username class Meta: # pylint: disable=C0111 model = CourseEnrollment - fields = ('created', 'mode', 'is_active', 'course') + fields = ('created', 'mode', 'is_active', 'course', 'student') lookup_field = 'username' diff --git a/common/djangoapps/enrollment/tests/test_data.py b/common/djangoapps/enrollment/tests/test_data.py index f069f68bcc..e39b3ea74e 100644 --- a/common/djangoapps/enrollment/tests/test_data.py +++ b/common/djangoapps/enrollment/tests/test_data.py @@ -156,6 +156,7 @@ class EnrollmentDataTest(ModuleStoreTestCase): ) # Get the enrollment and compare it to the original. result = data.get_course_enrollment(self.user.username, unicode(self.course.id)) + self.assertEqual(self.user.username, result['student']) self.assertEqual(enrollment, result) @raises(NonExistentCourseError)