diff --git a/cms/djangoapps/contentstore/views/import_export.py b/cms/djangoapps/contentstore/views/import_export.py index 30db35faf6..e1a0f613a1 100644 --- a/cms/djangoapps/contentstore/views/import_export.py +++ b/cms/djangoapps/contentstore/views/import_export.py @@ -18,8 +18,8 @@ from django_future.csrf import ensure_csrf_cookie from django.core.urlresolvers import reverse from django.core.servers.basehttp import FileWrapper from django.core.files.temp import NamedTemporaryFile -from django.views.decorators.http import require_http_methods from django.core.exceptions import SuspiciousOperation +from django.views.decorators.http import require_http_methods, require_GET from mitxmako.shortcuts import render_to_response from auth.authz import create_all_course_groups @@ -36,7 +36,7 @@ from util.json_request import JsonResponse from extract_tar import safetar_extractall -__all__ = ['import_course', 'generate_export_course', 'export_course'] +__all__ = ['import_course', 'import_status', 'generate_export_course', 'export_course'] log = logging.getLogger(__name__) @@ -145,15 +145,15 @@ def import_course(request, org, course, name): else: # This was the last chunk. - # 'Lock' with status info. - status_file = data_root / (course + filename + ".lock") + # Use sessions to keep info about import progress + session_status = request.session.setdefault("import_status", {}) + key = org + course + filename + session_status[key] = 1 + request.session.modified = True - # Do everything from now on in a with-context, to be sure we've - # properly cleaned up. - with wfile(status_file, course_dir): - - with open(status_file, 'w+') as sf: - sf.write("Extracting") + # Do everything from now on in a try-finally block to make sure + # everything is properly cleaned up. + try: tar_file = tarfile.open(temp_filepath) try: @@ -167,8 +167,8 @@ def import_course(request, org, course, name): status=400 ) - with open(status_file, 'w+') as sf: - sf.write("Verifying") + session_status[key] = 2 + request.session.modified = True # find the 'course.xml' file dirpath = None @@ -221,12 +221,15 @@ def import_course(request, org, course, name): logging.debug('new course at {0}'.format(course_items[0].location)) - with open(status_file, 'w') as sf: - sf.write("Updating course") + session_status[key] = 3 + request.session.modified = True create_all_course_groups(request.user, course_items[0].location) logging.debug('created all course groups at {0}'.format(course_items[0].location)) + finally: + shutil.rmtree(course_dir) + return JsonResponse({'Status': 'OK'}) else: course_module = modulestore().get_item(location) @@ -240,33 +243,27 @@ def import_course(request, org, course, name): }) }) +@require_GET @ensure_csrf_cookie @login_required -def get_import_status(request, course, filename): +def import_status(request, org, course, name): """ Returns an integer corresponding to the status of a file import. These are: - 0 : No status file found (import done or upload still in progress) + 0 : No status info found (import done or upload still in progress) 1 : Extracting file 2 : Validating. 3 : Importing to mongo - 4 : Error reading file (e.g., converting contents to int) - """ - data_root = path(settings.GITHUB_REPO_ROOT) - status_file = data_root / (course + filename + ".lock") - if not os.path.isfile(status_file): - return JsonResponse({"ImportStatus": 0 }) - - with open(status_file, "r") as f: - try: - status = int(f.read()) - except ValueError: - status = 4 - return JsonResponse({"ImportStatus": status}) + try: + session_status = request.session["import_status"] + status = session_status[org + course + name] + except KeyError: + status = 0 + return JsonResponse({"ImportStatus": status }) @ensure_csrf_cookie diff --git a/cms/static/sass/views/_import.scss b/cms/static/sass/views/_import.scss index d342abe8bd..2e0e4a5e95 100644 --- a/cms/static/sass/views/_import.scss +++ b/cms/static/sass/views/_import.scss @@ -121,6 +121,11 @@ } } +div.status-info-block { + display: none; + text-align: left; +} + .not-started { opacity: 0.4; } diff --git a/cms/templates/import.html b/cms/templates/import.html index cc5786bf8d..bd997ffcbe 100644 --- a/cms/templates/import.html +++ b/cms/templates/import.html @@ -35,24 +35,18 @@ -
-

+

+

+ Unpacking - . - . - .

+ Verifying - . - . - .

+ Importing - . - . - .

@@ -69,6 +63,7 @@