Commit 49e5e215 by Clinton Blackburn Committed by GitHub

Optimized performance of the Affiliate Window endpoint (#322)

ECOM-5534
parent d9c3d591
...@@ -45,7 +45,9 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP ...@@ -45,7 +45,9 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP
def test_affiliate_with_supported_seats(self): def test_affiliate_with_supported_seats(self):
""" Verify that endpoint returns course runs for verified and professional seats only. """ """ Verify that endpoint returns course runs for verified and professional seats only. """
with self.assertNumQueries(11):
response = self.client.get(self.affiliate_url) response = self.client.get(self.affiliate_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
root = ET.fromstring(response.content) root = ET.fromstring(response.content)
self.assertEqual(1, len(root.findall('product'))) self.assertEqual(1, len(root.findall('product')))
...@@ -126,6 +128,8 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP ...@@ -126,6 +128,8 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP
# Superusers can view all catalogs # Superusers can view all catalogs
self.client.force_authenticate(superuser) self.client.force_authenticate(superuser)
with self.assertNumQueries(8):
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
...@@ -135,5 +139,6 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP ...@@ -135,5 +139,6 @@ class AffiliateWindowViewSetTests(ElasticsearchTestMixin, SerializationMixin, AP
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
catalog.viewers = [self.user] catalog.viewers = [self.user]
with self.assertNumQueries(8):
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
...@@ -3,12 +3,10 @@ import logging ...@@ -3,12 +3,10 @@ import logging
import os import os
from io import StringIO from io import StringIO
import pytz
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.management import call_command from django.core.management import call_command
from django.db import transaction from django.db import transaction
from django.db.models import Q
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
...@@ -492,14 +490,10 @@ class AffiliateWindowViewSet(viewsets.ViewSet): ...@@ -492,14 +490,10 @@ class AffiliateWindowViewSet(viewsets.ViewSet):
if not catalog.has_object_read_permission(request): if not catalog.has_object_read_permission(request):
raise PermissionDenied raise PermissionDenied
courses = catalog.courses().active() courses = catalog.courses()
course_runs = CourseRun.objects.filter(course__in=courses).active().marketable()
seats = Seat.objects.filter( seats = Seat.objects.filter(type__in=[Seat.VERIFIED, Seat.PROFESSIONAL]).filter(course_run__in=course_runs)
(Q(course_run__end__gte=datetime.datetime.now(pytz.UTC)) | Q(course_run__end__isnull=True)) & seats = seats.select_related('course_run').prefetch_related('course_run__course', 'course_run__course__partner')
Q(course_run__course__in=courses) & Q(type__in=[Seat.VERIFIED, Seat.PROFESSIONAL]) &
(Q(course_run__enrollment_end__isnull=True) |
Q(course_run__enrollment_end__gte=datetime.datetime.now(pytz.UTC)))
)
serializer = serializers.AffiliateWindowSerializer(seats, many=True) serializer = serializers.AffiliateWindowSerializer(seats, many=True)
return Response(serializer.data) return Response(serializer.data)
......
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