Commit c70f29e8 by Calen Pennington

Wire up import to the frontend, and make sure that it puts it into the already extant course

parent 3d2a1c24
...@@ -13,6 +13,8 @@ import shutil ...@@ -13,6 +13,8 @@ import shutil
from collections import defaultdict from collections import defaultdict
from uuid import uuid4 from uuid import uuid4
from lxml import etree from lxml import etree
from path import path
from shutil import rmtree
# to install PIL on MacOSX: 'easy_install http://dist.repoze.org/PIL-1.1.6.tar.gz' # to install PIL on MacOSX: 'easy_install http://dist.repoze.org/PIL-1.1.6.tar.gz'
from PIL import Image from PIL import Image
...@@ -849,7 +851,8 @@ def asset_index(request, org, course, name): ...@@ -849,7 +851,8 @@ def asset_index(request, org, course, name):
def edge(request): def edge(request):
return render_to_response('university_profiles/edge.html', {}) return render_to_response('university_profiles/edge.html', {})
@ensure_csrf_cookie
@login_required
def import_course(request, org, course, name): def import_course(request, org, course, name):
location = ['i4x', org, course, 'course', name] location = ['i4x', org, course, 'course', name]
...@@ -859,42 +862,48 @@ def import_course(request, org, course, name): ...@@ -859,42 +862,48 @@ def import_course(request, org, course, name):
raise PermissionDenied() raise PermissionDenied()
if request.method == 'POST': if request.method == 'POST':
filename = request.FILES['file'].name filename = request.FILES['course-data'].name
if not filename.endswith('.tar.gz'): if not filename.endswith('.tar.gz'):
return HttpResponse(json.dumps({'ErrMsg': 'We only support uploading a .tar.gz file.'})) return HttpResponse(json.dumps({'ErrMsg': 'We only support uploading a .tar.gz file.'}))
temp_filepath = settings.GITHUB_REPO_ROOT + '/' + filename data_root = path(settings.GITHUB_REPO_ROOT)
temp_filepath = data_root / filename
logging.debug('importing course to {0}'.format(temp_filepath)) logging.debug('importing course to {0}'.format(temp_filepath))
# stream out the uploaded files in chunks to disk # stream out the uploaded files in chunks to disk
temp_file = open(temp_filepath, 'wb+') temp_file = open(temp_filepath, 'wb+')
for chunk in request.FILES['file'].chunks(): for chunk in request.FILES['course-data'].chunks():
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)
tf.extractall(settings.GITHUB_REPO_ROOT + '/') shutil.rmtree(data_root / course_dir)
tf.extractall(data_root + '/')
os.remove(temp_filepath) # remove the .tar.gz file os.remove(temp_filepath) # remove the .tar.gz file
# @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', '') with open(data_root / 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(temp_filepath / course_dir / 'course.xml', 'rw') as course_file: with open(data_root / course_dir / 'course.xml', 'w') as course_file:
course_data = etree.parse(course_file, parser=edx_xml_parser).getroot()
course_data.set('org', org)
course_data.set('course', course)
course_data.set('url_name', name)
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 :-) # remove content directory - we *shouldn't* need this any longer :-)
shutil.rmtree(temp_filepath.replace('.tar.gz', '')) shutil.rmtree(data_root / course_dir)
logging.debug('new course at {0}'.format(course_items[0].location)) logging.debug('new course at {0}'.format(course_items[0].location))
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<p>Please note that if your course has any problems with auto-generated <code>url_name</code> nodes, <p>Please note that if your course has any problems with auto-generated <code>url_name</code> nodes,
re-importing your course could cause the loss of student data associated with those problems.</p> re-importing your course could cause the loss of student data associated with those problems.</p>
</div> </div>
<form class="import-form"> <form action="${reverse('import_course', kwargs=dict(org=context_course.location.org, course=context_course.location.course, name=context_course.location.name))}" method="post" enctype="multipart/form-data" class="import-form">
<h2>Course to import:</h2> <h2>Course to import:</h2>
<a href="#" class="choose-file-button">Choose File</a> <a href="#" class="choose-file-button">Choose File</a>
<p class="file-name-block"><span class="file-name"></span><a href="#" class="choose-file-button-inline">change</a></p> <p class="file-name-block"><span class="file-name"></span><a href="#" class="choose-file-button-inline">change</a></p>
...@@ -26,3 +26,32 @@ ...@@ -26,3 +26,32 @@
</div> </div>
</div> </div>
</%block> </%block>
<%block name="jsextra">
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
});
})();
</script>
</%block>
\ No newline at end of file
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