Commit be0ab928 by ichuang

add import from github

parent 348ee515
...@@ -5,6 +5,7 @@ import os ...@@ -5,6 +5,7 @@ import os
import sys import sys
import time import time
import tarfile import tarfile
import re
import shutil import shutil
from datetime import datetime from datetime import datetime
from collections import defaultdict from collections import defaultdict
...@@ -1236,7 +1237,63 @@ def import_course(request, org, course, name): ...@@ -1236,7 +1237,63 @@ def import_course(request, org, course, name):
if not has_access(request.user, location): if not has_access(request.user, location):
raise PermissionDenied() raise PermissionDenied()
if request.method == 'POST': course_module = modulestore().get_item(location)
message = ''
if request.method == 'POST' and 'do_github_import' in request.POST:
message = "hello world"
git_repo = request.POST['git_repo'].replace(';','_').replace('\n','')
git_branch = request.POST['git_branch'].replace(';','_').replace('\n','')
metadata = course_module.metadata
if 'import' not in metadata:
metadata['import'] = {}
importinfo = metadata['import']
importinfo['git_repo'] = git_repo
importinfo['git_branch'] = git_branch
log.debug('set export info (%s, %s)' % (git_repo, git_branch))
m = re.search('/([^/]+)\.git$',git_repo) # get local_dir from git_repo
if m:
local_dir = m.group(1)
importinfo['local_dir'] = local_dir
store = get_modulestore(Location(location));
store.update_metadata(location, course_module.metadata) # save in mongodb store
message = "Git repository information updated"
message += "\nlocal_dir = %s" % local_dir
# do import from github by pulling then doing XML import
log.debug('doing import now to %s' % location)
data_root = path(settings.GITHUB_REPO_ROOT)
course_dir = data_root / local_dir
message += 'importing course to %s\n' % course_dir
log.debug(message)
if os.path.exists(course_dir) and request.POST.get('purge')=='yes':
message += 'directory exists, purging contents\n'
shutil.rmtree(course_dir)
# message += os.popen('rm -rf "%s" 2>&1' % course_dir).read()
if not os.path.exists(course_dir):
message += "Creating course directory %s\n" % course_dir
message += os.popen('(cd "%s"; git clone %s) 2>&1' % (data_root, git_repo)).read()
if not os.path.exists(course_dir):
message += "Failed to create course directory!"
cmd = '(cd "%s"; git checkout %s; git pull -u) 2>&1' % (course_dir, git_branch)
message += os.popen(cmd).read()
module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT,
[local_dir], load_error_modules=False, static_content_store=contentstore(), target_location_namespace = Location(location))
# grab error log from import and show that?
message += 'Import done.'
elif request.method == 'POST':
filename = request.FILES['course-data'].name filename = request.FILES['course-data'].name
if not filename.endswith('.tar.gz'): if not filename.endswith('.tar.gz'):
...@@ -1291,12 +1348,13 @@ def import_course(request, org, course, name): ...@@ -1291,12 +1348,13 @@ def import_course(request, org, course, name):
create_all_course_groups(request.user, course_items[0].location) create_all_course_groups(request.user, course_items[0].location)
return HttpResponse(json.dumps({'Status': 'OK'})) return HttpResponse(json.dumps({'Status': 'OK'}))
else:
course_module = modulestore().get_item(location)
return render_to_response('import.html', { # return page to render
return render_to_response('import.html', {
'context_course': course_module, 'context_course': course_module,
'active_tab': 'import', 'active_tab': 'import',
'message' : message,
'successful_import_redirect_url' : reverse('course_index', args=[ 'successful_import_redirect_url' : reverse('course_index', args=[
course_module.location.org, course_module.location.org,
course_module.location.course, course_module.location.course,
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<div class="inner-wrapper"> <div class="inner-wrapper">
<article class="import-overview"> <article class="import-overview">
<div class="description"> <div class="description">
<h2>Import from .tar.gz file</h2>
<h2>Please <a href="https://edge.edx.org/courses/edX/edx101/edX_Studio_Reference/about" target="_blank">read the documentation</a> before attempting an import!</h2> <h2>Please <a href="https://edge.edx.org/courses/edX/edx101/edX_Studio_Reference/about" target="_blank">read the documentation</a> before attempting an import!</h2>
<p><strong>Importing a new course will delete all content currently associated with your course <p><strong>Importing a new course will delete all content currently associated with your course
and replace it with the contents of the uploaded file.</strong></p> and replace it with the contents of the uploaded file.</strong></p>
...@@ -17,7 +18,7 @@ ...@@ -17,7 +18,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 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"> <form id="importform" 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>
<p class="error-block"></p> <p class="error-block"></p>
<a href="#" class="choose-file-button">Choose File</a> <a href="#" class="choose-file-button">Choose File</a>
...@@ -31,6 +32,42 @@ ...@@ -31,6 +32,42 @@
</form> </form>
</article> </article>
</div> </div>
<div class="inner-wrapper">
<article class="import-overview">
<div class="description">
<h2>Import from github</h2>
<p><strong>Importing a new course will delete all content currently associated with your course
and replace it with the contents of the uploaded file.</strong></p>
<p>You may import from a specific branch of a github repository.</p>
% if message:
<br/>
<p><hr width="100%"><pre>${message|h}</pre><hr width="100%"></p>
% endif
</div>
<form action="${reverse('import_course', kwargs=dict(org=context_course.location.org, course=context_course.location.course, name=context_course.location.name))}" method="post" class="import-form">
<ul>
<%
md = context_course.metadata
importinfo = md.get('import',{})
git_repo = importinfo.get('git_repo','')
git_branch = importinfo.get('git_branch','')
%>
<li>git repository location (ssh): <input type="text" size="53" name="git_repo" value="${git_repo}"></li>
<li>branch to use: <input type="text" size="53" name="git_branch" value="${git_branch}"></li>
<li><input type="checkbox" name="purge" value="yes"> Purge existing import files?</li>
</ul>
<br/>
<p>
<input type="submit" name="do_github_import" value="Import from Git Repository" class="submit-button2">
</p>
<input type="hidden" name="csrfmiddlewaretoken" value="${ csrf_token }">
</form>
</article>
</div>
</div> </div>
</%block> </%block>
...@@ -44,7 +81,7 @@ var percent = $('.percent'); ...@@ -44,7 +81,7 @@ var percent = $('.percent');
var status = $('#status'); var status = $('#status');
var submitBtn = $('.submit-button'); var submitBtn = $('.submit-button');
$('form').ajaxForm({ $('#importform').ajaxForm({
beforeSend: function() { beforeSend: function() {
status.empty(); status.empty();
var percentVal = '0%'; var percentVal = '0%';
......
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