diff options
-rw-r--r-- | yaksh/static/yaksh/js/requesthandler.js | 51 | ||||
-rw-r--r-- | yaksh/templates/yaksh/question.html | 1 | ||||
-rw-r--r-- | yaksh/views.py | 4 |
3 files changed, 41 insertions, 15 deletions
diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 5159c31..3a7cdba 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -81,31 +81,47 @@ function focus_on_error(ele){ window.scrollBy(0, -15); } } +function csrfSafeMethod(method) { + // these HTTP methods do not require CSRF protection + return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); +} -function ajax_check_code(url, method_type, data_type, data, uid) { - $.ajax({ - method: method_type, - url: url, - data: data, - dataType: data_type, - success: function(data, status, xhr) { +function ajax_check_code(url, method_type, data_type, data, uid) + { + var ajax_post_data = { + "method": method_type, + "url": url, + "data": data, + "dataType": data_type, + "beforeSend": function(xhr, settings) { + if (!csrfSafeMethod(settings.type) && !this.crossDomain) { + xhr.setRequestHeader("X-CSRFToken", csrftoken); + } + }, + "success": function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); response_handler(method_type, content_type, data, uid) }, - error: function(xhr, text_status, error_thrown ) { + "error": function(xhr, text_status, error_thrown ) { reset_values(); unlock_screen(); notify("There is some problem. Try later.") } - }); + } + if (question_type == "upload"){ + ajax_post_data["processData"] = false; + ajax_post_data["contentType"] = false; + } + $.ajax(ajax_post_data); } var global_editor = {}; - +var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val(); $(document).ready(function(){ // Codemirror object, language modes and initial content // Get the textarea node + var textarea_node = document.querySelector('#answer'); var mode_dict = { @@ -126,19 +142,26 @@ $(document).ready(function(){ render(); } }; - + if (question_type == 'code'){ + // Initialize the codemirror editor global_editor.editor = CodeMirror.fromTextArea(textarea_node, options); - // Setting code editors initial content global_editor.editor.setValue(init_val); - +} +if (question_type == 'upload' || question_type == 'code') { $('#code').submit(function(e) { lock_screen(); + if (question_type == "code"){ var data = $(this).serializeArray(); + } + else if (question_type == "upload"){ + var data = new FormData(getElementById("code")); + } ajax_check_code($(this).attr("action"), "POST", "html", data, null) - e.preventDefault(); // To stop the default form submission. + e.preventDefault(); // To stop the default form submission. }); + } reset_editor = function() { global_editor.editor.setValue(init_val); diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index d58c934..749b34a 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -88,6 +88,7 @@ function call_skip(url) } init_val = '{{ last_attempt|escape_quotes|safe }}'; lang = "{{ question.language }}" +question_type = "{{ question.type }}" </script> diff --git a/yaksh/views.py b/yaksh/views.py index 74d352b..333b77f 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -579,6 +579,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): request, current_question, paper, notification=msg ) for fname in assignment_filename: + fname._name = fname._name.replace(" ","_") assignment_files = AssignmentUpload.objects.filter( assignmentQuestion=current_question, assignmentFile__icontains=fname, user=user, @@ -588,7 +589,8 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): assignmentQuestion=current_question, assignmentFile__icontains=fname, user=user, question_paper=questionpaper_id) - os.remove(assign_file.assignmentFile.path) + if os.path.exists(assign_file.assignmentFile.path): + os.remove(assign_file.assignmentFile.path) assign_file.delete() AssignmentUpload.objects.create( user=user, assignmentQuestion=current_question, |