diff --git a/courses/sync_external_courses/external_course_sync_api.py b/courses/sync_external_courses/external_course_sync_api.py index b9d5c4ae0..b3c7060bf 100644 --- a/courses/sync_external_courses/external_course_sync_api.py +++ b/courses/sync_external_courses/external_course_sync_api.py @@ -11,6 +11,7 @@ from django.contrib.contenttypes.models import ContentType from django.db import transaction +from django.db.models import Subquery from wagtail.images.models import Image from wagtail.models import Page @@ -832,10 +833,12 @@ def deactivate_removed_course_runs(external_course_run_codes, platform_name): external_course_run_codes (list): List of external course run codes. platform_name (str): Name of the platform. """ - course_runs = CourseRun.objects.filter( - course__platform__name__iexact=platform_name, start_date__gt=now_in_utc() - ).exclude(external_course_run_id__in=external_course_run_codes) + course_runs = CourseRun.objects.filter(course__platform__name__iexact=platform_name, start_date__gt=now_in_utc()).exclude( + external_course_run_id__in=external_course_run_codes + ) + Product.objects.filter(object_id__in=Subquery(course_runs.values("id"))).update(is_active=False) course_runs.update(live=False) + log.info( f"Deactivated {course_runs.count()} course runs for platform {platform_name}." ) diff --git a/courses/sync_external_courses/external_course_sync_api_test.py b/courses/sync_external_courses/external_course_sync_api_test.py index e8ec8be99..682ee9ab0 100644 --- a/courses/sync_external_courses/external_course_sync_api_test.py +++ b/courses/sync_external_courses/external_course_sync_api_test.py @@ -1003,12 +1003,10 @@ def test_deactivate_removed_course_runs( """ platform = PlatformFactory.create(name=EMERITUS_PLATFORM_NAME) course = CourseFactory.create(platform=platform, is_external=True) - course_run = CourseRunFactory.create( - course=course, - live=True, - external_course_run_id=external_course_run_id, - start_date=start_date, - ) + course_run = CourseRunFactory.create(course=course, live=True, external_course_run_id=external_course_run_id, start_date=start_date) + product = ProductFactory.create(content_object=course_run) deactivate_removed_course_runs(api_course_run_codes, EMERITUS_PLATFORM_NAME) course_run.refresh_from_db() + product.refresh_from_db() assert course_run.live == is_live + assert product.is_active == is_live