Commit b0055879 by Calen Pennington

Merge pull request #948 from MITx/fix/cdodge/import-improvements

have import be a bit more flexible in terms of the packaging of the cont...
parents 44251612 1530341d
...@@ -27,7 +27,7 @@ def get_course_location_for_item(location): ...@@ -27,7 +27,7 @@ def get_course_location_for_item(location):
raise BaseException('Could not find course at {0}'.format(course_search_location)) raise BaseException('Could not find course at {0}'.format(course_search_location))
if found_cnt > 1: 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 location = courses[0].location
......
...@@ -10,6 +10,7 @@ import sys ...@@ -10,6 +10,7 @@ import sys
import time import time
import tarfile import tarfile
import shutil import shutil
import tempfile
from datetime import datetime from datetime import datetime
from collections import defaultdict from collections import defaultdict
from uuid import uuid4 from uuid import uuid4
...@@ -947,6 +948,12 @@ def create_new_course(request): ...@@ -947,6 +948,12 @@ def create_new_course(request):
if existing_course is not None: if existing_course is not None:
return HttpResponse(json.dumps({'ErrMsg': 'There is already a course defined with this name.'})) 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) new_course = modulestore('direct').clone_item(template, dest_location)
if display_name is not None: if display_name is not None:
...@@ -982,7 +989,11 @@ def import_course(request, org, course, name): ...@@ -982,7 +989,11 @@ def import_course(request, org, course, name):
data_root = path(settings.GITHUB_REPO_ROOT) 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)) logging.debug('importing course to {0}'.format(temp_filepath))
...@@ -992,32 +1003,42 @@ def import_course(request, org, course, name): ...@@ -992,32 +1003,42 @@ def import_course(request, org, course, name):
temp_file.write(chunk) temp_file.write(chunk)
temp_file.close() 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) tf = tarfile.open(temp_filepath)
if (data_root / course_dir).isdir(): tf.extractall(course_dir + '/')
shutil.rmtree(data_root / course_dir)
tf.extractall(data_root + '/') # 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
if files != 'course.xml':
return HttpResponse(json.dumps({'ErrMsg': 'Could not find the course.xml file in the package.'}))
os.remove(temp_filepath) # remove the .tar.gz file 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(data_root / course_dir / 'course.xml', 'r') as course_file: with open(course_dir / 'course.xml', 'r') as course_file:
course_data = etree.parse(course_file, parser=edx_xml_parser) course_data = etree.parse(course_file, parser=edx_xml_parser)
course_data_root = course_data.getroot() course_data_root = course_data.getroot()
course_data_root.set('org', org) course_data_root.set('org', org)
course_data_root.set('course', course) course_data_root.set('course', course)
course_data_root.set('url_name', name) 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) course_data.write(course_file)
module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT, module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT,
[course_dir], load_error_modules=False, static_content_store=contentstore()) [course_dir], load_error_modules=False, static_content_store=contentstore())
# remove content directory - we *shouldn't* need this any longer :-) # we can blow this away when we're done importing.
shutil.rmtree(data_root / course_dir) shutil.rmtree(course_dir)
logging.debug('new course at {0}'.format(course_items[0].location)) logging.debug('new course at {0}'.format(course_items[0].location))
......
...@@ -29,7 +29,9 @@ def import_static_content(modules, data_dir, static_content_store): ...@@ -29,7 +29,9 @@ def import_static_content(modules, data_dir, static_content_store):
# now import all static assets # 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 dirname, dirnames, filenames in os.walk(static_dir):
for filename in filenames: for filename in filenames:
......
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