diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 104 |
1 files changed, 85 insertions, 19 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 8ce0c02..66ad1fa 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -4,7 +4,7 @@ import os from datetime import datetime, timedelta import collections import csv -from django.http import HttpResponse +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 @@ -31,6 +31,18 @@ except ImportError: from io import BytesIO as string_io import re # Local imports. +<<<<<<< HEAD +from yaksh.code_server import get_result, SERVER_POOL_PORT +from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ + StandardTestCase, McqTestCase,\ + StdIOBasedTestCase, HookTestCase, IntegerTestCase,\ + FloatTestCase, StringTestCase +from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ + QuestionForm, RandomQuestionForm,\ + QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ + get_object_form, FileForm, QuestionPaperForm +======= from yaksh.models import ( Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, FloatTestCase, HookTestCase, IntegerTestCase, McqTestCase, Profile, @@ -43,6 +55,7 @@ from yaksh.forms import ( RandomQuestionForm, QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm, get_object_form, FileForm, QuestionPaperForm ) +>>>>>>> 97a96d1678cb55b95ed14eb9a2b2790415d30abc from .settings import URL_ROOT from yaksh.models import AssignmentUpload from .file_utils import extract_files @@ -527,7 +540,6 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): current_question = get_object_or_404(Question, pk=q_id) if request.method == 'POST': - snippet_code = request.POST.get('snippet') # Add the answer submitted, regardless of it being correct or not. if current_question.type == 'mcq': user_answer = request.POST.get('answer') @@ -587,8 +599,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): next_q = paper.add_completed_question(current_question.id) return show_question(request, next_q, paper) else: - user_code = request.POST.get('answer') - user_answer = snippet_code + "\n" + user_code if snippet_code else user_code + user_answer = request.POST.get('answer') if not user_answer: msg = ["Please submit a valid option or code"] return show_question( @@ -599,11 +610,29 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): correct=False, error=json.dumps([]) ) new_answer.save() + uid = new_answer.id paper.answers.add(new_answer) # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. json_data = current_question.consolidate_answer_data(user_answer, user) \ +<<<<<<< HEAD + if current_question.type == 'code' or \ + current_question.type == 'upload' else None + result = paper.validate_answer(user_answer, current_question, + json_data, uid + ) + 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 = json.loads(result.get('result')) + next_question, error_message, paper = _update_paper(request, uid, + result) + return show_question(request, next_question, paper, error_message) + else: + return JsonResponse(result) +======= if current_question.type == 'code' or \ current_question.type == 'upload' else None result = paper.validate_answer( @@ -619,26 +648,63 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): error_message = None new_answer.error = json.dumps(result.get('error')) next_question = paper.add_completed_question(current_question.id) +>>>>>>> 97a96d1678cb55b95ed14eb9a2b2790415d30abc else: - new_answer.marks = (current_question.points * result['weight'] / - current_question.get_maximum_test_case_weight()) \ - if current_question.partial_grading and \ - current_question.type == 'code' or current_question.type == 'upload' \ - else 0 - error_message = result.get('error') if current_question.type == 'code' \ - or current_question.type == 'upload' else None - new_answer.error = json.dumps(result.get('error')) - next_question = current_question if current_question.type == 'code' \ - or current_question.type == 'upload' \ - else paper.add_completed_question(current_question.id) - new_answer.save() - paper.update_marks('inprogress') - paper.set_end_time(timezone.now()) - return show_question(request, next_question, paper, error_message) + next_question, error_message, paper = _update_paper(request, uid, result) + return show_question(request, next_question, paper, error_message) else: return show_question(request, current_question, paper) +<<<<<<< HEAD +@csrf_exempt +def get_results(request, uid): + result = {} + url = 'http://localhost:%s' % SERVER_POOL_PORT + result_state = get_result(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) + return JsonResponse(result) + + +def _update_paper(request, uid, result): + new_answer = Answer.objects.get(id=uid) + current_question = new_answer.question + paper = new_answer.answerpaper_set.first() + + if result.get('success'): + new_answer.marks = (current_question.points * result['weight'] / + current_question.get_maximum_test_case_weight()) \ + if current_question.partial_grading and \ + current_question.type == 'code' or current_question.type == 'upload' \ + else current_question.points + new_answer.correct = result.get('success') + error_message = None + new_answer.error = json.dumps(result.get('error')) + next_question = paper.add_completed_question(current_question.id) + else: + new_answer.marks = (current_question.points * result['weight'] / + current_question.get_maximum_test_case_weight()) \ + if current_question.partial_grading and \ + current_question.type == 'code' or current_question.type == 'upload' \ + else 0 + error_message = result.get('error') if current_question.type == 'code' \ + or current_question.type == 'upload' else None + new_answer.error = json.dumps(result.get('error')) + next_question = current_question if current_question.type == 'code' \ + or current_question.type == 'upload' \ + else paper.add_completed_question(current_question.id) + new_answer.save() + paper.update_marks('inprogress') + paper.set_end_time(timezone.now()) + return next_question, error_message, paper + + +======= +>>>>>>> 97a96d1678cb55b95ed14eb9a2b2790415d30abc def quit(request, reason=None, attempt_num=None, questionpaper_id=None): """Show the quit page when the user logs out.""" paper = AnswerPaper.objects.get(user=request.user, |