From 84da71d923996fb74de8b77642c09b0bcdbe627b Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Thu, 15 May 2014 15:58:00 -0400 Subject: [PATCH] Ensure all cms commands accept either deprecated or new syntax for keys LMS-2712 --- .../management/commands/check_course.py | 7 ++++++- .../management/commands/clone_course.py | 15 ++++++++++++-- .../management/commands/delete_course.py | 11 +++++++--- .../management/commands/edit_course_tabs.py | 2 +- .../commands/empty_asset_trashcan.py | 9 ++++++++- .../management/commands/export.py | 13 ++++++++---- .../management/commands/export_all_courses.py | 1 - .../management/commands/migrate_to_split.py | 20 ++++++++++++------- 8 files changed, 58 insertions(+), 20 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/check_course.py b/cms/djangoapps/contentstore/management/commands/check_course.py index b7d5606bb0..292139c054 100644 --- a/cms/djangoapps/contentstore/management/commands/check_course.py +++ b/cms/djangoapps/contentstore/management/commands/check_course.py @@ -2,6 +2,8 @@ from django.core.management.base import BaseCommand, CommandError from xmodule.modulestore.django import modulestore from xmodule.modulestore.xml_importer import check_module_metadata_editability from xmodule.modulestore.keys import CourseKey +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey class Command(BaseCommand): @@ -11,7 +13,10 @@ class Command(BaseCommand): if len(args) != 1: raise CommandError("check_course requires one argument: ") - course_key = CourseKey.from_string(args[0]) + try: + course_key = CourseKey.from_string(args[0]) + except InvalidKeyError: + course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) store = modulestore() diff --git a/cms/djangoapps/contentstore/management/commands/clone_course.py b/cms/djangoapps/contentstore/management/commands/clone_course.py index 07cae77f9a..20d34a2d8a 100644 --- a/cms/djangoapps/contentstore/management/commands/clone_course.py +++ b/cms/djangoapps/contentstore/management/commands/clone_course.py @@ -7,6 +7,8 @@ from xmodule.modulestore.django import modulestore from xmodule.contentstore.django import contentstore from student.roles import CourseInstructorRole, CourseStaffRole from xmodule.modulestore.keys import CourseKey +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey # @@ -16,13 +18,22 @@ class Command(BaseCommand): """Clone a MongoDB-backed course to another location""" help = 'Clone a MongoDB backed course to another location' + def course_key_from_arg(self, arg): + """ + Convert the command line arg into a course key + """ + try: + return CourseKey.from_string(arg) + except InvalidKeyError: + return SlashSeparatedCourseKey.from_deprecated_string(arg) + def handle(self, *args, **options): "Execute the command" if len(args) != 2: raise CommandError("clone requires 2 arguments: ") - source_course_id = CourseKey.from_string(args[0]) - dest_course_id = CourseKey.from_string(args[1]) + source_course_id = self.course_key_from_arg(args[0]) + dest_course_id = self.course_key_from_arg(args[1]) mstore = modulestore('direct') cstore = contentstore() diff --git a/cms/djangoapps/contentstore/management/commands/delete_course.py b/cms/djangoapps/contentstore/management/commands/delete_course.py index 981044cd32..6a842123e5 100644 --- a/cms/djangoapps/contentstore/management/commands/delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/delete_course.py @@ -5,6 +5,8 @@ from django.core.management.base import BaseCommand, CommandError from .prompt import query_yes_no from contentstore.utils import delete_course_and_groups from xmodule.modulestore.keys import CourseKey +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey class Command(BaseCommand): @@ -14,7 +16,10 @@ class Command(BaseCommand): if len(args) != 1 and len(args) != 2: raise CommandError("delete_course requires one or more arguments: |commit|") - course_id = CourseKey.from_string(args[0]) + try: + course_key = CourseKey.from_string(args[0]) + except InvalidKeyError: + course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) commit = False if len(args) == 2: @@ -23,6 +28,6 @@ class Command(BaseCommand): if commit: print('Actually going to delete the course from DB....') - if query_yes_no("Deleting course {0}. Confirm?".format(course_id), default="no"): + if query_yes_no("Deleting course {0}. Confirm?".format(course_key), default="no"): if query_yes_no("Are you sure. This action cannot be undone!", default="no"): - delete_course_and_groups(course_id, commit) + delete_course_and_groups(course_key, commit) diff --git a/cms/djangoapps/contentstore/management/commands/edit_course_tabs.py b/cms/djangoapps/contentstore/management/commands/edit_course_tabs.py index 5224bb1310..0d08390e76 100644 --- a/cms/djangoapps/contentstore/management/commands/edit_course_tabs.py +++ b/cms/djangoapps/contentstore/management/commands/edit_course_tabs.py @@ -71,7 +71,7 @@ command again, adding --insert or --delete to edit the list. course_key = CourseKey.from_string(options['course']) except InvalidKeyError: course_key = SlashSeparatedCourseKey.from_deprecated_string(options['course']) - print u'Warning: you are using a deprecated format. Please use {} in the future'.format(course_key) + course = get_course_by_id(course_key) print 'Warning: this command directly edits the list of course tabs in mongo.' diff --git a/cms/djangoapps/contentstore/management/commands/empty_asset_trashcan.py b/cms/djangoapps/contentstore/management/commands/empty_asset_trashcan.py index 85cb8d2f87..1b5ec5bba6 100644 --- a/cms/djangoapps/contentstore/management/commands/empty_asset_trashcan.py +++ b/cms/djangoapps/contentstore/management/commands/empty_asset_trashcan.py @@ -3,6 +3,8 @@ from xmodule.contentstore.utils import empty_asset_trashcan from xmodule.modulestore.django import modulestore from xmodule.modulestore.keys import CourseKey from .prompt import query_yes_no +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey class Command(BaseCommand): @@ -13,7 +15,12 @@ class Command(BaseCommand): raise CommandError("empty_asset_trashcan requires one or no arguments: ||") if len(args) == 1: - course_ids = [CourseKey.from_string(args[0])] + try: + course_key = CourseKey.from_string(args[0]) + except InvalidKeyError: + course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) + + course_ids = [course_key] else: course_ids = [course.id for course in modulestore('direct').get_courses()] diff --git a/cms/djangoapps/contentstore/management/commands/export.py b/cms/djangoapps/contentstore/management/commands/export.py index 07639d5072..212ce7b5f0 100644 --- a/cms/djangoapps/contentstore/management/commands/export.py +++ b/cms/djangoapps/contentstore/management/commands/export.py @@ -8,7 +8,8 @@ from xmodule.modulestore.xml_exporter import export_to_xml from xmodule.modulestore.django import modulestore from xmodule.modulestore.keys import CourseKey from xmodule.contentstore.django import contentstore -from xmodule.course_module import CourseDescriptor +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey class Command(BaseCommand): @@ -22,12 +23,16 @@ class Command(BaseCommand): if len(args) != 2: raise CommandError("export requires two arguments: ") - course_id = CourseKey.from_string(args[0]) + try: + course_key = CourseKey.from_string(args[0]) + except InvalidKeyError: + course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) + output_path = args[1] - print("Exporting course id = {0} to {1}".format(course_id, output_path)) + print("Exporting course id = {0} to {1}".format(course_key, output_path)) root_dir = os.path.dirname(output_path) course_dir = os.path.splitext(os.path.basename(output_path))[0] - export_to_xml(modulestore('direct'), contentstore(), course_id, root_dir, course_dir, modulestore()) + export_to_xml(modulestore('direct'), contentstore(), course_key, root_dir, course_dir, modulestore()) diff --git a/cms/djangoapps/contentstore/management/commands/export_all_courses.py b/cms/djangoapps/contentstore/management/commands/export_all_courses.py index c03e80287f..b9b05cacb8 100644 --- a/cms/djangoapps/contentstore/management/commands/export_all_courses.py +++ b/cms/djangoapps/contentstore/management/commands/export_all_courses.py @@ -5,7 +5,6 @@ from django.core.management.base import BaseCommand, CommandError from xmodule.modulestore.xml_exporter import export_to_xml from xmodule.modulestore.django import modulestore from xmodule.contentstore.django import contentstore -from xmodule.course_module import CourseDescriptor class Command(BaseCommand): diff --git a/cms/djangoapps/contentstore/management/commands/migrate_to_split.py b/cms/djangoapps/contentstore/management/commands/migrate_to_split.py index f205954ba4..0cedb6a924 100644 --- a/cms/djangoapps/contentstore/management/commands/migrate_to_split.py +++ b/cms/djangoapps/contentstore/management/commands/migrate_to_split.py @@ -7,6 +7,9 @@ from django.contrib.auth.models import User from xmodule.modulestore.django import modulestore from xmodule.modulestore.split_migrator import SplitMigrator from xmodule.modulestore.django import loc_mapper +from xmodule.modulestore.keys import CourseKey +from opaque_keys import InvalidKeyError +from xmodule.modulestore.locations import SlashSeparatedCourseKey def user_from_str(identifier): @@ -28,20 +31,23 @@ class Command(BaseCommand): "Migrate a course from old-Mongo to split-Mongo" help = "Migrate a course from old-Mongo to split-Mongo" - args = "location email " + args = "course_key email " def parse_args(self, *args): """ - Return a 4-tuple of (location, user, org, offering). + Return a 4-tuple of (course_key, user, org, offering). If the user didn't specify an org & offering, those will be None. """ if len(args) < 2: raise CommandError( "migrate_to_split requires at least two arguments: " - "a location and a user identifier (email or ID)" + "a course_key and a user identifier (email or ID)" ) - location = args[0] + try: + course_key = CourseKey.from_string(args[0]) + except InvalidKeyError: + course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) try: user = user_from_str(args[1]) @@ -54,10 +60,10 @@ class Command(BaseCommand): except IndexError: org = offering = None - return location, user, org, offering + return course_key, user, org, offering def handle(self, *args, **options): - location, user, org, offering = self.parse_args(*args) + course_key, user, org, offering = self.parse_args(*args) migrator = SplitMigrator( draft_modulestore=modulestore('default'), @@ -66,4 +72,4 @@ class Command(BaseCommand): loc_mapper=loc_mapper(), ) - migrator.migrate_mongo_course(location, user, org, offering) + migrator.migrate_mongo_course(course_key, user, org, offering)