Commit 7c7e2f72 by Clinton Blackburn

Serializer updates

- Nesting course runs in courses
- Exposing key field for organizations and people
- Exposing course field for course runs

ECOM-3765
parent c7d0af60
...@@ -2,7 +2,7 @@ from django.utils.translation import ugettext_lazy as _ ...@@ -2,7 +2,7 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from course_discovery.apps.catalogs.models import Catalog from course_discovery.apps.catalogs.models import Catalog
from course_discovery.apps.course_metadata.models import( from course_discovery.apps.course_metadata.models import (
Course, CourseRun, Image, Organization, Person, Prerequisite, Seat, Subject, Video Course, CourseRun, Image, Organization, Person, Prerequisite, Seat, Subject, Video
) )
...@@ -15,7 +15,7 @@ class NamedModelSerializer(serializers.ModelSerializer): ...@@ -15,7 +15,7 @@ class NamedModelSerializer(serializers.ModelSerializer):
name = serializers.CharField() name = serializers.CharField()
class Meta(object): class Meta(object):
fields = ('name', ) fields = ('name',)
class SubjectSerializer(NamedModelSerializer): class SubjectSerializer(NamedModelSerializer):
...@@ -47,7 +47,7 @@ class VideoSerializer(MediaSerializer): ...@@ -47,7 +47,7 @@ class VideoSerializer(MediaSerializer):
class Meta(object): class Meta(object):
model = Video model = Video
fields = ('src', 'description', 'image', ) fields = ('src', 'description', 'image',)
class SeatSerializer(serializers.ModelSerializer): class SeatSerializer(serializers.ModelSerializer):
...@@ -65,7 +65,7 @@ class SeatSerializer(serializers.ModelSerializer): ...@@ -65,7 +65,7 @@ class SeatSerializer(serializers.ModelSerializer):
class Meta(object): class Meta(object):
model = Seat model = Seat
fields = ('type', 'price', 'currency', 'upgrade_deadline', 'credit_provider', 'credit_hours', ) fields = ('type', 'price', 'currency', 'upgrade_deadline', 'credit_provider', 'credit_hours',)
class PersonSerializer(serializers.ModelSerializer): class PersonSerializer(serializers.ModelSerializer):
...@@ -73,7 +73,7 @@ class PersonSerializer(serializers.ModelSerializer): ...@@ -73,7 +73,7 @@ class PersonSerializer(serializers.ModelSerializer):
class Meta(object): class Meta(object):
model = Person model = Person
fields = ('name', 'title', 'bio', 'profile_image',) fields = ('key', 'name', 'title', 'bio', 'profile_image',)
class OrganizationSerializer(serializers.ModelSerializer): class OrganizationSerializer(serializers.ModelSerializer):
...@@ -81,7 +81,7 @@ class OrganizationSerializer(serializers.ModelSerializer): ...@@ -81,7 +81,7 @@ class OrganizationSerializer(serializers.ModelSerializer):
class Meta(object): class Meta(object):
model = Organization model = Organization
fields = ('name', 'description', 'logo_image', 'homepage_url', ) fields = ('key', 'name', 'description', 'logo_image', 'homepage_url',)
class CatalogSerializer(serializers.ModelSerializer): class CatalogSerializer(serializers.ModelSerializer):
...@@ -90,26 +90,8 @@ class CatalogSerializer(serializers.ModelSerializer): ...@@ -90,26 +90,8 @@ class CatalogSerializer(serializers.ModelSerializer):
fields = ('id', 'name', 'query', 'courses_count',) fields = ('id', 'name', 'query', 'courses_count',)
class CourseSerializer(TimestampModelSerializer):
level_type = serializers.SlugRelatedField(read_only=True, slug_field='name')
subjects = SubjectSerializer(many=True)
prerequisites = PrerequisiteSerializer(many=True)
expected_learning_items = serializers.SlugRelatedField(many=True, read_only=True, slug_field='value')
image = ImageSerializer()
video = VideoSerializer()
owners = OrganizationSerializer(many=True)
sponsors = OrganizationSerializer(many=True)
class Meta(object):
model = Course
fields = (
'key', 'title', 'short_description', 'full_description', 'level_type', 'subjects',
'prerequisites', 'expected_learning_items', 'image', 'video', 'owners', 'sponsors',
'modified',
)
class CourseRunSerializer(TimestampModelSerializer): class CourseRunSerializer(TimestampModelSerializer):
course = serializers.SlugRelatedField(read_only=True, slug_field='key')
content_language = serializers.SlugRelatedField(read_only=True, slug_field='code', source='language') content_language = serializers.SlugRelatedField(read_only=True, slug_field='code', source='language')
transcript_languages = serializers.SlugRelatedField(many=True, read_only=True, slug_field='code') transcript_languages = serializers.SlugRelatedField(many=True, read_only=True, slug_field='code')
image = ImageSerializer() image = ImageSerializer()
...@@ -121,13 +103,32 @@ class CourseRunSerializer(TimestampModelSerializer): ...@@ -121,13 +103,32 @@ class CourseRunSerializer(TimestampModelSerializer):
class Meta(object): class Meta(object):
model = CourseRun model = CourseRun
fields = ( fields = (
'key', 'title', 'short_description', 'full_description', 'start', 'end', 'course', 'key', 'title', 'short_description', 'full_description', 'start', 'end',
'enrollment_start', 'enrollment_end', 'announcement', 'image', 'video', 'seats', 'enrollment_start', 'enrollment_end', 'announcement', 'image', 'video', 'seats',
'content_language', 'transcript_languages', 'instructors', 'staff', 'content_language', 'transcript_languages', 'instructors', 'staff',
'pacing_type', 'min_effort', 'max_effort', 'modified', 'pacing_type', 'min_effort', 'max_effort', 'modified',
) )
class CourseSerializer(TimestampModelSerializer):
level_type = serializers.SlugRelatedField(read_only=True, slug_field='name')
subjects = SubjectSerializer(many=True)
prerequisites = PrerequisiteSerializer(many=True)
expected_learning_items = serializers.SlugRelatedField(many=True, read_only=True, slug_field='value')
image = ImageSerializer()
video = VideoSerializer()
owners = OrganizationSerializer(many=True)
sponsors = OrganizationSerializer(many=True)
course_runs = CourseRunSerializer(many=True)
class Meta(object):
model = Course
fields = (
'key', 'title', 'short_description', 'full_description', 'level_type', 'subjects', 'prerequisites',
'expected_learning_items', 'image', 'video', 'owners', 'sponsors', 'modified', 'course_runs',
)
class ContainedCoursesSerializer(serializers.Serializer): # pylint: disable=abstract-method class ContainedCoursesSerializer(serializers.Serializer): # pylint: disable=abstract-method
courses = serializers.DictField( courses = serializers.DictField(
child=serializers.BooleanField(), child=serializers.BooleanField(),
......
...@@ -39,10 +39,8 @@ class CourseSerializerTests(TestCase): ...@@ -39,10 +39,8 @@ class CourseSerializerTests(TestCase):
course = CourseFactory() course = CourseFactory()
image = course.image image = course.image
video = course.video video = course.video
CourseRunFactory.create_batch(3, course=course)
serializer = CourseSerializer(course) serializer = CourseSerializer(course)
# path = reverse('api:v1:course-detail', kwargs={'key': course.key})
# request = RequestFactory().get(path)
# serializer = CourseSerializer(course, context={'request': request})
expected = { expected = {
'key': course.key, 'key': course.key,
...@@ -57,7 +55,8 @@ class CourseSerializerTests(TestCase): ...@@ -57,7 +55,8 @@ class CourseSerializerTests(TestCase):
'video': VideoSerializer(video).data, 'video': VideoSerializer(video).data,
'owners': [], 'owners': [],
'sponsors': [], 'sponsors': [],
'modified': json_date_format(course.modified) # pylint: disable=no-member 'modified': json_date_format(course.modified), # pylint: disable=no-member
'course_runs': CourseRunSerializer(course.course_runs, many=True).data
} }
self.assertDictEqual(serializer.data, expected) self.assertDictEqual(serializer.data, expected)
...@@ -71,6 +70,7 @@ class CourseRunSerializerTests(TestCase): ...@@ -71,6 +70,7 @@ class CourseRunSerializerTests(TestCase):
serializer = CourseRunSerializer(course_run) serializer = CourseRunSerializer(course_run)
expected = { expected = {
'course': course_run.course.key,
'key': course_run.key, 'key': course_run.key,
'title': course_run.title, # pylint: disable=no-member 'title': course_run.title, # pylint: disable=no-member
'short_description': course_run.short_description, # pylint: disable=no-member 'short_description': course_run.short_description, # pylint: disable=no-member
...@@ -163,6 +163,7 @@ class OrganizationSerializerTests(TestCase): ...@@ -163,6 +163,7 @@ class OrganizationSerializerTests(TestCase):
serializer = OrganizationSerializer(organization) serializer = OrganizationSerializer(organization)
expected = { expected = {
'key': organization.key,
'name': organization.name, 'name': organization.name,
'description': organization.description, 'description': organization.description,
'homepage_url': organization.homepage_url, 'homepage_url': organization.homepage_url,
...@@ -202,6 +203,7 @@ class PersonSerializerTests(TestCase): ...@@ -202,6 +203,7 @@ class PersonSerializerTests(TestCase):
serializer = PersonSerializer(person) serializer = PersonSerializer(person)
expected = { expected = {
'key': person.key,
'name': person.name, 'name': person.name,
'title': person.title, 'title': person.title,
'bio': person.bio, 'bio': person.bio,
......
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