From d72c998352706662dea9498b95d56b7449a9b468 Mon Sep 17 00:00:00 2001 From: Ali Akbar Date: Tue, 10 Sep 2024 22:33:08 +0500 Subject: [PATCH] fix: allow both active and marketable runs in update_course_ai_translations --- .../test_update_course_ai_translations.py | 33 +++++++++++++++++++ .../commands/update_course_ai_translations.py | 7 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) 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 5372b9fbe1..25511bf141 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 @@ -101,6 +101,39 @@ def test_command_with_marketable_flag(self, mock_get_translations): [{'code': 'es', 'label': 'Spanish'}] ) + def test_command_with_marketable_and_active_flag(self, mock_get_translations): + """Test the command with the marketable and active flag filtering both marketable and active course runs.""" + mock_get_translations.return_value = { + **self.TRANSLATION_DATA, + 'available_translation_languages': [{'code': 'es', 'label': 'Spanish'}] + } + + active_non_marketable_course_run = CourseRunFactory(end=now() + datetime.timedelta(days=10)) + + verified_and_audit_type = CourseRunType.objects.get(slug='verified-audit') + verified_and_audit_type.is_marketable = True + verified_and_audit_type.save() + + marketable_non_active_course_run = CourseRunFactory( + status='published', + slug='test-course-run', + type=verified_and_audit_type + ) + seat = SeatFactory(course_run=marketable_non_active_course_run) + marketable_non_active_course_run.seats.add(seat) + + call_command('update_course_ai_translations', partner=self.partner.name, marketable=True) + + marketable_non_active_course_run.refresh_from_db() + self.assertListEqual( + marketable_non_active_course_run.translation_languages, + [{'code': 'es', 'label': 'Spanish'}] + ) + self.assertListEqual( + active_non_marketable_course_run.translation_languages, + [{'code': 'fr', 'label': 'French'}] + ) + def test_command_no_partner(self, _): """Test the command raises an error if no valid partner is found.""" with self.assertRaises(CommandError): 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 d38d56d1fe..d4861dbea4 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 @@ -51,10 +51,11 @@ def handle(self, *args, **options): course_runs = CourseRun.objects.all() - if options['active']: + if options['active'] and options['marketable']: + course_runs = course_runs.marketable().union(course_runs.active()) + elif options['active']: course_runs = course_runs.active() - - if options['marketable']: + elif options['marketable']: course_runs = course_runs.marketable() for course_run in course_runs: