-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sync job to populate translation info
- Loading branch information
1 parent
83681de
commit 32e939f
Showing
5 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
course_discovery/apps/course_metadata/management/commands/tests/test_update_translations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Unit tests for the `update_translations` management command. | ||
""" | ||
import datetime | ||
from unittest.mock import patch | ||
|
||
from django.core.management import CommandError, call_command | ||
from django.test import TestCase | ||
from django.utils.timezone import now | ||
|
||
from course_discovery.apps.course_metadata.models import CourseRun, CourseRunType | ||
from course_discovery.apps.course_metadata.tests.factories import CourseRunFactory, PartnerFactory, SeatFactory | ||
|
||
|
||
@patch('course_discovery.apps.core.api_client.lms.LMSAPIClient.get_course_run_translations') | ||
class UpdateTranslationsTests(TestCase): | ||
"""Test Suite for the update_translations management command.""" | ||
|
||
def setUp(self): | ||
self.partner = PartnerFactory() | ||
self.course_run = CourseRunFactory() | ||
|
||
def test_update_course_run_translations(self, mock_get_translations): | ||
"""Test the command with a valid course run and translation data.""" | ||
mock_get_translations.return_value = { | ||
'available_translation_languages': ['fr', 'es'], | ||
'feature_enabled': True | ||
} | ||
|
||
call_command('update_translations', partner=self.partner.name) | ||
|
||
course_run = CourseRun.objects.get(id=self.course_run.id) | ||
self.assertEqual(course_run.translation_languages, ['fr', 'es']) | ||
|
||
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 = {} | ||
|
||
call_command('update_translations', partner=self.partner.name) | ||
|
||
course_run = CourseRun.objects.get(id=self.course_run.id) | ||
self.assertEqual(course_run.translation_languages, []) | ||
|
||
def test_command_with_active_flag(self, mock_get_translations): | ||
"""Test the command with the active flag filtering active course runs.""" | ||
mock_get_translations.return_value = { | ||
'available_translation_languages': ['fr'], | ||
'feature_enabled': True | ||
} | ||
|
||
active_course_run = CourseRunFactory(end=now() + datetime.timedelta(days=10)) | ||
call_command('update_translations', partner=self.partner.name, active=True) | ||
|
||
active_course_run.refresh_from_db() | ||
self.assertEqual(active_course_run.translation_languages, ['fr']) | ||
|
||
def test_command_with_marketable_flag(self, mock_get_translations): | ||
"""Test the command with the marketable flag filtering marketable course runs.""" | ||
mock_get_translations.return_value = { | ||
'available_translation_languages': ['es'], | ||
'feature_enabled': True | ||
} | ||
|
||
verified_and_audit_type = CourseRunType.objects.get(slug='verified-audit') | ||
verified_and_audit_type.is_marketable = True | ||
verified_and_audit_type.save() | ||
|
||
marketable_course_run = CourseRunFactory( | ||
status='published', | ||
slug='test-course-run', | ||
type=verified_and_audit_type | ||
) | ||
seat = SeatFactory(course_run=marketable_course_run) | ||
marketable_course_run.seats.add(seat) | ||
|
||
call_command('update_translations', partner=self.partner.name, marketable=True) | ||
|
||
marketable_course_run.refresh_from_db() | ||
self.assertEqual(marketable_course_run.translation_languages, ['es']) | ||
|
||
def test_command_no_partner(self, _): | ||
"""Test the command raises an error if no valid partner is found.""" | ||
with self.assertRaises(CommandError): | ||
call_command('update_translations', partner='nonexistent-partner') |
74 changes: 74 additions & 0 deletions
74
course_discovery/apps/course_metadata/management/commands/update_translations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
Management command to fetch translation information from the LMS and update the CourseRun model. | ||
""" | ||
|
||
import logging | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from course_discovery.apps.core.api_client.lms import LMSAPIClient | ||
from course_discovery.apps.course_metadata.models import CourseRun, Partner | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Fetches Content AI Translations metadata from the LMS and updates the CourseRun model in Discovery.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--partner', | ||
type=str, | ||
default=settings.DEFAULT_PARTNER_ID, | ||
help='Specify the partner name or ID to fetch translations for. ' | ||
'Defaults to the partner configured in settings.DEFAULT_PARTNER_ID.', | ||
) | ||
parser.add_argument( | ||
'--active', | ||
action='store_true', | ||
default=False, | ||
help='Only update translations for active course runs. Defaults to False.', | ||
) | ||
parser.add_argument( | ||
'--marketable', | ||
action='store_true', | ||
default=False, | ||
help='Only update translations for marketable course runs. Defaults to False.', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Example usage: ./manage.py update_translations --partner=edx --active --marketable | ||
""" | ||
partner_identifier = options.get('partner') | ||
partner = Partner.objects.filter(name__iexact=partner_identifier).first() | ||
|
||
if not partner: | ||
raise CommandError('No partner object found. Ensure that the Partner data is correctly configured.') | ||
|
||
lms_api_client = LMSAPIClient(partner) | ||
|
||
course_runs = CourseRun.objects.all() | ||
|
||
if options['active']: | ||
course_runs = course_runs.active() | ||
|
||
if options['marketable']: | ||
course_runs = course_runs.marketable() | ||
|
||
for course_run in course_runs: | ||
try: | ||
translation_data = lms_api_client.get_course_run_translations(course_run.key) | ||
|
||
course_run.translation_languages = ( | ||
translation_data.get('available_translation_languages', []) | ||
if translation_data.get('feature_enabled', False) | ||
else [] | ||
) | ||
course_run.draft_version.translation_languages = course_run.translation_languages | ||
course_run.save() | ||
|
||
logger.info(f'Updated translations for {course_run.key}') | ||
except Exception as e: # pylint: disable=broad-except | ||
logger.error(f'Error processing {course_run.key}: {e}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters