Commit 3de6070b by Matthew Piatetsky Committed by GitHub

Merge pull request #446 from edx/ECOM-6377

ECOM-6377 Add fields to MinimalCourseRunSerializer
parents e53efe14 e018d3b7
......@@ -331,11 +331,15 @@ class MinimalCourseRunSerializer(TimestampModelSerializer):
@classmethod
def prefetch_queryset(cls):
return CourseRun.objects.all().select_related('course').prefetch_related('course__partner')
return CourseRun.objects.all().select_related('course').prefetch_related(
'course__partner',
Prefetch('seats', queryset=SeatSerializer.prefetch_queryset()),
)
class Meta:
model = CourseRun
fields = ('key', 'uuid', 'title', 'image', 'short_description', 'marketing_url',)
fields = ('key', 'uuid', 'title', 'image', 'short_description', 'marketing_url',
'start', 'end', 'enrollment_start', 'enrollment_end', 'pacing_type', 'type',)
def get_marketing_url(self, obj):
return get_marketing_url_for_user(
......@@ -364,15 +368,14 @@ class CourseRunSerializer(MinimalCourseRunSerializer):
queryset = super().prefetch_queryset()
return queryset.select_related('language', 'video').prefetch_related(
'transcript_languages',
Prefetch('seats', queryset=SeatSerializer.prefetch_queryset()),
Prefetch('staff', queryset=PersonSerializer.prefetch_queryset()),
)
class Meta(MinimalCourseRunSerializer.Meta):
fields = MinimalCourseRunSerializer.Meta.fields + (
'course', 'full_description', 'start', 'end', 'enrollment_start', 'enrollment_end', 'announcement',
'video', 'seats', 'content_language', 'transcript_languages', 'instructors', 'staff', 'pacing_type',
'min_effort', 'max_effort', 'modified', 'level_type', 'availability', 'mobile_available', 'hidden',
'course', 'full_description', 'announcement', 'video', 'seats', 'content_language',
'transcript_languages', 'instructors', 'staff', 'min_effort', 'max_effort', 'modified',
'level_type', 'availability', 'mobile_available', 'hidden',
)
def get_instructors(self, obj): # pylint: disable=unused-argument
......
......@@ -216,6 +216,12 @@ class MinimalCourseRunSerializerTests(TestCase):
'utm_medium': request.user.referral_tracking_id,
})
),
'start': json_date_format(course_run.start),
'end': json_date_format(course_run.end),
'enrollment_start': json_date_format(course_run.enrollment_start),
'enrollment_end': json_date_format(course_run.enrollment_end),
'pacing_type': course_run.pacing_type,
'type': course_run.type,
}
def test_data(self):
......@@ -236,13 +242,8 @@ class CourseRunSerializerTests(MinimalCourseRunSerializerTests):
'key': course_run.key,
'title': course_run.title, # pylint: disable=no-member
'full_description': course_run.full_description, # pylint: disable=no-member
'start': json_date_format(course_run.start),
'end': json_date_format(course_run.end),
'enrollment_start': json_date_format(course_run.enrollment_start),
'enrollment_end': json_date_format(course_run.enrollment_end),
'announcement': json_date_format(course_run.announcement),
'video': VideoSerializer(course_run.video).data,
'pacing_type': course_run.pacing_type,
'mobile_available': course_run.mobile_available,
'hidden': course_run.hidden,
'content_language': course_run.language.code,
......
......@@ -141,7 +141,7 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
CourseRunFactory(enrollment_end=enrollment_end, course__title='ABC Test Course 2')
CourseRunFactory(enrollment_end=enrollment_end, course=self.course)
with self.assertNumQueries(40):
with self.assertNumQueries(42):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertListEqual(response.data['results'], self.serialize_catalog_course(courses, many=True))
......
......@@ -60,7 +60,7 @@ class CourseRunViewSetTests(SerializationMixin, ElasticsearchTestMixin, APITestC
url = reverse('api:v1:course_run-detail', kwargs={'key': self.course_run.key})
url += '?include_deleted_programs=1'
with self.assertNumQueries(19):
with self.assertNumQueries(20):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
......@@ -89,7 +89,7 @@ class CourseRunViewSetTests(SerializationMixin, ElasticsearchTestMixin, APITestC
url = reverse('api:v1:course_run-detail', kwargs={'key': self.course_run.key})
url += '?include_unpublished_programs=1'
with self.assertNumQueries(19):
with self.assertNumQueries(20):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
......@@ -130,7 +130,7 @@ class CourseRunViewSetTests(SerializationMixin, ElasticsearchTestMixin, APITestC
query = 'title:Some random title'
url = '{root}?q={query}'.format(root=reverse('api:v1:course_run-list'), query=query)
with self.assertNumQueries(37):
with self.assertNumQueries(40):
response = self.client.get(url)
actual_sorted = sorted(response.data['results'], key=lambda course_run: course_run['key'])
......
......@@ -66,7 +66,7 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
def test_retrieve(self):
""" Verify the endpoint returns the details for a single program. """
program = self.create_program()
with self.assertNumQueries(75):
with self.assertNumQueries(76):
self.assert_retrieve_success(program)
@ddt.data(True, False)
......@@ -76,7 +76,7 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
for course in course_list:
CourseRunFactory(course=course)
program = ProgramFactory(courses=course_list, order_courses_by_start_date=order_courses_by_start_date)
with self.assertNumQueries(87):
with self.assertNumQueries(90):
self.assert_retrieve_success(program)
self.assertEqual(course_list, list(program.courses.all())) # pylint: disable=no-member
......@@ -113,7 +113,7 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns a list of all programs. """
expected = [self.create_program() for __ in range(3)]
expected.reverse()
self.assert_list_results(self.list_path, expected, 11)
self.assert_list_results(self.list_path, expected, 13)
def test_filter_by_type(self):
""" Verify that the endpoint filters programs to those of a given type. """
......@@ -157,7 +157,7 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns marketing URLs without UTM parameters. """
url = self.list_path + '?exclude_utm=1'
program = self.create_program()
self.assert_list_results(url, [program], 11, extra_context={'exclude_utm': 1})
self.assert_list_results(url, [program], 13, extra_context={'exclude_utm': 1})
def test_minimal_serializer_use(self):
""" Verify that the list view uses the minimal serializer. """
......
......@@ -442,7 +442,7 @@ class CourseRun(TimeStampedModel):
@property
def seat_types(self):
return list(self.seats.values_list('type', flat=True))
return [seat.type for seat in self.seats.all()]
@property
def type(self):
......
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