From 1530341d5551944dc65515a4d4dad0d8420fc11c Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Mon, 22 Oct 2012 16:01:57 -0400 Subject: [PATCH] have import be a bit more flexible in terms of the packaging of the content. Allow for tar.gz names to be different from the course name. Also don't assume that the unpacking of the zip will yield a single top level root folder. --- cms/djangoapps/contentstore/utils.py | 2 +- cms/djangoapps/contentstore/views.py | 45 ++++++++++++++----- .../xmodule/modulestore/xml_importer.py | 4 +- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 582123b78f..18afd331d0 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -27,7 +27,7 @@ def get_course_location_for_item(location): raise BaseException('Could not find course at {0}'.format(course_search_location)) if found_cnt > 1: - raise BaseException('Found more than one course at {0}. There should only be one!!!'.format(course_search_location)) + raise BaseException('Found more than one course at {0}. There should only be one!!! Dump = {1}'.format(course_search_location, courses)) location = courses[0].location diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index adf79ffe41..eb635bc30d 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -10,6 +10,7 @@ import sys import time import tarfile import shutil +import tempfile from datetime import datetime from collections import defaultdict from uuid import uuid4 @@ -943,6 +944,12 @@ def create_new_course(request): if existing_course is not None: return HttpResponse(json.dumps({'ErrMsg': 'There is already a course defined with this name.'})) + course_search_location = ['i4x', dest_location.org, dest_location.course, 'course', None] + courses = modulestore().get_items(course_search_location) + + if len(courses) > 0: + return HttpResponse(json.dumps({'ErrMsg': 'There is already a course defined with the same organization and course number.'})) + new_course = modulestore('direct').clone_item(template, dest_location) if display_name is not None: @@ -978,7 +985,11 @@ def import_course(request, org, course, name): data_root = path(settings.GITHUB_REPO_ROOT) - temp_filepath = data_root / filename + course_dir = data_root / "{0}-{1}-{2}".format(org, course, name) + if not course_dir.isdir(): + os.mkdir(course_dir) + + temp_filepath = course_dir / filename logging.debug('importing course to {0}'.format(temp_filepath)) @@ -988,32 +999,42 @@ def import_course(request, org, course, name): temp_file.write(chunk) temp_file.close() - # @todo: don't assume the top-level directory that was unziped was the same name (but without .tar.gz) - course_dir = filename.replace('.tar.gz', '') - tf = tarfile.open(temp_filepath) - if (data_root / course_dir).isdir(): - shutil.rmtree(data_root / course_dir) - tf.extractall(data_root + '/') + tf.extractall(course_dir + '/') - os.remove(temp_filepath) # remove the .tar.gz file + # find the 'course.xml' file + for r,d,f in os.walk(course_dir): + for files in f: + if files == 'course.xml': + break + if files == 'course.xml': + break - with open(data_root / course_dir / 'course.xml', 'r') as course_file: + if files != 'course.xml': + return HttpResponse(json.dumps({'ErrMsg': 'Could not find the course.xml file in the package.'})) + + logging.debug('found course.xml at {0}'.format(r)) + + if r != course_dir: + for fname in os.listdir(r): + shutil.move(r/fname, course_dir) + + with open(course_dir / 'course.xml', 'r') as course_file: course_data = etree.parse(course_file, parser=edx_xml_parser) course_data_root = course_data.getroot() course_data_root.set('org', org) course_data_root.set('course', course) course_data_root.set('url_name', name) - with open(data_root / course_dir / 'course.xml', 'w') as course_file: + with open(course_dir / 'course.xml', 'w') as course_file: course_data.write(course_file) module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT, [course_dir], load_error_modules=False, static_content_store=contentstore()) - # remove content directory - we *shouldn't* need this any longer :-) - shutil.rmtree(data_root / course_dir) + # we can blow this away when we're done importing. + shutil.rmtree(course_dir) logging.debug('new course at {0}'.format(course_items[0].location)) diff --git a/common/lib/xmodule/xmodule/modulestore/xml_importer.py b/common/lib/xmodule/xmodule/modulestore/xml_importer.py index 37e57bbcd5..9e03851b88 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_importer.py @@ -29,7 +29,9 @@ def import_static_content(modules, data_dir, static_content_store): # now import all static assets - static_dir = '{0}/{1}/static/'.format(data_dir, course_data_dir) + static_dir = '{0}/static/'.format(course_data_dir) + + logging.debug("Importing static assets in {0}".format(static_dir)) for dirname, dirnames, filenames in os.walk(static_dir): for filename in filenames: