From 7e2972786599fc23c436e593fcc236defd93c88c Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Wed, 22 Nov 2017 20:49:07 +0530 Subject: Display error output without reloading. --- yaksh/static/yaksh/js/requesthandler.js | 43 +++++++++++++++-- yaksh/templates/exam.html | 83 ++------------------------------- yaksh/views.py | 26 +++++++++-- 3 files changed, 64 insertions(+), 88 deletions(-) diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 7ff90df..1c9a290 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -53,16 +53,53 @@ function response_handler(method_type, content_type, data, uid){ } else if(content_type.indexOf("application/json") !== -1) { res = JSON.parse(data); request_status = res.status; - if(method_type === "POST") { - uid = res.uid; + if (request_status){ + if(method_type === "POST") { + uid = res.uid; + } + check_state(request_status, uid); + } + else{ + unlock_screen(); + error_array = res.error; + generic_error(error_array) + } - check_state(request_status, uid); } else { reset_values(); unlock_screen(); } } +function generic_error(error_array){ + var error_output = document.getElementById("error_panel"); + error_output.innerHTML = "" + for (var i = 0; i < error_array.length; i++) { + var panel_danger = document.createElement('div'); + panel_danger.setAttribute('class', "panel panel-danger"); + + var panel_heading = document.createElement('div'); + panel_heading.setAttribute('class', "panel-heading"); + panel_heading.innerHTML = "Error no. " + (i + 1); + + var panel_body = document.createElement('div'); + panel_body.setAttribute('class', "panel-body"); + + var well = document.createElement('div'); + well.setAttribute('class', "well well-sm"); + + var pre = document.createElement('pre'); + var code = document.createElement('code'); + console.log(error_array[i]) + code.append(error_array[i]); + pre.appendChild(code); + well.appendChild(pre); + panel_body.appendChild(well); + panel_danger.appendChild(panel_heading); + panel_danger.appendChild(panel_body); + error_output.appendChild(panel_danger); + } +} function ajax_check_code(url, method_type, data_type, data, uid) { $.ajax({ method: method_type, diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index a1f0df4..f722c5f 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -82,86 +82,9 @@
{% if question.type == 'code' or question.type == 'upload' %} - {% if error_message %} -
- {% for error in error_message %} -
-
Error No. {{ forloop.counter }}
-
-
- {% if not error.type %} -
 {{error|safe}} 
- {% elif error.type == 'assertion' %} - {% if error.test_case %} - We tried your code with the following test case:

-
{{error.test_case}}
- {% endif %} -

The following error took place:

- - - - - - - - - - - {% if error.traceback %} - - - {% endif %} - -
Exception Name: {{error.exception}}
Exception Message: {{error.message}}
Full Traceback:
{{error.traceback}}
- {% elif error.type == 'stdio' %} - {% if error.given_input %} - - - - - - -
For given Input value(s):{{error.given_input}}
- {% endif %} - - - - - - - - - - - - {% for expected,user in error.expected_output|zip:error.user_output %} - - - - {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} - - {% else %} - - {% endif %} - - {% endfor %} -
Line No.
Expected Output
User output
Status
{{forloop.counter}} {{expected|default:""}} {{user|default:""}}
- - - - - - -
Error:{{error.error_msg}}
- - {% endif %} -
-
-
- {% endfor %} - -
- {% endif %} +
+
+ {% endif %} diff --git a/yaksh/views.py b/yaksh/views.py index bc03ca2..dd86e40 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -8,7 +8,7 @@ from django.http import HttpResponse, JsonResponse from django.core.urlresolvers import reverse from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect -from django.template import RequestContext +from django.template import RequestContext, Context, Template from django.http import Http404 from django.db.models import Sum, Max, Q, F from django.views.decorators.csrf import csrf_exempt @@ -44,7 +44,7 @@ from yaksh.forms import ( RandomQuestionForm, QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm, get_object_form, FileForm, QuestionPaperForm ) -from .settings import URL_ROOT +from .settings import URL_ROOT from .file_utils import extract_files, is_csv from .send_emails import send_user_mail, generate_activation_key, send_bulk_mail from .decorators import email_verified, has_profile @@ -653,13 +653,29 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): @csrf_exempt def get_result(request, uid): result = {} + template_dir = os.path.dirname(os.path.realpath(__file__)) + template_path = os.path.join(*[template_dir, 'templates', + 'yaksh','error_messages.html' + ]) url = 'http://localhost:%s' % SERVER_POOL_PORT - result_state = get_result_from_code_server(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')) - next_question, error_message, paper = _update_paper(request, uid, result) - return show_question(request, next_question, paper, error_message) + if result.get('success'): + next_question, error_message, paper = _update_paper(request, + uid, result + ) + return show_question(request, next_question, paper, error_message) + # else: + # with open(template_path) as f: + # template_data = f.read() + # template = Template(template_data) + # context = Context(result.get('error')[0]) + # render_error = template.render(context) + # print(render_error) + # result["error"] = render_error + return JsonResponse(result) -- cgit From ae4e2dad126535cda41a7b4a05fdb247f9cdf737 Mon Sep 17 00:00:00 2001 From: mahesh Date: Thu, 23 Nov 2017 01:11:59 +0530 Subject: Render error output with django template instead of javascript --- yaksh/static/yaksh/css/exam.css | 2 +- yaksh/static/yaksh/js/requesthandler.js | 43 ++++---------- yaksh/templates/exam.html | 16 +----- yaksh/templates/yaksh/error_template.html | 93 +++++++++++++++++++++++++++++++ yaksh/views.py | 26 +++++---- 5 files changed, 123 insertions(+), 57 deletions(-) create mode 100644 yaksh/templates/yaksh/error_template.html diff --git a/yaksh/static/yaksh/css/exam.css b/yaksh/static/yaksh/css/exam.css index ec48a14..7d10629 100644 --- a/yaksh/static/yaksh/css/exam.css +++ b/yaksh/static/yaksh/css/exam.css @@ -4,4 +4,4 @@ table td, table th { border: black solid 1px !important; } #stdio, #assertion { table-layout: fixed -} \ No newline at end of file +} diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 1c9a290..5d72241 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -61,9 +61,9 @@ function response_handler(method_type, content_type, data, uid){ } else{ unlock_screen(); - error_array = res.error; - generic_error(error_array) - + var error_output = document.getElementById("error_panel"); + error_output.innerHTML = res.error; + focus_on_error(); } } else { reset_values(); @@ -71,35 +71,14 @@ function response_handler(method_type, content_type, data, uid){ } } -function generic_error(error_array){ - var error_output = document.getElementById("error_panel"); - error_output.innerHTML = "" - for (var i = 0; i < error_array.length; i++) { - var panel_danger = document.createElement('div'); - panel_danger.setAttribute('class', "panel panel-danger"); - - var panel_heading = document.createElement('div'); - panel_heading.setAttribute('class', "panel-heading"); - panel_heading.innerHTML = "Error no. " + (i + 1); - - var panel_body = document.createElement('div'); - panel_body.setAttribute('class', "panel-body"); - - var well = document.createElement('div'); - well.setAttribute('class', "well well-sm"); - - var pre = document.createElement('pre'); - var code = document.createElement('code'); - console.log(error_array[i]) - code.append(error_array[i]); - pre.appendChild(code); - well.appendChild(pre); - panel_body.appendChild(well); - panel_danger.appendChild(panel_heading); - panel_danger.appendChild(panel_body); - error_output.appendChild(panel_danger); - } -} +function focus_on_error(){ + var ele = document.getElementById('error_panel') + if (ele) { + ele.scrollIntoView(true); + window.scrollBy(0, -15); + } + } + function ajax_check_code(url, method_type, data_type, data, uid) { $.ajax({ method: method_type, diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index f722c5f..fede185 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -33,15 +33,6 @@ {% endblock %} {% block content %} -
+

+ {% if question.type == 'code' or question.type == 'upload' %} -
-
- +
{% endif %} - diff --git a/yaksh/templates/yaksh/error_template.html b/yaksh/templates/yaksh/error_template.html new file mode 100644 index 0000000..d42a259 --- /dev/null +++ b/yaksh/templates/yaksh/error_template.html @@ -0,0 +1,93 @@ +{% block css%} + +{% endblock %} + +{% load custom_filters %} + +{% if error_message %} +
+ {% for error in error_message %} + +
+
Error No. {{ forloop.counter }}
+
+
+ {% if not error.type %} +
 {{error|safe}} 
+ + {% elif error.type == 'assertion' %} + + {% if error.test_case %} + We tried your code with the following test case: +

+

+          {{error.test_case}}
+          
+ {% endif %} +

The following error took place:

+ + + + + + + + + + + {% if error.traceback %} + + + {% endif %} + +
Exception Name: {{error.exception}}
Exception Message: {{error.message}}
Full Traceback:
{{error.traceback}}
+ + {% elif error.type == 'stdio' %} + + {% if error.given_input %} + + + + + + +
For given Input value(s):{{error.given_input}}
+ {% endif %} + + + + + + + + + + + + + {% for expected,user in error.expected_output|zip:error.user_output %} + + + + {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} + + {% else %} + + {% endif %} + + {% endfor %} +
Line No.
Expected Output
User output
Status
{{forloop.counter}} {{expected|default:""}} {{user|default:""}}
+ + + + + + +
Error:{{error.error_msg}}
+ {% endif %} +
+
+
+ {% endfor %} +
+{% endif %} \ No newline at end of file diff --git a/yaksh/views.py b/yaksh/views.py index dd86e40..29f017e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -662,19 +662,23 @@ def get_result(request, uid): result['status'] = result_state.get('status') if result['status'] == 'done': result = json.loads(result_state.get('result')) + template_path = os.path.join(*[os.path.dirname(__file__), + 'templates','yaksh', + 'error_template.html' + ] + ) + next_question, error_message, paper = _update_paper(request,uid, + result + ) if result.get('success'): - next_question, error_message, paper = _update_paper(request, - uid, result - ) return show_question(request, next_question, paper, error_message) - # else: - # with open(template_path) as f: - # template_data = f.read() - # template = Template(template_data) - # context = Context(result.get('error')[0]) - # render_error = template.render(context) - # print(render_error) - # result["error"] = render_error + else: + with open(template_path) as f: + template_data = f.read() + template = Template(template_data) + context = Context({"error_message": result.get('error')}) + render_error = template.render(context) + result["error"] = render_error return JsonResponse(result) -- cgit From c5469243a3357a030af0047dac90ae8fbbb77e85 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 23 Nov 2017 12:59:25 +0530 Subject: Minor changes in requesthandler.js --- yaksh/static/yaksh/js/requesthandler.js | 10 +++++++--- yaksh/templates/exam.html | 2 +- yaksh/templates/yaksh/error_template.html | 2 -- yaksh/views.py | 4 ---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 5d72241..9421317 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -61,9 +61,14 @@ function response_handler(method_type, content_type, data, uid){ } else{ unlock_screen(); + // var notify = document.getElementById("notification"); + if ($("#notification")){ + $("#notification").toggle(); + } + var error_output = document.getElementById("error_panel"); error_output.innerHTML = res.error; - focus_on_error(); + focus_on_error(error_output); } } else { reset_values(); @@ -71,8 +76,7 @@ function response_handler(method_type, content_type, data, uid){ } } -function focus_on_error(){ - var ele = document.getElementById('error_panel') +function focus_on_error(ele){ if (ele) { ele.scrollIntoView(true); window.scrollBy(0, -15); diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index fede185..63c31d6 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -73,11 +73,11 @@
- {% if question.type == 'code' or question.type == 'upload' %}
{% endif %} + {% endblock %} diff --git a/yaksh/templates/yaksh/error_template.html b/yaksh/templates/yaksh/error_template.html index d42a259..61657ae 100644 --- a/yaksh/templates/yaksh/error_template.html +++ b/yaksh/templates/yaksh/error_template.html @@ -5,7 +5,6 @@ {% load custom_filters %} {% if error_message %} -
{% for error in error_message %}
@@ -89,5 +88,4 @@
{% endfor %} - {% endif %} \ No newline at end of file diff --git a/yaksh/views.py b/yaksh/views.py index 29f017e..9d0d9e8 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -653,10 +653,6 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): @csrf_exempt def get_result(request, uid): result = {} - template_dir = os.path.dirname(os.path.realpath(__file__)) - template_path = os.path.join(*[template_dir, 'templates', - 'yaksh','error_messages.html' - ]) url = 'http://localhost:%s' % SERVER_POOL_PORT result_state = get_result_from_code_server(url, uid) result['status'] = result_state.get('status') -- cgit From 252449b7b96d73548f53ecd0c3256aa0e777d1dd Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 23 Nov 2017 13:54:47 +0530 Subject: Put page footer outside div container --- yaksh/static/yaksh/js/requesthandler.js | 1 - yaksh/templates/base.html | 2 +- yaksh/templates/exam.html | 9 +-------- yaksh/templates/yaksh/question.html | 14 +++++++++----- yaksh/views.py | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9421317..5159c31 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -61,7 +61,6 @@ function response_handler(method_type, content_type, data, uid){ } else{ unlock_screen(); - // var notify = document.getElementById("notification"); if ($("#notification")){ $("#notification").toggle(); } diff --git a/yaksh/templates/base.html b/yaksh/templates/base.html index e7cc15c..3302482 100644 --- a/yaksh/templates/base.html +++ b/yaksh/templates/base.html @@ -52,6 +52,7 @@ {% block content %} {% endblock %} + - diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index 63c31d6..6589bb5 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -66,18 +66,11 @@

Question(s) left: {{ paper.questions_left }}

-
{% block main %} {% endblock %}
-
-
- {% if question.type == 'code' or question.type == 'upload' %} -
- {% endif %}
- - + {% endblock %} diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index af24778..c1e0e9a 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -227,12 +227,16 @@ lang = "{{ question.language }}" {% if question in paper.get_questions_unanswered %} {% endif %} - - - - + {% endif %} + - {% endif %} + + + +
+ {% if question.type == 'code' or question.type == 'upload' %} +
+ {% endif %}