Skip to content

Commit

Permalink
feat: scheduled jobs save calls to only save relevant fields (#4487)
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 authored Nov 13, 2024
1 parent baafe4b commit 39d332d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion course_discovery/apps/course_metadata/data_loaders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down

0 comments on commit 39d332d

Please sign in to comment.