Commit e018d3b7 by Matthew Piatetsky

Add fields to MinimalCourseRunSerializer

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