diff --git a/lms/djangoapps/mobile_api/users/serializers.py b/lms/djangoapps/mobile_api/users/serializers.py index 267378f674..e57a95e978 100644 --- a/lms/djangoapps/mobile_api/users/serializers.py +++ b/lms/djangoapps/mobile_api/users/serializers.py @@ -6,6 +6,7 @@ from rest_framework.reverse import reverse from courseware.courses import course_image_url from student.models import CourseEnrollment, User +from certificates.models import certificate_status_for_student, CertificateStatuses class CourseField(serializers.RelatedField): @@ -64,10 +65,21 @@ class CourseEnrollmentSerializer(serializers.ModelSerializer): Serializes CourseEnrollment models """ course = CourseField() + certificate = serializers.SerializerMethodField('get_certificate') + + def get_certificate(self, model): + """Returns the information about the user's certificate in the course.""" + certificate_info = certificate_status_for_student(model.user, model.course_id) + if certificate_info['status'] == CertificateStatuses.downloadable: + return { + "url": certificate_info['download_url'], + } + else: + return {} class Meta: # pylint: disable=missing-docstring model = CourseEnrollment - fields = ('created', 'mode', 'is_active', 'course') + fields = ('created', 'mode', 'is_active', 'course', 'certificate') lookup_field = 'username' diff --git a/lms/djangoapps/mobile_api/users/tests.py b/lms/djangoapps/mobile_api/users/tests.py index 7281dc66c8..86fa8fcbb4 100644 --- a/lms/djangoapps/mobile_api/users/tests.py +++ b/lms/djangoapps/mobile_api/users/tests.py @@ -1,13 +1,14 @@ """ Tests for users API """ - import datetime from django.utils import timezone from xmodule.modulestore.tests.factories import ItemFactory, CourseFactory from xmodule.modulestore.django import modulestore from student.models import CourseEnrollment +from certificates.models import CertificateStatuses +from certificates.tests.factories import GeneratedCertificateFactory from .. import errors from ..testutils import MobileAPITestCase, MobileAuthTestMixin, MobileAuthUserTestMixin, MobileEnrolledCourseAccessTestMixin @@ -83,6 +84,29 @@ class TestUserEnrollmentApi(MobileAPITestCase, MobileAuthUserTestMixin, MobileEn unicode(courses[num_courses - course_num - 1].id) ) + def test_no_certificate(self): + self.login_and_enroll() + + response = self.api_response() + certificate_data = response.data[0]['certificate'] # pylint: disable=no-member + self.assertDictEqual(certificate_data, {}) + + def test_certificate(self): + self.login_and_enroll() + + certificate_url = "http://test_certificate_url" + GeneratedCertificateFactory.create( + user=self.user, + course_id=self.course.id, + status=CertificateStatuses.downloadable, + mode='verified', + download_url=certificate_url, + ) + + response = self.api_response() + certificate_data = response.data[0]['certificate'] # pylint: disable=no-member + self.assertEquals(certificate_data['url'], certificate_url) + class CourseStatusAPITestCase(MobileAPITestCase): """ diff --git a/lms/djangoapps/mobile_api/users/views.py b/lms/djangoapps/mobile_api/users/views.py index 763988e93e..54f0089038 100644 --- a/lms/djangoapps/mobile_api/users/views.py +++ b/lms/djangoapps/mobile_api/users/views.py @@ -210,6 +210,8 @@ class UserCourseEnrollmentsList(generics.ListAPIView): * mode: The type of certificate registration for this course: honor or certified. * is_active: Whether the course is currently active; true or false. + * certificate: Information about the user's earned certificate in the course. + * url: URL to the downloadable version of the certificate, if exists. * course: A collection of data about the course: * course_about: The URI to get the data for the course About page.