-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Iahmad/migrate transcripts s3 #17605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
cms/djangoapps/contentstore/management/commands/migrate_transcripts.py
This file contains hidden or 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,124 @@ | ||
| """ | ||
| Command to migrate transcripts to django storage. | ||
| """ | ||
|
|
||
| import logging | ||
| from django.core.management import BaseCommand, CommandError | ||
| from opaque_keys import InvalidKeyError | ||
| from opaque_keys.edx.keys import CourseKey | ||
| from opaque_keys.edx.locator import CourseLocator | ||
| from cms.djangoapps.contentstore.tasks import ( | ||
| DEFAULT_ALL_COURSES, | ||
| DEFAULT_FORCE_UPDATE, | ||
| DEFAULT_COMMIT, | ||
| enqueue_async_migrate_transcripts_tasks | ||
| ) | ||
| from openedx.core.lib.command_utils import get_mutually_exclusive_required_option, parse_course_keys | ||
| from openedx.core.djangoapps.video_config.models import TranscriptMigrationSetting | ||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """ | ||
| Example usage: | ||
| $ ./manage.py cms migrate_transcripts --all-courses --force-update --commit | ||
| $ ./manage.py cms migrate_transcripts --course-id 'Course1' --course-id 'Course2' --commit | ||
| $ ./manage.py cms migrate_transcripts --from-settings | ||
| """ | ||
| args = '<course_id course_id ...>' | ||
| help = 'Migrates transcripts to S3 for one or more courses.' | ||
|
|
||
| def add_arguments(self, parser): | ||
| """ | ||
| Add arguments to the command parser. | ||
| """ | ||
| parser.add_argument( | ||
| '--course-id', '--course_id', | ||
| dest='course_ids', | ||
| action='append', | ||
| help=u'Migrates transcripts for the list of courses.' | ||
| ) | ||
| parser.add_argument( | ||
| '--all-courses', '--all', '--all_courses', | ||
| dest='all_courses', | ||
| action='store_true', | ||
| default=DEFAULT_ALL_COURSES, | ||
| help=u'Migrates transcripts to the configured django storage for all courses.' | ||
| ) | ||
| parser.add_argument( | ||
| '--from-settings', '--from_settings', | ||
| dest='from_settings', | ||
| help='Migrate Transcripts with settings set via django admin', | ||
| action='store_true', | ||
| default=False, | ||
| ) | ||
| parser.add_argument( | ||
| '--force-update', '--force_update', | ||
| dest='force_update', | ||
| action='store_true', | ||
| default=DEFAULT_FORCE_UPDATE, | ||
| help=u'Force migrate transcripts for the requested courses, overwrite if already present.' | ||
| ) | ||
| parser.add_argument( | ||
| '--commit', | ||
| dest='commit', | ||
| action='store_true', | ||
| default=DEFAULT_COMMIT, | ||
| help=u'Commits the discovered video transcripts to django storage. ' | ||
| u'Without this flag, the command will return the transcripts discovered for migration.' | ||
| ) | ||
|
|
||
| def _parse_course_key(self, raw_value): | ||
| """ Parses course key from string """ | ||
| try: | ||
| result = CourseKey.from_string(raw_value) | ||
| except InvalidKeyError: | ||
| raise CommandError("Invalid course_key: '%s'." % raw_value) | ||
|
|
||
| if not isinstance(result, CourseLocator): | ||
| raise CommandError(u"Argument {0} is not a course key".format(raw_value)) | ||
|
|
||
| return result | ||
|
|
||
| def _get_migration_options(self, options): | ||
| """ | ||
| Returns the command arguments configured via django admin. | ||
| """ | ||
| force_update = options['force_update'] | ||
| commit = options['commit'] | ||
| courses_mode = get_mutually_exclusive_required_option(options, 'course_ids', 'all_courses', 'from_settings') | ||
| if courses_mode == 'all_courses': | ||
| course_keys = [course.id for course in modulestore().get_course_summaries()] | ||
| elif courses_mode == 'course_ids': | ||
| course_keys = map(self._parse_course_key, options['course_ids']) | ||
| else: | ||
| if self._latest_settings().all_courses: | ||
| course_keys = [course.id for course in modulestore().get_course_summaries()] | ||
| else: | ||
| course_keys = parse_course_keys(self._latest_settings().course_ids.split()) | ||
| force_update = self._latest_settings().force_update | ||
| commit = self._latest_settings().commit | ||
|
|
||
| return course_keys, force_update, commit | ||
|
|
||
| def _latest_settings(self): | ||
| """ | ||
| Return the latest version of the TranscriptMigrationSetting | ||
| """ | ||
| return TranscriptMigrationSetting.current() | ||
|
|
||
| def handle(self, *args, **options): | ||
| """ | ||
| Invokes the migrate transcripts enqueue function. | ||
| """ | ||
| course_keys, force_update, commit = self._get_migration_options(options) | ||
| kwargs = {'force_update': force_update, 'commit': commit} | ||
| try: | ||
| enqueue_async_migrate_transcripts_tasks( | ||
| course_keys, | ||
| **kwargs | ||
| ) | ||
| except InvalidKeyError as exc: | ||
| raise CommandError(u'Invalid course key: ' + unicode(exc)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_current_transcript_migration_settingslooks better.