Commit 956a960c by Carlos Andrés Rocha

Add command to dump the course_ids available to the LMS

parent 15852cd8
# pylint: disable=missing-docstring
from optparse import make_option
from textwrap import dedent
from django.core.management.base import BaseCommand, CommandError
from xmodule.modulestore.django import modulestore
class Command(BaseCommand):
"""
Simple command to dump the course_ids available to the lms.
"""
help = dedent(__doc__).strip()
option_list = BaseCommand.option_list + (
make_option('--modulestore',
action='store',
default='default',
help='Name of the modulestore to use'),
)
def handle(self, *args, **options):
output = []
try:
name = options['modulestore']
store = modulestore(name)
except KeyError:
raise CommandError("Unknown modulestore {}".format(name))
for course in store.get_courses():
course_id = course.location.course_id
output.append(course_id)
return '\n'.join(output) + '\n'
"""Tests for Django management commands"""
from StringIO import StringIO
from django.core.management import call_command
from django.test.utils import override_settings
from courseware.tests.tests import TEST_DATA_MONGO_MODULESTORE
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.xml_importer import import_from_xml
DATA_DIR = 'common/test/data/'
@override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE)
class CommandTestCase(ModuleStoreTestCase):
"""Parent class with helpers for testing management commands"""
def load_courses(self):
"""Load test courses and return list of ids"""
store = modulestore()
import_from_xml(store, DATA_DIR, ['toy', 'simple'])
return [course.id for course in store.get_courses()]
def call_command(self, name, *args, **kwargs):
"""Call management command and return output"""
out = StringIO() # To Capture the output of the command
call_command(name, *args, stdout=out, **kwargs)
out.seek(0)
return out.read()
class CommandsTestCase(CommandTestCase):
"""Test case for management commands"""
def setUp(self):
self.loaded_courses = self.load_courses()
def test_dump_course_ids(self):
kwargs = {'modulestore': 'default'}
output = self.call_command('dump_course_ids', **kwargs)
dumped_courses = output.strip().split('\n')
self.assertEqual(self.loaded_courses, dumped_courses)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment