Commit 55d6f543 by Marko Jevtić Committed by GitHub

Merge pull request #157 from edx/mjevtic/SOL-1934

[SOL-1934] Optimize course catalog course_runs/contains endpoint
parents f575170b 3d599189
......@@ -20,6 +20,7 @@ class CourseRunViewSetTests(ElasticsearchTestMixin, APITestCase):
self.user = UserFactory(is_staff=True, is_superuser=True)
self.client.force_authenticate(self.user)
self.course_run = CourseRunFactory()
self.course_run_2 = CourseRunFactory()
self.refresh_index()
self.request = APIRequestFactory().get('/')
self.request.user = self.user
......@@ -67,7 +68,7 @@ class CourseRunViewSetTests(ElasticsearchTestMixin, APITestCase):
)
self.assertListEqual(actual_sorted, expected_sorted)
def test_contains(self):
def test_contains_single_course_run(self):
qs = urllib.parse.urlencode({
'query': 'id:course*',
'course_run_ids': self.course_run.key
......@@ -85,6 +86,26 @@ class CourseRunViewSetTests(ElasticsearchTestMixin, APITestCase):
}
)
def test_contains_multiple_course_runs(self):
qs = urllib.parse.urlencode({
'query': 'id:course*',
'course_run_ids': '{},{},{}'.format(self.course_run.key, self.course_run_2.key, 'abc')
})
url = '{}?{}'.format(reverse('api:v1:course_run-contains'), qs)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(
response.data,
{
'course_runs': {
self.course_run.key: True,
self.course_run_2.key: True,
'abc': False
}
}
)
@ddt.data(
{'params': {'course_run_ids': 'a/b/c'}},
{'params': {'query': 'id:course*'}},
......
......@@ -265,11 +265,9 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
course_run_ids = request.GET.get('course_run_ids')
if query and course_run_ids:
course_runs = CourseRun.search(query)
contains = {course_run_id: False for course_run_id in course_run_ids.split(',')}
for course_run in course_runs:
contains[course_run.key] = True
course_run_ids = course_run_ids.split(',')
course_runs = CourseRun.search(query).filter(key__in=course_run_ids).values_list('key', flat=True)
contains = {course_run_id: course_run_id in course_runs for course_run_id in course_run_ids}
instance = {'course_runs': contains}
serializer = serializers.ContainedCourseRunsSerializer(instance)
......
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