Skip to content
Snippets Groups Projects
Commit cce00e69 authored by Nimisha Asthagiri's avatar Nimisha Asthagiri
Browse files

Merge pull request #7310 from edx/mobile/MA-125

MA-125 Mobile Enrollment API: add certificate info.
parents 3c4252e7 9f0d6c00
No related merge requests found
......@@ -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'
......
"""
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):
"""
......
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment