diff --git a/course_discovery/apps/course_metadata/data_loaders/analytics_api.py b/course_discovery/apps/course_metadata/data_loaders/analytics_api.py index c044b0e120..03ae6efee2 100644 --- a/course_discovery/apps/course_metadata/data_loaders/analytics_api.py +++ b/course_discovery/apps/course_metadata/data_loaders/analytics_api.py @@ -91,7 +91,7 @@ def _process_course_enrollment_count(self, course, count, recent_count): # update course count course.enrollment_count = count course.recent_enrollment_count = recent_count - course.save() + course.save(update_fields=['enrollment_count', 'recent_enrollment_count']) # Add course count to program dictionary for all programs for program in course.programs.all(): diff --git a/course_discovery/apps/course_metadata/data_loaders/api.py b/course_discovery/apps/course_metadata/data_loaders/api.py index 40cf7c984b..eb5e44be73 100644 --- a/course_discovery/apps/course_metadata/data_loaders/api.py +++ b/course_discovery/apps/course_metadata/data_loaders/api.py @@ -135,7 +135,7 @@ def process_single_course_run(self, body): logger.info(f"Course created with uuid {course.uuid} and key {course.key}") logger.info(f"Course run created with uuid {course_run.uuid} and key {course_run.key}") course.canonical_course_run = course_run - course.save() + course.save(update_fields=['canonical_course_run']) except Exception: # pylint: disable=broad-except if self.enable_api: msg = 'An error occurred while updating {course_run} from {api_url}'.format( diff --git a/course_discovery/apps/course_metadata/data_loaders/csv_loader.py b/course_discovery/apps/course_metadata/data_loaders/csv_loader.py index 06877e3860..d1c3e788a8 100644 --- a/course_discovery/apps/course_metadata/data_loaders/csv_loader.py +++ b/course_discovery/apps/course_metadata/data_loaders/csv_loader.py @@ -403,8 +403,8 @@ def add_product_source(self, course): course.product_source = self.product_source if course.official_version: course.official_version.product_source = self.product_source - course.official_version.save() - course.save() + course.official_version.save(update_fields=['product_source']) + course.save(update_fields=['product_source']) def get_ingestion_stats(self): return { diff --git a/course_discovery/apps/course_metadata/management/commands/tests/test_update_course_ai_translations.py b/course_discovery/apps/course_metadata/management/commands/tests/test_update_course_ai_translations.py index 263ba7d9d3..55e50439ce 100644 --- a/course_discovery/apps/course_metadata/management/commands/tests/test_update_course_ai_translations.py +++ b/course_discovery/apps/course_metadata/management/commands/tests/test_update_course_ai_translations.py @@ -40,6 +40,29 @@ def test_update_course_run_translations(self, mock_get_translations): self.TRANSLATION_DATA['available_translation_languages'] ) + def test_update_course_run_translations_draft(self, mock_get_translations): + """ + Test the command with both draft and non-draft course runs, ensuring that the both draft and non-draft + course runs are updated with the available translation languages. + """ + mock_get_translations.return_value = self.TRANSLATION_DATA + draft_course_run = CourseRunFactory( + draft=True, end=now() + datetime.timedelta(days=10) + ) + course_run = CourseRunFactory(draft=False, draft_version_id=draft_course_run.id) + + call_command("update_course_ai_translations", partner=self.partner.name) + + course_run.refresh_from_db() + self.assertListEqual( + course_run.translation_languages, self.TRANSLATION_DATA["available_translation_languages"], + ) + + draft_course_run.refresh_from_db() + self.assertListEqual( + draft_course_run.translation_languages, self.TRANSLATION_DATA["available_translation_languages"], + ) + def test_command_with_no_translations(self, mock_get_translations): """Test the command when no translations are returned for a course run.""" mock_get_translations.return_value = { diff --git a/course_discovery/apps/course_metadata/management/commands/update_course_ai_translations.py b/course_discovery/apps/course_metadata/management/commands/update_course_ai_translations.py index d4861dbea4..8cbd5826b0 100644 --- a/course_discovery/apps/course_metadata/management/commands/update_course_ai_translations.py +++ b/course_discovery/apps/course_metadata/management/commands/update_course_ai_translations.py @@ -67,11 +67,11 @@ def handle(self, *args, **options): if translation_data.get('feature_enabled', False) else [] ) - course_run.save() + course_run.save(update_fields=["translation_languages"]) if course_run.draft_version: course_run.draft_version.translation_languages = course_run.translation_languages - course_run.draft_version.save() + course_run.draft_version.save(update_fields=["translation_languages"]) logger.info(f'Updated translations for {course_run.key} (both draft and non-draft versions)') else: logger.info(f'Updated translations for {course_run.key} (non-draft version only)')