def handle(self, *args, **options):
        if not options['course']:
            raise CommandError(Command.course_option.help)

        try:
            course_key = CourseKey.from_string(options['course'])
        except InvalidKeyError:
            course_key = SlashSeparatedCourseKey.from_deprecated_string(options['course'])

        course = get_course_by_id(course_key)

        print 'Warning: this command directly edits the list of course tabs in mongo.'
        print 'Tabs before any changes:'
        print_course(course)

        try:
            if options['delete']:
                if len(args) != 1:
                    raise CommandError(Command.delete_option.help)
                num = int(args[0])
                if query_yes_no('Deleting tab {0} Confirm?'.format(num), default='no'):
                    primitive_delete(course, num - 1)  # -1 for 0-based indexing
            elif options['insert']:
                if len(args) != 3:
                    raise CommandError(Command.insert_option.help)
                num = int(args[0])
                tab_type = args[1]
                name = args[2]
                if query_yes_no('Inserting tab {0} "{1}" "{2}" Confirm?'.format(num, tab_type, name), default='no'):
                    primitive_insert(course, num - 1, tab_type, name)  # -1 as above
        except ValueError as e:
            # Cute: translate to CommandError so the CLI error prints nicely.
            raise CommandError(e)
Beispiel #2
0
 def test_delete(self):
     """Test primitive tab deletion."""
     course = CourseFactory.create()
     with self.assertRaises(ValueError):
         primitive_delete(course, 0)
     with self.assertRaises(ValueError):
         primitive_delete(course, 1)
     with self.assertRaises(IndexError):
         primitive_delete(course, 6)
     primitive_delete(course, 2)
     self.assertFalse({u'type': u'textbooks'} in course.tabs)
     # Check that discussion has shifted up
     self.assertEquals(course.tabs[2], {'type': 'discussion', 'name': 'Discussion'})
def delete_tab(course_id, args):
    """Deletes the specified tab from the course"""

    try:
        tab_number = int(args)
        course_key = locator.CourseLocator.from_string(course_id)
        course = get_course_by_id(course_key)
    except InvalidKeyError:
        message = _('Error - Invalid Course ID')
        return message
    except ValueError:
        message = _('Error - Invalid arguments. Expecting one argument [tab-number]')
        return message
    try:
        primitive_delete(course, tab_number - 1)  # -1 for 0-based indexing
        message = _("Tab {tab_number} for course {course_key} successfully deleted".format(
            tab_number=tab_number,
            course_key=course_key,
        ))
    except ValueError as error:
        message = _("Command Failed - {msg}".format(
            msg=error,
        ))
    return message