Files
edx-platform/lms/djangoapps/bulk_enroll/serializers.py
Feanil Patel 9cf2f9f298 Run 2to3 -f future . -w
This will remove imports from __future__ that are no longer needed.

https://docs.python.org/3.5/library/2to3.html#2to3fixer-future
2019-12-30 10:35:30 -05:00

71 lines
2.4 KiB
Python

"""
Serializers for Bulk Enrollment.
"""
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from rest_framework import serializers
from six.moves import zip
from openedx.core.djangoapps.course_groups.cohorts import is_cohort_exists
class StringListField(serializers.ListField):
def to_internal_value(self, data):
if not data:
return []
if isinstance(data, list):
data = data[0]
return data.split(',')
class BulkEnrollmentSerializer(serializers.Serializer):
"""Serializes enrollment information for a collection of students/emails.
This is mainly useful for implementing validation when performing bulk enrollment operations.
"""
identifiers = serializers.CharField(required=True)
courses = StringListField(required=True)
cohorts = StringListField(required=False)
action = serializers.ChoiceField(
choices=(
('enroll', 'enroll'),
('unenroll', 'unenroll')
),
required=True
)
auto_enroll = serializers.BooleanField(default=False)
email_students = serializers.BooleanField(default=False)
def validate_courses(self, value):
"""
Check that each course key in list is valid.
"""
course_keys = value
for course in course_keys:
try:
CourseKey.from_string(course)
except InvalidKeyError:
raise serializers.ValidationError(u"Course key not valid: {}".format(course))
return value
def validate(self, attrs):
"""
Check that the cohorts list is the same size as the courses list.
"""
if attrs.get('cohorts'):
if attrs['action'] != 'enroll':
raise serializers.ValidationError("Cohorts can only be used for enrollments.")
if len(attrs['cohorts']) != len(attrs['courses']):
raise serializers.ValidationError(
"If provided, the cohorts and courses should have equal number of items.")
for course_id, cohort_name in zip(attrs['courses'], attrs['cohorts']):
if not is_cohort_exists(course_key=CourseKey.from_string(course_id), name=cohort_name):
raise serializers.ValidationError(u"cohort {cohort_name} not found in course {course_id}.".format(
cohort_name=cohort_name, course_id=course_id)
)
return attrs