Commit 1f9ead5d by zubair-arbi

Merge pull request #7547 from edx/zub/bugfix/ecom-1273-enrollment-api-catch-NoneType-courses

log and ignore invlaid/deleted courses in Enrollment API
parents ec2f4dc2 a9b13f0e
......@@ -2,11 +2,16 @@
Serializers for all Course Enrollment related return objects.
"""
import logging
from rest_framework import serializers
from student.models import CourseEnrollment
from course_modes.models import CourseMode
log = logging.getLogger(__name__)
class StringListField(serializers.CharField):
"""Custom Serializer for turning a comma delimited string into a list.
......@@ -56,7 +61,28 @@ class CourseEnrollmentSerializer(serializers.ModelSerializer):
course_details = serializers.SerializerMethodField('get_course_details')
user = serializers.SerializerMethodField('get_username')
@property
def data(self):
serialized_data = super(CourseEnrollmentSerializer, self).data
# filter the results with empty courses 'course_details'
if isinstance(serialized_data, dict):
if serialized_data.get('course_details') is None:
return None
return serialized_data
return [enrollment for enrollment in serialized_data if enrollment.get('course_details')]
def get_course_details(self, model):
if model.course is None:
msg = u"Course '{0}' does not exist (maybe deleted), in which User (user_id: '{1}') is enrolled.".format(
model.course_id,
model.user.id
)
log.warning(msg)
return None
field = CourseField()
return field.to_native(model.course)
......
......@@ -127,6 +127,19 @@ class EnrollmentDataTest(ModuleStoreTestCase):
results = data.get_course_enrollments(self.user.username)
self.assertEqual(results, created_enrollments)
# Now create a course enrollment with some invalid course (does
# not exist in database) for the user and check that the method
# 'get_course_enrollments' ignores course enrollments for invalid
# or deleted courses
CourseEnrollment.objects.create(
user=self.user,
course_id='InvalidOrg/InvalidCourse/InvalidRun',
mode='honor',
is_active=True
)
updated_results = data.get_course_enrollments(self.user.username)
self.assertEqual(results, updated_results)
@ddt.data(
# Default (no course modes in the database)
# Expect that users are automatically enrolled as "honor".
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment