Skip to content

Commit

Permalink
feat: filter restricted runs on APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila committed May 13, 2024
1 parent 6039074 commit 9c1c271
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 40 deletions.
29 changes: 18 additions & 11 deletions course_discovery/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pytz
import waffle # lint-amnesty, pylint: disable=invalid-django-waffle-import
from django.contrib.auth import get_user_model
from django.db.models import Count
from django.db.models import Count, prefetch_related_objects
from django.db.models.query import Prefetch
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
Expand All @@ -31,7 +31,7 @@
from course_discovery.apps.api.fields import (
HtmlField, ImageField, SlugRelatedFieldWithReadSerializer, SlugRelatedTranslatableField, StdImageSerializerField
)
from course_discovery.apps.api.utils import StudioAPI
from course_discovery.apps.api.utils import StudioAPI, get_forbidden_runs
from course_discovery.apps.catalogs.models import Catalog
from course_discovery.apps.core.api_client.lms import LMSAPIClient
from course_discovery.apps.core.utils import update_instance
Expand Down Expand Up @@ -1638,8 +1638,15 @@ class CourseWithRecommendationsSerializer(FlexFieldsSerializerMixin, TimestampMo
recommendations = serializers.SerializerMethodField()

def get_recommendations(self, course):
recommended_courses = course.recommendations()
forbidden_runs = get_forbidden_runs(self.context['request'])
runs = CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs)
prefetch_related_objects(recommended_courses, Prefetch(
'course_runs',
queryset=runs.select_related('type').prefetch_related('seats')),
)
return CourseRecommendationSerializer(
course.recommendations(),
recommended_courses,
many=True,
context={
'request': self.context.get('request'),
Expand Down Expand Up @@ -1996,7 +2003,7 @@ def get_organization_logo_override_url(self, obj):
return None

@classmethod
def prefetch_queryset(cls, partner, queryset=None):
def prefetch_queryset(cls, partner, queryset=None, course_runs=None):
# Explicitly check if the queryset is None before selecting related
queryset = queryset if queryset is not None else Program.objects.filter(partner=partner)

Expand All @@ -2020,7 +2027,7 @@ def prefetch_queryset(cls, partner, queryset=None):
'degree__rankings',
'degree__quick_facts',
'labels',
Prefetch('courses', queryset=MinimalProgramCourseSerializer.prefetch_queryset()),
Prefetch('courses', queryset=MinimalProgramCourseSerializer.prefetch_queryset(course_runs=course_runs)),
Prefetch('authoring_organizations', queryset=OrganizationSerializer.prefetch_queryset(partner)),
)

Expand Down Expand Up @@ -2165,8 +2172,8 @@ class MinimalExtendedProgramSerializer(MinimalProgramSerializer):
expected_learning_items = serializers.SlugRelatedField(many=True, read_only=True, slug_field='value')

@classmethod
def prefetch_queryset(cls, partner, queryset=None):
queryset = super().prefetch_queryset(partner=partner, queryset=queryset)
def prefetch_queryset(cls, partner, queryset=None, course_runs=None):
queryset = super().prefetch_queryset(partner=partner, queryset=queryset, course_runs=course_runs)

return queryset.prefetch_related(
'expected_learning_items',
Expand Down Expand Up @@ -2209,7 +2216,7 @@ class ProgramSerializer(MinimalProgramSerializer):
product_source = SourceSerializer(required=False, read_only=True)

@classmethod
def prefetch_queryset(cls, partner, queryset=None):
def prefetch_queryset(cls, partner, queryset=None, course_runs=None):
"""
Prefetch the related objects that will be serialized with a `Program`.
Expand Down Expand Up @@ -2255,7 +2262,7 @@ def prefetch_queryset(cls, partner, queryset=None):
'instructor_ordering',
# We need the full Course prefetch here to get CourseRun information that methods on the Program
# model iterate across (e.g. language). These fields aren't prefetched by the minimal Course serializer.
Prefetch('courses', queryset=CourseSerializer.prefetch_queryset(partner=partner)),
Prefetch('courses', queryset=CourseSerializer.prefetch_queryset(partner=partner, course_runs=course_runs)),
Prefetch('authoring_organizations', queryset=OrganizationSerializer.prefetch_queryset(partner)),
Prefetch('credit_backing_organizations', queryset=OrganizationSerializer.prefetch_queryset(partner)),
Prefetch('corporate_endorsements', queryset=CorporateEndorsementSerializer.prefetch_queryset()),
Expand Down Expand Up @@ -2302,11 +2309,11 @@ class PathwaySerializer(BaseModelSerializer):
course_run_statuses = serializers.ReadOnlyField()

@classmethod
def prefetch_queryset(cls, partner):
def prefetch_queryset(cls, partner, course_runs=None):
queryset = Pathway.objects.filter(partner=partner)

return queryset.prefetch_related(
Prefetch('programs', queryset=MinimalProgramSerializer.prefetch_queryset(partner=partner)),
Prefetch('programs', queryset=MinimalProgramSerializer.prefetch_queryset(partner=partner, course_runs=course_runs)),
)

class Meta:
Expand Down
4 changes: 4 additions & 0 deletions course_discovery/apps/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from course_discovery.apps.core.api_client.lms import LMSAPIClient
from course_discovery.apps.core.utils import serialize_datetime
from course_discovery.apps.course_metadata.models import CourseRun
from course_discovery.apps.course_metadata.choices import CourseRunRestrictionType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -198,6 +199,9 @@ def increment_character(character):
"""
return chr(ord(character) + 1) if character != 'z' else 'a'

def get_forbidden_runs(request):
restriction_list=request.query_params.get('restriction_list', '').split(',')
return list(set(CourseRunRestrictionType.values) - set(restriction_list))

class StudioAPI:
"""
Expand Down
5 changes: 3 additions & 2 deletions course_discovery/apps/api/v1/views/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from course_discovery.apps.api import filters, serializers
from course_discovery.apps.api.pagination import ProxiedPagination
from course_discovery.apps.api.renderers import CourseRunCSVRenderer
from course_discovery.apps.api.utils import check_catalog_api_access
from course_discovery.apps.api.utils import check_catalog_api_access, get_forbidden_runs
from course_discovery.apps.catalogs.models import Catalog
from course_discovery.apps.course_metadata.models import CourseRun, CourseType

Expand Down Expand Up @@ -105,7 +105,8 @@ def courses(self, request, id=None): # pylint: disable=redefined-builtin, unuse
if not catalog.include_archived:
queryset = queryset.available()
course_runs = course_runs.active().enrollable().marketable()

forbidden_runs = get_forbidden_runs(request)
course_runs = CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs)
queryset = serializers.CatalogCourseSerializer.prefetch_queryset(
self.request.site.partner,
queryset=queryset,
Expand Down
7 changes: 5 additions & 2 deletions course_discovery/apps/api/v1/views/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from course_discovery.apps.api.pagination import ProxiedPagination
from course_discovery.apps.api.permissions import IsCourseEditorOrReadOnly
from course_discovery.apps.api.serializers import CourseEntitlementSerializer, MetadataWithType
from course_discovery.apps.api.utils import decode_image_data, get_query_param, reviewable_data_has_changed
from course_discovery.apps.api.utils import decode_image_data, get_query_param, reviewable_data_has_changed, get_forbidden_runs
from course_discovery.apps.api.v1.exceptions import EditableAndQUnsupported
from course_discovery.apps.api.v1.views.course_runs import CourseRunViewSet
from course_discovery.apps.course_metadata.choices import CourseRunStatus, ProgramStatus
Expand Down Expand Up @@ -122,9 +122,11 @@ def get_queryset(self):
else:
queryset = self.queryset

forbidden_runs = get_forbidden_runs(self.request)
if q:
queryset = Course.search(q, queryset=queryset)
queryset = self.get_serializer_class().prefetch_queryset(queryset=queryset, partner=partner)
course_runs = CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs)
queryset = self.get_serializer_class().prefetch_queryset(queryset=queryset, partner=partner, course_runs=course_runs)
else:
if edit_mode:
course_runs = CourseRun.objects.filter_drafts(course__partner=partner)
Expand All @@ -148,6 +150,7 @@ def get_queryset(self):
else:
programs = Program.objects.exclude(status=ProgramStatus.Deleted)

course_runs = course_runs.exclude(restricted_run__restriction_type__in=forbidden_runs)
queryset = self.get_serializer_class().prefetch_queryset(
queryset=queryset,
course_runs=course_runs,
Expand Down
7 changes: 6 additions & 1 deletion course_discovery/apps/api/v1/views/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
from rest_framework import viewsets

from course_discovery.apps.api import serializers
from course_discovery.apps.api.utils import get_forbidden_runs
from course_discovery.apps.api.cache import CompressedCacheResponseMixin
from course_discovery.apps.api.permissions import ReadOnlyByPublisherUser

from course_discovery.apps.course_metadata.models import CourseRun

class PathwayViewSet(CompressedCacheResponseMixin, viewsets.ReadOnlyModelViewSet):
permission_classes = (ReadOnlyByPublisherUser,)
serializer_class = serializers.PathwaySerializer

def get_queryset(self):
queryset = self.get_serializer_class().prefetch_queryset(partner=self.request.site.partner)
forbidden_runs = get_forbidden_runs(self.request)
course_runs = CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs)

queryset = self.get_serializer_class().prefetch_queryset(partner=self.request.site.partner, course_runs=course_runs)
return queryset.order_by('created')
9 changes: 6 additions & 3 deletions course_discovery/apps/api/v1/views/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from course_discovery.apps.api import filters, serializers
from course_discovery.apps.api.cache import CompressedCacheResponseMixin
from course_discovery.apps.api.pagination import ProxiedPagination
from course_discovery.apps.api.utils import get_query_param
from course_discovery.apps.course_metadata.models import Program
from course_discovery.apps.api.utils import get_query_param, get_forbidden_runs
from course_discovery.apps.course_metadata.models import Program, CourseRun


class ProgramViewSet(CompressedCacheResponseMixin, viewsets.ReadOnlyModelViewSet):
Expand Down Expand Up @@ -46,7 +46,10 @@ def get_queryset(self):
queryset = Program.objects.filter(uuid=program_uuid)
elif q:
queryset = Program.search(q, queryset=queryset)
return self.get_serializer_class().prefetch_queryset(queryset=queryset, partner=partner)

forbidden_runs = get_forbidden_runs(self.request)
course_runs = CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs)
return self.get_serializer_class().prefetch_queryset(queryset=queryset, partner=partner, course_runs=course_runs)

def get_serializer_context(self):
context = super().get_serializer_context()
Expand Down
5 changes: 5 additions & 0 deletions course_discovery/apps/api/v1/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from course_discovery.apps.api.utils import update_query_params_with_body_data
from course_discovery.apps.course_metadata.choices import ProgramStatus
from course_discovery.apps.course_metadata.models import Person

from course_discovery.apps.course_metadata.models import *

from course_discovery.apps.course_metadata.search_indexes import documents as search_documents
from course_discovery.apps.course_metadata.search_indexes import serializers as search_indexes_serializers
from course_discovery.apps.course_metadata.search_indexes.constants import LEARNER_PATHWAY_FEATURE_PARAM
Expand Down Expand Up @@ -101,6 +104,8 @@ class CourseSearchViewSet(BaseElasticsearchDocumentViewSet):
'subjects': {'field': 'subjects.raw', 'enabled': True},
'prerequisites': {'field': 'prerequisites', 'enabled': True},
}




class CourseRunSearchViewSet(FacetQueryFieldsMixin, BaseElasticsearchDocumentViewSet):
Expand Down
19 changes: 17 additions & 2 deletions course_discovery/apps/course_metadata/algolia_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from course_discovery.apps.course_metadata.choices import CourseRunStatus, ExternalProductStatus, ProgramStatus
from course_discovery.apps.course_metadata.models import (
AbstractLocationRestrictionModel, Course, CourseType, ProductValue, Program, ProgramType
AbstractLocationRestrictionModel, Course, CourseRun, CourseType, ProductValue, Program, ProgramType
)
from course_discovery.apps.course_metadata.utils import transform_skills_data

Expand Down Expand Up @@ -87,7 +87,8 @@ def _wrap(self, *args, **kwargs):


def get_course_availability(course):
all_runs = course.course_runs.filter(status=CourseRunStatus.Published)
all_runs = course.course_runs.all()
all_runs = filter(lambda r: r.status == CourseRunStatus.Published, all_runs)
availability = set()

for course_run in all_runs:
Expand Down Expand Up @@ -211,6 +212,13 @@ class AlgoliaProxyCourse(Course, AlgoliaBasicModelFieldsMixin):
class Meta:
proxy = True

@classmethod
def prefetch_queryset(cls):
return cls.objects.all().prefetch_related(
models.Prefetch(
'course_runs', queryset=CourseRun.objects.filter(restricted_run__isnull=True)
)
)
@property
def product_type(self):
if self.type.slug == CourseType.EXECUTIVE_EDUCATION_2U:
Expand Down Expand Up @@ -465,6 +473,13 @@ class AlgoliaProxyProgram(Program, AlgoliaBasicModelFieldsMixin):
class Meta:
proxy = True

@classmethod
def prefetch_queryset(cls):
return cls.objects.all().prefetch_related(
models.Prefetch(
'courses__course_runs', queryset=CourseRun.objects.filter(restricted_run__isnull=True)
)
)
@property
def product_type(self):
if self.is_2u_degree_program:
Expand Down
4 changes: 2 additions & 2 deletions course_discovery/apps/course_metadata/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def get_queryset(self): # pragma: no cover

bootcamp_contentful_data = fetch_and_transform_bootcamp_contentful_data()
qs1 = [AlgoliaProxyProduct(course, self.language, contentful_data=bootcamp_contentful_data)
for course in AlgoliaProxyCourse.objects.all()]
for course in AlgoliaProxyCourse.prefetch_queryset()]

degree_contentful_data = fetch_and_transform_degree_contentful_data()
qs2 = [AlgoliaProxyProduct(program, self.language, contentful_data=degree_contentful_data)
for program in AlgoliaProxyProgram.objects.all()]
for program in AlgoliaProxyProgram.prefetch_queryset()]

return qs1 + qs2

Expand Down
2 changes: 0 additions & 2 deletions course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,6 @@ def recommendations(self):
)
.select_related('partner', 'type')
.prefetch_related(
Prefetch('course_runs', queryset=CourseRun.objects.select_related('type').prefetch_related('seats')),
'authoring_organizations',
'_official_version'
)
Expand All @@ -1937,7 +1936,6 @@ def recommendations(self):
)
.select_related('partner', 'type')
.prefetch_related(
Prefetch('course_runs', queryset=CourseRun.objects.select_related('type').prefetch_related('seats')),
'authoring_organizations',
'_official_version'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.conf import settings
from django.db.models import Prefetch
from django_elasticsearch_dsl import Index, fields
from opaque_keys.edx.keys import CourseKey
from taxonomy.choices import ProductTypes
from taxonomy.utils import get_whitelisted_serialized_skills

from course_discovery.apps.course_metadata.models import Course
from course_discovery.apps.course_metadata.models import Course, CourseRun
from course_discovery.apps.course_metadata.utils import get_product_skill_names

from .analyzers import case_insensitive_keyword
Expand Down Expand Up @@ -122,9 +123,12 @@ def prepare_partner(self, obj):
def prepare_prerequisites(self, obj):
return [prerequisite.name for prerequisite in obj.prerequisites.all()]

def get_queryset(self):
def get_queryset(self, forbidden_runs=[]):
return super().get_queryset().prefetch_related(
'course_runs__seats__type', 'course_runs__type', 'course_runs__language').select_related('partner')
Prefetch('course_runs', queryset=CourseRun.objects.exclude(restricted_run__restriction_type__in=forbidden_runs).prefetch_related(
'seats__type', 'type', 'language'
))
).select_related('partner')

def prepare_course_type(self, obj):
return obj.type.slug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def prepare_transcript_languages(self, obj):
for language in obj.transcript_languages.all()
]

def get_queryset(self):
def get_queryset(self, forbidden_runs=[]):
return filter_visible_runs(
super().get_queryset()
.select_related('course')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.conf import settings
from django.db.models import Prefetch
from django_elasticsearch_dsl import Index, fields

from course_discovery.apps.learner_pathway.choices import PathwayStatus
from course_discovery.apps.learner_pathway.models import LearnerPathway

from course_discovery.apps.course_metadata.models import CourseRun

from .analyzers import case_insensitive_keyword, edge_ngram_completion, html_strip, synonym_text
from .common import BaseDocument, OrganizationsMixin

Expand Down Expand Up @@ -50,10 +53,16 @@ def prepare_partner(self, obj):
def prepare_published(self, obj):
return obj.status == PathwayStatus.Active

def get_queryset(self):
def get_queryset(self, forbidden_runs=[]):
return super().get_queryset().prefetch_related(
'steps', 'steps__learnerpathwaycourse_set', 'steps__learnerpathwayprogram_set',
'steps__learnerpathwayblock_set'
'steps__learnerpathwayblock_set',
Prefetch('steps__learnerpathwaycourse_set__course__course_runs', queryset=CourseRun.objects.exclude(
restricted_run__restriction_type__in=forbidden_runs
)),
Prefetch('steps__learnerpathwayprogram_set__program__courses__course_runs', queryset=CourseRun.objects.exclude(
restricted_run__restriction_type__in=forbidden_runs
))
)

def prepare_skill_names(self, obj):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def prepare_position(self, obj):
return []
return [position.title, position.organization_override]

def get_queryset(self):
def get_queryset(self, forbidden_runs=[]):
return super().get_queryset().select_related('bio_language')

class Django:
Expand Down
Loading

0 comments on commit 9c1c271

Please sign in to comment.