diff --git a/cms/djangoapps/contentstore/management/commands/import.py b/cms/djangoapps/contentstore/management/commands/import.py index 2e16bf1258..bb41010580 100644 --- a/cms/djangoapps/contentstore/management/commands/import.py +++ b/cms/djangoapps/contentstore/management/commands/import.py @@ -3,6 +3,8 @@ Script for importing courseware from XML format """ from django.core.management.base import BaseCommand, CommandError, make_option +from django_comment_common.utils import (seed_permissions_roles, + are_permissions_roles_seeded) from xmodule.modulestore.xml_importer import import_from_xml from xmodule.modulestore.django import modulestore from xmodule.contentstore.django import contentstore @@ -42,5 +44,14 @@ class Command(BaseCommand): 'default\n') mstore = modulestore('default') - import_from_xml(mstore, data_dir, course_dirs, load_error_modules=False, - static_content_store=contentstore(), verbose=True, do_import_static=do_import_static) + _, course_items = import_from_xml( + mstore, data_dir, course_dirs, load_error_modules=False, + static_content_store=contentstore(), verbose=True, + do_import_static=do_import_static + ) + + for module in course_items: + course_id = module.location.course_id + if not are_permissions_roles_seeded(course_id): + self.stdout.write('Seeding forum roles for course {0}'.format(course_id)) + seed_permissions_roles(course_id) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_import.py b/cms/djangoapps/contentstore/management/commands/tests/test_import.py new file mode 100644 index 0000000000..3a3cac70d1 --- /dev/null +++ b/cms/djangoapps/contentstore/management/commands/tests/test_import.py @@ -0,0 +1,50 @@ +""" +Unittests for importing a course via management command +""" + +import os +from path import path +import shutil +import tempfile + +from django.core.management import call_command +from django.test.utils import override_settings + +from contentstore.tests.modulestore_config import TEST_MODULESTORE +from django_comment_common.utils import are_permissions_roles_seeded +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase + + +@override_settings(MODULESTORE=TEST_MODULESTORE) +class TestImport(ModuleStoreTestCase): + """ + Unit tests for importing a course from command line + """ + + COURSE_ID = ['EDx', '0.00x', '2013_Spring', ] + + def setUp(self): + """ + Build course XML for importing + """ + super(TestImport, self).setUp() + self.content_dir = path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.content_dir) + + # Create good course xml + self.good_dir = tempfile.mkdtemp(dir=self.content_dir) + os.makedirs(os.path.join(self.good_dir, "course")) + with open(os.path.join(self.good_dir, "course.xml"), "w+") as f: + f.write(''.format(self.COURSE_ID)) + + with open(os.path.join(self.good_dir, "course", "{0[2]}.xml".format(self.COURSE_ID)), "w+") as f: + f.write('') + + def test_forum_seed(self): + """ + Tests that forum roles were created with import. + """ + self.assertFalse(are_permissions_roles_seeded('/'.join(self.COURSE_ID))) + call_command('import', self.content_dir, self.good_dir) + self.assertTrue(are_permissions_roles_seeded('/'.join(self.COURSE_ID)))