From ad020f9039e33fc6c29e9f0e1de8fd56c2f78406 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 8 Sep 2017 13:41:40 +0530 Subject: Modification as per comments on the PR --- yaksh/static/yaksh/js/requesthandler.js | 92 +++++++++++++++------------------ yaksh/urls.py | 2 +- yaksh/views.py | 8 +-- 3 files changed, 46 insertions(+), 56 deletions(-) (limited to 'yaksh') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9e2c2e5..73d0405 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,34 +1,34 @@ -request_status = "initial" +request_status = "initial"; count = 0; -checker = null + function submitRequest(){ document.forms["code"].submit(); } +function reset_values() { + request_status = "initial"; + count = 0; +} function check_state(state, uid) { if ((state == "running" || state == "not started") && count < 7) { count++; setTimeout(function() {get_result(uid);}, 2000); } else if (state == "unknown") { - request_status = "initial"; - count = 0; + reset_values(); notify("Request timeout. Try again later"); - clearInterval(checker); unlock_screen(); } else { - request_status = "initial"; - count = 0; - notify("Please try after few minutes"); - clearInterval(checker); + reset_values() + notify("Please try again"); unlock_screen(); } } function notify(text) { - var $notice = document.getElementById("notification"); - $notice.classList.add("alert"); - $notice.classList.add("alert-success"); - $notice.innerHTML = text; + var notice = document.getElementById("notification"); + notice.classList.add("alert"); + notice.classList.add("alert-success"); + notice.innerHTML = text; } function lock_screen() { @@ -39,54 +39,45 @@ function unlock_screen() { document.getElementById("ontop").style.display = "none"; } -function check_lock_screen() { - var $ontop_div = document.getElementById("ontop"); - if ($ontop_div.style.display == "block") { - $ontop_div.style.display = "none"; - } +function get_result(uid){ + var url = "/exam/get_result/"+uid+"/"; + ajax_check_code(url, "GET", "html", null, uid) } -function get_result(uid){ - var url = "/exam/get_results/"+uid+"/"; - ajax_call(url, "GET", "html", null, uid) +function response_handler(method_type, content_type, data, uid){ + if(content_type.indexOf("text/html") !== -1) { + if( method_type === "POST") { + reset_values(); + } + unlock_screen(); + document.open(); + document.write(data); + document.close(); + } else if(content_type.indexOf("application/json") !== -1) { + res = JSON.parse(data); + request_status = res.status; + if(method_type === "POST") { + uid = res.uid; + } + check_state(request_status, uid); + } else { + reset_values(); + unlock_screen(); + } } -function ajax_call(url, method_type, data_type, data, uid) { +function ajax_check_code(url, method_type, data_type, data, uid) { $.ajax({ method: method_type, url: url, data: data, - dataType: data_type, // Your server can response html, json, xml format. + dataType: data_type, success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); - if(content_type.indexOf("text/html") !== -1) { - if( method_type === "POST") { - request_status = "initial"; - count = 0; - } - clearInterval(checker); - unlock_screen(); - document.open(); - document.write(data); - document.close(); - } else if(content_type.indexOf("application/json") !== -1) { - res = JSON.parse(data); - request_status = res.status; - if(method_type === "POST") { - uid = res.uid; - } - check_state(request_status, uid); - } else { - request_status = "initial"; - count = 0; - clearInterval(checker); - unlock_screen(); - } + response_handler(method_type, content_type, data, uid) }, error: function(xhr, text_status, error_thrown ) { - request_status = "initial"; - count = 0; - clearInterval(checker); + reset_values(); unlock_screen(); notify("There is some problem. Try later.") } @@ -130,10 +121,9 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { - checker = setInterval(check_lock_screen, 30000); lock_screen(); var data = $(this).serializeArray(); - ajax_call($(this).attr("action"), "POST", "html", data, null) + ajax_check_code($(this).attr("action"), "POST", "html", data, null) e.preventDefault(); // To stop the default form submission. }); }); diff --git a/yaksh/urls.py b/yaksh/urls.py index 3a15f99..c236640 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -20,7 +20,7 @@ urlpatterns = [ views.complete), url(r'^register/$', views.user_register, name="register"), url(r'^(?P\d+)/check/$', views.check), - url(r'^get_results/(?P\d+)/$', views.get_results), + url(r'^get_result/(?P\d+)/$', views.get_result), url(r'^(?P\d+)/check/(?P\d+)/(?P\d+)/$',\ views.check), url(r'^(?P\d+)/skip/(?P\d+)/(?P\d+)/$', diff --git a/yaksh/views.py b/yaksh/views.py index 0b601f2..97da29c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -31,7 +31,7 @@ except ImportError: from io import BytesIO as string_io import re # Local imports. -from yaksh.code_server import get_result, SERVER_POOL_PORT +from yaksh.code_server import get_result as get_result_from_code_server, SERVER_POOL_PORT from yaksh.models import ( Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, FloatTestCase, HookTestCase, IntegerTestCase, McqTestCase, Profile, @@ -616,7 +616,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): if current_question.type in ['code', 'upload']: if paper.time_left() <= 0: url = 'http://localhost:%s' % SERVER_POOL_PORT - result = get_result(url, uid, block=True) + result = get_result_from_code_server(url, uid, block=True) result = json.loads(result.get('result')) next_question, error_message, paper = _update_paper(request, uid, result) @@ -631,10 +631,10 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): @csrf_exempt -def get_results(request, uid): +def get_result(request, uid): result = {} url = 'http://localhost:%s' % SERVER_POOL_PORT - result_state = get_result(url, uid) + result_state = get_result_from_code_server(url, uid) result['status'] = result_state.get('status') if result['status'] == 'done': result = json.loads(result_state.get('result')) -- cgit