Skip to content

Commit

Permalink
chore: cache retired types
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila committed Jan 7, 2025
1 parent 476650b commit 57dacc5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
16 changes: 15 additions & 1 deletion course_discovery/apps/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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.choices import CourseRunRestrictionType
from course_discovery.apps.course_metadata.models import CourseRun
from course_discovery.apps.course_metadata.models import CourseRun, CourseRunType, CourseType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -412,3 +412,17 @@ def wrapper(*args, **kwargs):
return result
return wrapper
return inner


@use_request_cache("retired_run_types_cache", lambda: "retired_run_types")
def get_retired_run_type_ids():
return list(
CourseRunType.objects.filter(slug__in=settings.RETIRED_RUN_TYPES).values_list('id', flat=True)
)


@use_request_cache("retired_course_types_cache", lambda: "retired_course_types")
def get_retired_course_type_ids():
return list(
CourseType.objects.filter(slug__in=settings.RETIRED_COURSE_TYPES).values_list('id', flat=True)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from taxonomy.choices import ProductTypes
from taxonomy.utils import get_whitelisted_serialized_skills

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

Expand Down Expand Up @@ -126,9 +127,7 @@ def prepare_prerequisites(self, obj):
def get_queryset(self, excluded_restriction_types=None):
if excluded_restriction_types is None:
excluded_restriction_types = []
retired_type_ids = list(
CourseType.objects.filter(slug__in=settings.RETIRED_COURSE_TYPES).values_list('id', flat=True)
)
retired_type_ids = get_retired_course_type_ids()

return super().get_queryset().exclude(type_id__in=retired_type_ids).prefetch_related(
Prefetch('course_runs', queryset=CourseRun.objects.exclude(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from taxonomy.choices import ProductTypes
from taxonomy.utils import get_whitelisted_serialized_skills

from course_discovery.apps.api.utils import get_retired_run_type_ids
from course_discovery.apps.course_metadata.choices import CourseRunStatus
from course_discovery.apps.course_metadata.models import CourseRun, CourseRunType
from course_discovery.apps.course_metadata.utils import get_product_skill_names
Expand Down Expand Up @@ -148,9 +149,7 @@ def prepare_transcript_languages(self, obj):
]

def get_queryset(self, excluded_restriction_types=None): # pylint: disable=unused-argument
retired_type_ids = list(
CourseRunType.objects.filter(slug__in=settings.RETIRED_RUN_TYPES).values_list('id', flat=True)
)
retired_type_ids = get_retired_run_type_ids()
return filter_visible_runs(
super().get_queryset()
.exclude(type_id__in=retired_type_ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ def test_product_external_status(self, external_status, should_index):
course.additional_metadata = AdditionalMetadataFactory(product_status=external_status)
assert course.should_index == should_index

@pytest.mark.foo
@override_settings(RETIRED_COURSE_TYPES=['audit'])
def test_retired_course_indexing(self):
"""
Expand Down

0 comments on commit 57dacc5

Please sign in to comment.