From cab43aaf4cfe35602537574bf61a5046b3be0af7 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:49:06 +0530 Subject: bash file based script --- yaksh/bash_files/sample1.args | 1 + yaksh/bash_files/sample1.sh | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 yaksh/bash_files/sample1.args create mode 100755 yaksh/bash_files/sample1.sh (limited to 'yaksh') diff --git a/yaksh/bash_files/sample1.args b/yaksh/bash_files/sample1.args new file mode 100644 index 0000000..541cb64 --- /dev/null +++ b/yaksh/bash_files/sample1.args @@ -0,0 +1 @@ +test.txt \ No newline at end of file diff --git a/yaksh/bash_files/sample1.sh b/yaksh/bash_files/sample1.sh new file mode 100755 index 0000000..965874b --- /dev/null +++ b/yaksh/bash_files/sample1.sh @@ -0,0 +1,2 @@ +#!/bin/bash +cat $1 -- cgit From cd0da16c708e9c80940f4d531b09b67358bda755 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:49:37 +0530 Subject: cpp file based script --- yaksh/c_cpp_files/file_data.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 yaksh/c_cpp_files/file_data.c (limited to 'yaksh') diff --git a/yaksh/c_cpp_files/file_data.c b/yaksh/c_cpp_files/file_data.c new file mode 100644 index 0000000..1c0ab12 --- /dev/null +++ b/yaksh/c_cpp_files/file_data.c @@ -0,0 +1,25 @@ +#include +#include + +extern int ans(); + +template +void check(T expect,T result) +{ + if (expect == result) + { + printf("\nCorrect:\n Expected %d got %d \n",expect,result); + } + else + { + printf("\nIncorrect:\n Expected %d got %d \n",expect,result); + exit (0); + } +} + +int main(void) +{ + int result; + result = ans(); + check(50, result); +} -- cgit From 542f862445b65f56704009e324ed39a7eec23dad Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:50:08 +0530 Subject: java file based script --- yaksh/java_files/read_file.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 yaksh/java_files/read_file.java (limited to 'yaksh') diff --git a/yaksh/java_files/read_file.java b/yaksh/java_files/read_file.java new file mode 100644 index 0000000..21a5836 --- /dev/null +++ b/yaksh/java_files/read_file.java @@ -0,0 +1,26 @@ +class read_file +{ + public static void check(E expect, E result) + { + if(result.equals(expect)) + { + System.out.println("Correct:\nOutput expected "+expect+" and got "+result); + } + else + { + System.out.println("Incorrect:\nOutput expected "+expect+" but got "+result); + System.exit(1); + } + } + public static void main(String arg[]) + { + String result = ""; + Test t = new Test(); + try{ + result = t.readFile();} + catch(Exception e){ + System.out.print(e); + } + check("2", result); + } +} -- cgit From d27b168e6fa0789891acf8952a60a7c200caef44 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:50:58 +0530 Subject: added files upload for questions --- yaksh/forms.py | 7 +++++- yaksh/models.py | 42 +++++++++++++++++++++++++++++++-- yaksh/views.py | 73 ++++++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 98 insertions(+), 24 deletions(-) (limited to 'yaksh') diff --git a/yaksh/forms.py b/yaksh/forms.py index a443e34..1226fe2 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -175,6 +175,11 @@ class QuestionForm(forms.ModelForm): exclude = ['user'] +class FileForm(forms.Form): + file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}), + required=False) + + class RandomQuestionForm(forms.Form): question_type = forms.CharField(max_length=8, widget=forms.Select\ (choices=question_types)) @@ -211,7 +216,7 @@ class ProfileForm(forms.ModelForm): class Meta: model = Profile fields = ['first_name', 'last_name', 'institute', - 'department', 'roll_number', 'position', 'timezone'] + 'department', 'roll_number', 'position', 'timezone'] first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) diff --git a/yaksh/models.py b/yaksh/models.py index 4ee6141..20d8716 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -11,6 +11,8 @@ from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone import pytz +import os +import shutil languages = ( ("python", "Python"), @@ -62,6 +64,9 @@ def has_profile(user): """ check if user has profile """ return True if hasattr(user, 'profile') else False +def get_upload_dir(instance, filename): + return "question_%s/%s" % (instance.question.id, filename) + ############################################################################### class CourseManager(models.Manager): @@ -216,6 +221,10 @@ class Question(models.Model): question_data['test_case_data'] = test_case_data question_data['user_answer'] = user_answer + files = QuestionsFileUpload.objects.filter(question=self) + if files: + question_data['file_paths'] = [(file.files.path, file.extract) + for file in files] return json.dumps(question_data) @@ -252,7 +261,7 @@ class Question(models.Model): model=self.test_case_type ) test_cases = test_case_ctype.get_all_objects_for_this_type( - question=self, + question=self, **kwargs ) @@ -263,7 +272,7 @@ class Question(models.Model): model=self.test_case_type ) test_case = test_case_ctype.get_object_for_this_type( - question=self, + question=self, **kwargs ) @@ -273,6 +282,35 @@ class Question(models.Model): return self.summary +############################################################################### +class QuestionsFileUpload(models.Model): + files = models.FileField(upload_to=get_upload_dir, blank=True) + question = models.ForeignKey(Question, related_name="question") + extract = models.BooleanField(default=False) + + def delete_all_files(self, files): + for file in files: + if os.path.exists(file.files.path): + shutil.rmtree(os.path.dirname(file.files.path)) + file.delete() + + def delete_selected_files(self, files): + for file in files: + if os.path.exists(file.files.path): + os.remove(file.files.path) + if os.listdir(os.path.dirname(file.files.path)) == []: + os.rmdir(os.path.dirname(file.files.path)) + file.delete() + + def extract_files(self, files): + for file in files: + if file.extract: + file.extract = False + else: + file.extract = True + file.save() + + ############################################################################### class Answer(models.Model): """Answers submitted by the users.""" diff --git a/yaksh/views.py b/yaksh/views.py index 56746b0..b5de26d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -22,14 +22,14 @@ import pytz from taggit.models import Tag from itertools import chain import json +import shutil # Local imports. -from yaksh.models import get_model_class, Quiz, Question, QuestionPaper,\ - QuestionSet, Course, Profile, Answer, AnswerPaper, User, TestCase,\ - has_profile +from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ - QuestionForm, RandomQuestionForm,\ - QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ - get_object_form + QuestionForm, RandomQuestionForm,\ + QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ + get_object_form, FileForm from yaksh.xmlrpc_clients import code_server from settings import URL_ROOT from yaksh.models import AssignmentUpload @@ -151,19 +151,27 @@ def add_question(request): if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST) + form = FileForm(request.POST, request.FILES) if question_form.is_valid(): new_question = question_form.save(commit=False) new_question.user = user new_question.save() + files = request.FILES.getlist('file_field') + if files: + for file in files: + QuestionsFileUpload.objects.get_or_create(question=new_question, files=file) return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id)) else: return my_render_to_response('yaksh/add_question.html', - {'form': question_form}, + {'form': question_form, + 'upload_form': form}, context_instance=ci) else: question_form = QuestionForm() + form = FileForm() return my_render_to_response('yaksh/add_question.html', - {'form': question_form}, + {'form': question_form, + 'upload_form': form}, context_instance=ci) @login_required @@ -176,9 +184,24 @@ def edit_question(request, question_id=None): raise Http404('No Question Found') question_instance = Question.objects.get(id=question_id) - + if request.method == "POST" and 'delete_files' in request.POST: + remove_files_id = request.POST.getlist('clear') + if remove_files_id: + files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) + file = QuestionsFileUpload() + file.delete_selected_files(files) if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) + form = FileForm(request.POST, request.FILES) + files = request.FILES.getlist('file_field') + extract_files_id = request.POST.getlist('extract') + if files: + for file in files: + QuestionsFileUpload.objects.get_or_create(question=question_instance, files=file) + if extract_files_id: + files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) + file = QuestionsFileUpload() + file.extract_files(files) if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -196,23 +219,29 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance) + uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, - 'question_id': question_id}, + 'question_id': question_id, + 'upload_form': form, + 'uploaded_files': uploaded_files}, context_instance=ci) else: question_form = QuestionForm(instance=question_instance) + form = FileForm() test_case_type = question_instance.test_case_type test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question']) test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(instance=question_instance) - + uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, - 'question_id': question_id}, + 'question_id': question_id, + 'upload_form': form, + 'uploaded_files': uploaded_files}, context_instance=ci) @login_required @@ -405,18 +434,14 @@ def show_question(request, question, paper, error_message=None): reason='Your time is up!' return complete(request, reason, paper.attempt_number, paper.question_paper.id) test_cases = question.get_test_cases() - context = {'question': question, - 'paper': paper, - 'error_message': error_message, - 'test_cases': test_cases, - 'last_attempt': question.snippet.encode('unicode-escape') - } - + files = QuestionsFileUpload.objects.filter(question_id=question.id) + context = {'question': question, 'paper': paper, 'error_message': error_message, + 'test_cases': test_cases, 'files': files, + 'last_attempt': question.snippet.encode('unicode-escape')} answers = paper.get_previous_answers(question) if answers: last_attempt = answers[0].answer context['last_attempt'] = last_attempt.encode('unicode-escape') - # context['last_attempt'] = answers[0].answer.encode('unicode-escape') ci = RequestContext(request) return my_render_to_response('yaksh/question.html', context, context_instance=ci) @@ -832,7 +857,13 @@ def show_all_questions(request): if request.POST.get('delete') == 'delete': data = request.POST.getlist('question') if data is not None: - question = Question.objects.filter(id__in=data, user_id=user.id).delete() + questions = Question.objects.filter(id__in=data, user_id=user.id) + for question in questions: + files = QuestionsFileUpload.objects.filter(question_id=question.id) + if files: + file = QuestionsFileUpload() + file.delete_all_files(files) + questions.delete() if request.POST.get('upload') == 'upload': form = UploadFileForm(request.POST, request.FILES) -- cgit From fd97b0391b7364927061487604b187541dab6510 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:55:08 +0530 Subject: changed evaluators to support file based questions --- yaksh/bash_code_evaluator.py | 19 +++++++++++++++++-- yaksh/cpp_code_evaluator.py | 21 +++++++++++++++------ yaksh/java_code_evaluator.py | 22 +++++++++++++++------- yaksh/python_assertion_evaluator.py | 17 ++++++++++++++--- yaksh/python_stdout_evaluator.py | 19 ++++++++++++++----- yaksh/scilab_code_evaluator.py | 11 +++++++++-- 6 files changed, 84 insertions(+), 25 deletions(-) (limited to 'yaksh') diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index a0af0e2..c602ceb 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -9,6 +9,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class BashCodeEvaluator(CodeEvaluator): @@ -22,8 +23,11 @@ class BashCodeEvaluator(CodeEvaluator): # Delete the created file. super(BashCodeEvaluator, self).teardown() os.remove(self.submit_code_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): """ Function validates student script using instructor script as reference. Test cases can optionally be provided. The first argument ref_path, is the path to instructor script, it is assumed to @@ -53,6 +57,10 @@ class BashCodeEvaluator(CodeEvaluator): clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(get_ref_path, get_test_case_path) + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -67,6 +75,7 @@ class BashCodeEvaluator(CodeEvaluator): return False, msg success = False + user_answer = user_answer.replace("\r", "") self.write_to_submit_code_file(self.submit_code_path, user_answer) if clean_test_case_path is None or "": @@ -114,6 +123,12 @@ class BashCodeEvaluator(CodeEvaluator): stderr=subprocess.PIPE ) proc, inst_stdout, inst_stderr = ret + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) args = [self.submit_code_path] + \ [x for x in test_case.split()] ret = self._run_command(args, @@ -126,7 +141,7 @@ class BashCodeEvaluator(CodeEvaluator): return True, "Correct answer" else: err = ("Error:expected" - " %s, got %s").format(inst_stdout+inst_stderr, + " {0}, got {1}").format(inst_stdout+inst_stderr, stdnt_stdout+stdnt_stderr ) return False, err diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index b869442..29b24d5 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class CppCodeEvaluator(CodeEvaluator): @@ -22,6 +23,13 @@ class CppCodeEvaluator(CodeEvaluator): super(CppCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) + if os.path.exists(self.ref_output_path): + os.remove(self.ref_output_path) + if os.path.exists(self.user_output_path): + os.remove(self.user_output_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) def set_file_paths(self): user_output_path = os.getcwd() + '/output' @@ -38,14 +46,17 @@ class CppCodeEvaluator(CodeEvaluator): ref_output_path) return compile_command, compile_main - def compile_code(self, user_answer, test_case): + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] if self.compiled_user_answer and self.compiled_test_code: return None else: ref_code_path = test_case clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) - + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -57,7 +68,7 @@ class CppCodeEvaluator(CodeEvaluator): self.user_output_path, self.ref_output_path = self.set_file_paths() self.compile_command, self.compile_main = self.get_commands( clean_ref_code_path, - self.user_output_path, + self.user_output_path, self.ref_output_path ) self.compiled_user_answer = self._run_command( @@ -76,7 +87,7 @@ class CppCodeEvaluator(CodeEvaluator): return self.compiled_user_answer, self.compiled_test_code - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): """ Function validates student code using instructor code as reference.The first argument ref_code_path, is the path to instructor code, it is assumed to have executable permission. @@ -117,7 +128,6 @@ class CppCodeEvaluator(CodeEvaluator): success, err = True, "Correct answer" else: err = stdout + "\n" + stderr - os.remove(self.ref_output_path) else: err = "Error:" try: @@ -129,7 +139,6 @@ class CppCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + main_err - os.remove(self.user_output_path) else: err = "Compilation Error:" try: diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index c64aa3b..dde2ec2 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class JavaCodeEvaluator(CodeEvaluator): @@ -21,7 +22,14 @@ class JavaCodeEvaluator(CodeEvaluator): def teardown(self): super(JavaCodeEvaluator, self).teardown() # Delete the created file. - os.remove(self.submit_code_path) + os.remove(self.submit_code_path) + if os.path.exists(self.user_output_path): + os.remove(self.user_output_path) + if os.path.exists(self.ref_output_path): + os.remove(self.ref_output_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) def get_commands(self, clean_ref_code_path, user_code_directory): compile_command = 'javac {0}'.format(self.submit_code_path), @@ -35,14 +43,17 @@ class JavaCodeEvaluator(CodeEvaluator): output_path = "{0}{1}.class".format(directory, file_name) return output_path - def compile_code(self, user_answer, test_case): + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] if self.compiled_user_answer and self.compiled_test_code: return None else: ref_code_path = test_case clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) - + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -83,7 +94,7 @@ class JavaCodeEvaluator(CodeEvaluator): return self.compiled_user_answer, self.compiled_test_code - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): """ Function validates student code using instructor code as reference.The first argument ref_code_path, is the path to instructor code, it is assumed to have executable permission. @@ -123,7 +134,6 @@ class JavaCodeEvaluator(CodeEvaluator): success, err = True, "Correct answer" else: err = stdout + "\n" + stderr - os.remove(self.ref_output_path) else: err = "Error:" try: @@ -135,7 +145,6 @@ class JavaCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + main_err - os.remove(self.user_output_path) else: err = "Compilation Error:" try: @@ -147,5 +156,4 @@ class JavaCodeEvaluator(CodeEvaluator): err = err + "\n" + e except: err = err + "\n" + stdnt_stderr - return success, err diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index bf6a4be..3e98f08 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -7,6 +7,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator, TimeoutException +from copy_delete_files import CopyDeleteFiles class PythonAssertionEvaluator(CodeEvaluator): @@ -16,7 +17,18 @@ class PythonAssertionEvaluator(CodeEvaluator): super(PythonAssertionEvaluator, self).setup() self.exec_scope = None - def compile_code(self, user_answer, test_case): + def teardown(self): + super(PythonAssertionEvaluator, self).teardown() + # Delete the created file. + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + + def compile_code(self, user_answer, file_paths, test_case): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if self.exec_scope: return None else: @@ -25,7 +37,7 @@ class PythonAssertionEvaluator(CodeEvaluator): exec submitted in self.exec_scope return self.exec_scope - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): success = False try: tb = None @@ -42,6 +54,5 @@ class PythonAssertionEvaluator(CodeEvaluator): else: success = True err = 'Correct answer' - del tb return success, err diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py index 6606581..fa8d6c6 100644 --- a/yaksh/python_stdout_evaluator.py +++ b/yaksh/python_stdout_evaluator.py @@ -8,6 +8,7 @@ from contextlib import contextmanager # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles @contextmanager @@ -25,7 +26,18 @@ def redirect_stdout(): class PythonStdoutEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" - def compile_code(self, user_answer, expected_output): + def teardown(self): + super(PythonStdoutEvaluator, self).teardown() + # Delete the created file. + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) + + def compile_code(self, user_answer, file_paths, expected_output): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) if hasattr(self, 'output_value'): return None else: @@ -36,9 +48,8 @@ class PythonStdoutEvaluator(CodeEvaluator): self.output_value = output_buffer.getvalue() return self.output_value - def check_code(self, user_answer, expected_output): + def check_code(self, user_answer, file_paths, expected_output): success = False - tb = None if expected_output in user_answer: success = False @@ -52,7 +63,5 @@ class PythonStdoutEvaluator(CodeEvaluator): else: success = False err = "Incorrect Answer" - del tb return success, err - diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 91b4cb3..5f38087 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -8,6 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator +from copy_delete_files import CopyDeleteFiles class ScilabCodeEvaluator(CodeEvaluator): @@ -21,8 +22,15 @@ class ScilabCodeEvaluator(CodeEvaluator): super(ScilabCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) + if self.files_list: + file_delete = CopyDeleteFiles() + file_delete.delete_files(self.files_list) - def check_code(self, user_answer, test_case): + def check_code(self, user_answer, file_paths, test_case): + self.files_list = [] + if file_paths: + file_copy = CopyDeleteFiles() + self.files_list = file_copy.copy_files(file_paths) ref_code_path = test_case clean_ref_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) @@ -60,7 +68,6 @@ class ScilabCodeEvaluator(CodeEvaluator): err = add_err + stdout else: err = add_err + stderr - return success, err def _remove_scilab_exit(self, string): -- cgit From 98cda0357f238cd230c26eebbf75e1d90bc3588e Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:56:20 +0530 Subject: added parameter in safe_evaluate to take file paths list --- yaksh/code_evaluator.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'yaksh') diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index aab99eb..2fb429f 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -88,7 +88,7 @@ class CodeEvaluator(object): def setup(self): self._change_dir(self.in_dir) - def safe_evaluate(self, user_answer, test_case_data): + def safe_evaluate(self, user_answer, test_case_data, file_paths=None): """ Handles code evaluation along with compilation, signal handling and Exception handling @@ -101,8 +101,8 @@ class CodeEvaluator(object): # Do whatever testing needed. try: for test_case in test_case_data: - self.compile_code(user_answer, **test_case) - success, err = self.check_code(user_answer, **test_case) + self.compile_code(user_answer, file_paths, **test_case) + success, err = self.check_code(user_answer, file_paths, **test_case) if not success: break @@ -124,7 +124,7 @@ class CodeEvaluator(object): def check_code(self): raise NotImplementedError("check_code method not implemented") - def compile_code(self, user_answer, **kwargs): + def compile_code(self, user_answer, file_paths, **kwargs): pass def create_submit_code_file(self, file_name): @@ -136,7 +136,6 @@ class CodeEvaluator(object): return submit_path - def write_to_submit_code_file(self, file_path, user_answer): """ Write the code (`answer`) to a file""" submit_f = open(file_path, 'w') -- cgit From d8876ae789197de4d977d28f0c0ff04cf39c2c35 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:57:21 +0530 Subject: added test cases for file based questions --- yaksh/evaluator_tests/test_bash_evaluation.py | 26 ++++++-- yaksh/evaluator_tests/test_c_cpp_evaluation.py | 41 +++++++++++-- yaksh/evaluator_tests/test_java_evaluation.py | 48 +++++++++++++-- yaksh/evaluator_tests/test_python_evaluation.py | 79 ++++++++++++++++++++----- yaksh/evaluator_tests/test_scilab_evaluation.py | 15 +++-- 5 files changed, 173 insertions(+), 36 deletions(-) (limited to 'yaksh') diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index 4ff3e0a..1070c6a 100644 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -8,10 +8,11 @@ class BashEvaluationTestCases(unittest.TestCase): self.test_case_data = [ {"test_case": "bash_files/sample.sh,bash_files/sample.args"} ] - self.in_dir = "/tmp" + self.in_dir = os.getcwd() self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your" " code.").format(SERVER_TIMEOUT) + self.file_paths = None def test_correct_answer(self): user_answer = ("#!/bin/bash\n[[ $# -eq 2 ]]" @@ -19,7 +20,8 @@ class BashEvaluationTestCases(unittest.TestCase): ) get_class = BashCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) @@ -30,7 +32,8 @@ class BashEvaluationTestCases(unittest.TestCase): "&& echo $(( $1 - $2 )) && exit $(( $1 - $2 ))") get_class = BashCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) @@ -41,12 +44,27 @@ class BashEvaluationTestCases(unittest.TestCase): " do echo "" > /dev/null ; done") get_class = BashCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) self.assertEquals(result.get("error"), self.timeout_msg) + def test_file_based_assert(self): + self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.test_case_data = [ + {"test_case": "bash_files/sample1.sh,bash_files/sample1.args"} + ] + user_answer = ("#!/bin/bash\ncat $1") + get_class = BashCodeEvaluator() + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertTrue(result.get("success")) + self.assertEquals(result.get("error"), "Correct answer") if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py index 71fb843..71af177 100644 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -2,20 +2,23 @@ import unittest import os from yaksh.cpp_code_evaluator import CppCodeEvaluator from yaksh.settings import SERVER_TIMEOUT +from textwrap import dedent class CEvaluationTestCases(unittest.TestCase): def setUp(self): self.test_case_data = [{"test_case": "c_cpp_files/main.cpp"}] - self.in_dir = "/tmp" + self.in_dir = os.getcwd() self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your" " code.").format(SERVER_TIMEOUT) + self.file_paths = None def test_correct_answer(self): user_answer = "int add(int a, int b)\n{return a+b;}" get_class = CppCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) @@ -25,7 +28,8 @@ class CEvaluationTestCases(unittest.TestCase): user_answer = "int add(int a, int b)\n{return a-b;}" get_class = CppCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -36,7 +40,8 @@ class CEvaluationTestCases(unittest.TestCase): user_answer = "int add(int a, int b)\n{return a+b}" get_class = CppCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) @@ -46,11 +51,37 @@ class CEvaluationTestCases(unittest.TestCase): user_answer = "int add(int a, int b)\n{while(1>0){}}" get_class = CppCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) self.assertEquals(result.get("error"), self.timeout_msg) + def test_file_based_assert(self): + self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.test_case_data = [{"test_case": "c_cpp_files/file_data.c"}] + user_answer = dedent(""" + #include + char ans() + { + FILE *fp; + char buff[255]; + fp = fopen("test.txt", "r"); + fscanf(fp, "%s", buff); + fclose(fp); + return buff[0]; + } + """) + get_class = CppCodeEvaluator(self.in_dir) + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertTrue(result.get('success')) + self.assertEquals(result.get('error'), "Correct answer") + + if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index 801277f..ed28745 100644 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -3,18 +3,19 @@ import os from yaksh import code_evaluator as evaluator from yaksh.java_code_evaluator import JavaCodeEvaluator from yaksh.settings import SERVER_TIMEOUT - +from textwrap import dedent class JavaEvaluationTestCases(unittest.TestCase): def setUp(self): self.test_case_data = [ {"test_case": "java_files/main_square.java"} ] - self.in_dir = "/tmp" + self.in_dir = os.getcwd() evaluator.SERVER_TIMEOUT = 9 self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in" " your code.").format(evaluator.SERVER_TIMEOUT) + self.file_paths = None def tearDown(self): evaluator.SERVER_TIMEOUT = 2 @@ -23,7 +24,8 @@ class JavaEvaluationTestCases(unittest.TestCase): user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a;\n\t}\n}" get_class = JavaCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertEquals(result.get('error'), "Correct answer") @@ -33,7 +35,8 @@ class JavaEvaluationTestCases(unittest.TestCase): user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a;\n\t}\n}" get_class = JavaCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -44,7 +47,8 @@ class JavaEvaluationTestCases(unittest.TestCase): user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a" get_class = JavaCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) @@ -54,12 +58,44 @@ class JavaEvaluationTestCases(unittest.TestCase): user_answer = "class Test {\n\tint square_num(int a) {\n\t\twhile(0==0){\n\t\t}\n\t}\n}" get_class = JavaCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) self.assertEquals(result.get("error"), self.timeout_msg) + def test_file_based_assert(self): + self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.test_case_data = [ + {"test_case": "java_files/read_file.java"} + ] + user_answer = dedent(""" + import java.io.BufferedReader; + import java.io.FileReader; + import java.io.IOException; + class Test{ + String readFile() throws IOException { + BufferedReader br = new BufferedReader(new FileReader("test.txt")); + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine();} + return sb.toString(); + } finally { + br.close(); + }}} + """) + get_class = JavaCodeEvaluator(self.in_dir) + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertTrue(result.get("success")) + self.assertEquals(result.get("error"), "Correct answer") if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 1e867a3..b432630 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -15,13 +15,15 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in" " your code.").format(SERVER_TIMEOUT) + self.file_paths = None def test_correct_answer(self): user_answer = "def add(a,b):\n\treturn a + b" get_class = PythonAssertionEvaluator() - kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data - } + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) self.assertEqual(result.get('error'), "Correct answer") @@ -30,8 +32,9 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): user_answer = "def add(a,b):\n\treturn a - b" get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data - } + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) self.assertEqual(result.get('error'), @@ -42,7 +45,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): user_answer = "def add(a, b):\n\twhile True:\n\t\tpass" get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -63,7 +67,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -86,7 +91,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -105,7 +111,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -126,7 +133,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -148,7 +156,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -171,7 +180,8 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): ] get_class = PythonAssertionEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) err = result.get("error").splitlines() @@ -180,18 +190,37 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): for msg in value_error_msg: self.assertIn(msg, result.get("error")) + def test_file_based_assert(self): + self.test_case_data = [{"test_case": "assert(ans()=='2')"}] + self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + user_answer = dedent(""" + def ans(): + with open("test.txt") as f: + return f.read()[0] + """) + get_class = PythonAssertionEvaluator() + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertEqual(result.get('error'), "Correct answer") + self.assertTrue(result.get('success')) + class PythonStdoutEvaluationTestCases(unittest.TestCase): def setUp(self): self.test_case_data = [{"expected_output": "0 1 1 2 3"}] self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop" " in your code.").format(SERVER_TIMEOUT) + self.file_paths = None def test_correct_answer(self): user_answer = "a,b=0,1\nfor i in range(5):\n\tprint a,\n\ta,b=b,a+b" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertEqual(result.get('error'), "Correct answer") @@ -201,7 +230,8 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): user_answer = "a,b=0,1\nfor i in range(5):\n\tprint b,\n\ta,b=b,a+b" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -214,7 +244,8 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): ) get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -224,12 +255,28 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): user_answer = "def add(a, b):\n\twhile True:\n\t\tpass" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) self.assertEqual(result.get('error'), 'Incorrect Answer') + def test_file_based_answer(self): + self.test_case_data = [{"expected_output": "2\n\n"}] + self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + user_answer = dedent(""" + with open("test.txt") as f: + print f.read() + """) + get_class = PythonStdoutEvaluator() + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertEqual(result.get('error'), "Correct answer") + self.assertTrue(result.get('success')) if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_scilab_evaluation.py b/yaksh/evaluator_tests/test_scilab_evaluation.py index 242f260..f5e3767 100644 --- a/yaksh/evaluator_tests/test_scilab_evaluation.py +++ b/yaksh/evaluator_tests/test_scilab_evaluation.py @@ -7,17 +7,19 @@ from yaksh.settings import SERVER_TIMEOUT class ScilabEvaluationTestCases(unittest.TestCase): def setUp(self): self.test_case_data = [{"test_case": "scilab_files/test_add.sce"}] - self.in_dir = "/tmp" + self.in_dir = os.getcwd() self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop" " in your code.").format(SERVER_TIMEOUT) + self.file_paths = None def test_correct_answer(self): user_answer = ("funcprot(0)\nfunction[c]=add(a,b)" "\n\tc=a+b;\nendfunction") get_class = ScilabCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertEquals(result.get('error'), "Correct answer") @@ -28,7 +30,8 @@ class ScilabEvaluationTestCases(unittest.TestCase): "\n\tc=a+b;\ndis(\tendfunction") get_class = ScilabCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) @@ -40,7 +43,8 @@ class ScilabEvaluationTestCases(unittest.TestCase): "\n\tc=a-b;\nendfunction") get_class = ScilabCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) @@ -52,7 +56,8 @@ class ScilabEvaluationTestCases(unittest.TestCase): "\n\tc=a;\nwhile(1==1)\nend\nendfunction") get_class = ScilabCodeEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) -- cgit From dfb9c4b5d6de17187bffd0e3c793737275202cb9 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:58:08 +0530 Subject: added form to upload files --- yaksh/templates/yaksh/add_question.html | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index 255deaa..858a8f3 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -15,19 +15,27 @@ {% block onload %} onload='javascript:textareaformat();' {% endblock %} {% block manage %} -
+ {% csrf_token %}
Summary: {{ form.summary }}{{ form.summary.errors }}
Language: {{form.language}}{{form.language.errors}}
Active: {{ form.active }}{{form.active.errors}}   Type:  {{ form.type }}{{form.type.errors}} -
Points:{{ form.points }}{{ form.points.errors }} +
Points:{{form.points }}{{ form.points.errors }}
Rendered:

Description: {{ form.description}} {{form.description.errors}}
Tags: {{ form.tags }}
Snippet: {{ form.snippet }}
Test Case Type: {{ form.test_case_type }}{{ form.test_case_type.errors }} - +
File: {{ upload_form.file_field }}{{ upload_form.file_field.errors }} + {% if uploaded_files %}
Uploaded files:
Check the box to delete or extract files
+ {% for file in uploaded_files %} +  delete  + {% if file.extract %} dont extract{% else %} + extract{% endif %}
+ {{ file.files.name }} +
+ {% endfor %}{% endif %}
{{ test_case_formset.management_form }} @@ -43,6 +51,7 @@
+
{% endblock %} -- cgit From 1785d2613b8a56b0204c257b31e721be7032515a Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:58:53 +0530 Subject: students can download files --- yaksh/templates/yaksh/question.html | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'yaksh') diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index 40d4482..24de815 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -142,7 +142,14 @@ function call_skip(url)

{{ question.summary }} (Marks : {{ question.points }})


{{ question.description|safe }}
Language: {{ question.language }}
- + {% if files %} +

Files to download for this question

+ {% for file in files %} + {% if file.question_id == question.id %} +
{{file.files.name}}
+ {% endif %} + {% endfor %} + {% endif %} {% if question.type == "code" %}

Output:


{% if error_message %} -- cgit From 8796936bc739e8da694a02286f440796e6d5ead6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:59:56 +0530 Subject: created file copier to copy files --- yaksh/copy_delete_files.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 yaksh/copy_delete_files.py (limited to 'yaksh') diff --git a/yaksh/copy_delete_files.py b/yaksh/copy_delete_files.py new file mode 100644 index 0000000..f34b944 --- /dev/null +++ b/yaksh/copy_delete_files.py @@ -0,0 +1,29 @@ +import shutil +import os +import zipfile + + +class CopyDeleteFiles(object): + + def copy_files(self, file_paths): + files = [] + for src in file_paths: + file_path, extract = src + file_name = os.path.basename(file_path) + files.append(file_name) + shutil.copy(file_path, os.getcwd()) + if extract: + unzip = zipfile.ZipFile(file_name) + for zip_files in unzip.namelist(): + files.append(zip_files) + unzip.extractall() + unzip.close() + return files + + def delete_files(self, files): + for content in files: + if os.path.exists(content): + if os.path.isfile(content): + os.remove(content) + else: + shutil.rmtree(content) -- cgit From f84e26b203c77d4cf5c78860f43448a5858803c6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 15:01:00 +0530 Subject: added new text file for test cases to test file based questions --- yaksh/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 yaksh/test.txt (limited to 'yaksh') diff --git a/yaksh/test.txt b/yaksh/test.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/yaksh/test.txt @@ -0,0 +1 @@ +2 -- cgit From df9a893b91b4adb39f01b88f6b6d96359db37f47 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:37:50 +0530 Subject: changed file module name in evaluators --- yaksh/bash_code_evaluator.py | 18 ++++++------------ yaksh/cpp_code_evaluator.py | 12 +++++------- yaksh/java_code_evaluator.py | 12 +++++------- yaksh/python_assertion_evaluator.py | 12 +++++------- yaksh/python_stdout_evaluator.py | 12 +++++------- yaksh/scilab_code_evaluator.py | 12 +++++------- 6 files changed, 31 insertions(+), 47 deletions(-) (limited to 'yaksh') diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index c602ceb..bce7f07 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -9,7 +9,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files class BashCodeEvaluator(CodeEvaluator): @@ -23,9 +23,8 @@ class BashCodeEvaluator(CodeEvaluator): # Delete the created file. super(BashCodeEvaluator, self).teardown() os.remove(self.submit_code_path) - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def check_code(self, user_answer, file_paths, test_case): """ Function validates student script using instructor script as @@ -57,10 +56,9 @@ class BashCodeEvaluator(CodeEvaluator): clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(get_ref_path, get_test_case_path) - self.files_list = [] + self.files = [] if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg @@ -123,12 +121,8 @@ class BashCodeEvaluator(CodeEvaluator): stderr=subprocess.PIPE ) proc, inst_stdout, inst_stderr = ret - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) args = [self.submit_code_path] + \ [x for x in test_case.split()] ret = self._run_command(args, diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 29b24d5..c65242d 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -8,7 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files class CppCodeEvaluator(CodeEvaluator): @@ -27,9 +27,8 @@ class CppCodeEvaluator(CodeEvaluator): os.remove(self.ref_output_path) if os.path.exists(self.user_output_path): os.remove(self.user_output_path) - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def set_file_paths(self): user_output_path = os.getcwd() + '/output' @@ -47,7 +46,7 @@ class CppCodeEvaluator(CodeEvaluator): return compile_command, compile_main def compile_code(self, user_answer, file_paths, test_case): - self.files_list = [] + self.files = [] if self.compiled_user_answer and self.compiled_test_code: return None else: @@ -55,8 +54,7 @@ class CppCodeEvaluator(CodeEvaluator): clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index dde2ec2..ff76317 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -8,7 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files class JavaCodeEvaluator(CodeEvaluator): @@ -27,9 +27,8 @@ class JavaCodeEvaluator(CodeEvaluator): os.remove(self.user_output_path) if os.path.exists(self.ref_output_path): os.remove(self.ref_output_path) - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def get_commands(self, clean_ref_code_path, user_code_directory): compile_command = 'javac {0}'.format(self.submit_code_path), @@ -44,7 +43,7 @@ class JavaCodeEvaluator(CodeEvaluator): return output_path def compile_code(self, user_answer, file_paths, test_case): - self.files_list = [] + self.files = [] if self.compiled_user_answer and self.compiled_test_code: return None else: @@ -52,8 +51,7 @@ class JavaCodeEvaluator(CodeEvaluator): clean_ref_code_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) if not isfile(clean_ref_code_path): msg = "No file at %s or Incorrect path" % clean_ref_code_path return False, msg diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 3e98f08..04a4e69 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -7,7 +7,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator, TimeoutException -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files class PythonAssertionEvaluator(CodeEvaluator): @@ -20,15 +20,13 @@ class PythonAssertionEvaluator(CodeEvaluator): def teardown(self): super(PythonAssertionEvaluator, self).teardown() # Delete the created file. - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def compile_code(self, user_answer, file_paths, test_case): - self.files_list = [] + self.files = [] if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) if self.exec_scope: return None else: diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py index fa8d6c6..8f69b24 100644 --- a/yaksh/python_stdout_evaluator.py +++ b/yaksh/python_stdout_evaluator.py @@ -8,7 +8,7 @@ from contextlib import contextmanager # local imports from code_evaluator import CodeEvaluator -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files @contextmanager @@ -29,15 +29,13 @@ class PythonStdoutEvaluator(CodeEvaluator): def teardown(self): super(PythonStdoutEvaluator, self).teardown() # Delete the created file. - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def compile_code(self, user_answer, file_paths, expected_output): - self.files_list = [] + self.files = [] if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) if hasattr(self, 'output_value'): return None else: diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 5f38087..53fa343 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -8,7 +8,7 @@ import importlib # local imports from code_evaluator import CodeEvaluator -from copy_delete_files import CopyDeleteFiles +from file_utils import copy_files, delete_files class ScilabCodeEvaluator(CodeEvaluator): @@ -22,15 +22,13 @@ class ScilabCodeEvaluator(CodeEvaluator): super(ScilabCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) - if self.files_list: - file_delete = CopyDeleteFiles() - file_delete.delete_files(self.files_list) + if self.files: + delete_files(self.files) def check_code(self, user_answer, file_paths, test_case): - self.files_list = [] + self.files = [] if file_paths: - file_copy = CopyDeleteFiles() - self.files_list = file_copy.copy_files(file_paths) + self.files = copy_files(file_paths) ref_code_path = test_case clean_ref_path, clean_test_case_path = \ self._set_test_code_file_path(ref_code_path) -- cgit From d5c7cfbf8b7f81f2cfde404602bf4e5026583bd9 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:39:54 +0530 Subject: removed delete function from models.py --- yaksh/models.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 20d8716..18565c2 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -288,13 +288,7 @@ class QuestionsFileUpload(models.Model): question = models.ForeignKey(Question, related_name="question") extract = models.BooleanField(default=False) - def delete_all_files(self, files): - for file in files: - if os.path.exists(file.files.path): - shutil.rmtree(os.path.dirname(file.files.path)) - file.delete() - - def delete_selected_files(self, files): + def delete_files(self, files): for file in files: if os.path.exists(file.files.path): os.remove(file.files.path) @@ -302,7 +296,7 @@ class QuestionsFileUpload(models.Model): os.rmdir(os.path.dirname(file.files.path)) file.delete() - def extract_files(self, files): + def set_extract_status(self, files): for file in files: if file.extract: file.extract = False -- cgit From 80453af850b5080a43eb309b50151ecc8e5f6578 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:42:44 +0530 Subject: changed extract and delete function names in views.py --- yaksh/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/views.py b/yaksh/views.py index b5de26d..33be86d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -189,7 +189,7 @@ def edit_question(request, question_id=None): if remove_files_id: files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) file = QuestionsFileUpload() - file.delete_selected_files(files) + file.delete_files(files) if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) form = FileForm(request.POST, request.FILES) @@ -201,7 +201,7 @@ def edit_question(request, question_id=None): if extract_files_id: files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) file = QuestionsFileUpload() - file.extract_files(files) + file.set_extract_status(files) if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -862,7 +862,7 @@ def show_all_questions(request): files = QuestionsFileUpload.objects.filter(question_id=question.id) if files: file = QuestionsFileUpload() - file.delete_all_files(files) + file.delete_files(files) questions.delete() if request.POST.get('upload') == 'upload': -- cgit From 21f53d77a83ce683ad64b2031cd2b8b7aba05c26 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:43:35 +0530 Subject: changed module name from copy_delete_files to file_utils --- yaksh/file_utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 yaksh/file_utils.py (limited to 'yaksh') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py new file mode 100644 index 0000000..1e03f1f --- /dev/null +++ b/yaksh/file_utils.py @@ -0,0 +1,33 @@ +import shutil +import os +import zipfile + + +def copy_files(file_paths): + """ Copy Files to current directory, takes + tuple with file paths and extract status""" + + files = [] + for src in file_paths: + file_path, extract = src + file_name = os.path.basename(file_path) + files.append(file_name) + shutil.copy(file_path, os.getcwd()) + if extract: + unzip = zipfile.ZipFile(file_name) + for zip_files in unzip.namelist(): + files.append(zip_files) + unzip.extractall() + unzip.close() + return files + + +def delete_files(files): + """ Delete Files from current directory """ + + for content in files: + if os.path.exists(content): + if os.path.isfile(content): + os.remove(content) + else: + shutil.rmtree(content) -- cgit From 6a62d23842b64bbc20bda8ddca718c58a0e422cb Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:47:16 +0530 Subject: deleted copy_delete_files.py --- yaksh/copy_delete_files.py | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 yaksh/copy_delete_files.py (limited to 'yaksh') diff --git a/yaksh/copy_delete_files.py b/yaksh/copy_delete_files.py deleted file mode 100644 index f34b944..0000000 --- a/yaksh/copy_delete_files.py +++ /dev/null @@ -1,29 +0,0 @@ -import shutil -import os -import zipfile - - -class CopyDeleteFiles(object): - - def copy_files(self, file_paths): - files = [] - for src in file_paths: - file_path, extract = src - file_name = os.path.basename(file_path) - files.append(file_name) - shutil.copy(file_path, os.getcwd()) - if extract: - unzip = zipfile.ZipFile(file_name) - for zip_files in unzip.namelist(): - files.append(zip_files) - unzip.extractall() - unzip.close() - return files - - def delete_files(self, files): - for content in files: - if os.path.exists(content): - if os.path.isfile(content): - os.remove(content) - else: - shutil.rmtree(content) -- cgit From 2a5eb7eaf366dc6c91b8955a288c7f8371f0e682 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 17:10:14 +0530 Subject: rebase changes --- yaksh/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'yaksh') diff --git a/yaksh/views.py b/yaksh/views.py index 33be86d..cba9d58 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,8 @@ import json import shutil # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload, + has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ -- cgit From 0c8ce6ec96d4d44ac8c69eb964c7a3847c74b301 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 17:30:35 +0530 Subject: removed unused import and changed indentation --- yaksh/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/views.py b/yaksh/views.py index cba9d58..c659369 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -22,11 +22,11 @@ import pytz from taggit.models import Tag from itertools import chain import json -import shutil + # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload, - has_profile +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload,\ + has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ -- cgit From 4c6b1eb85f82536bfdcdee9cc7170a4a51cf5bf4 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:51:42 +0530 Subject: added validation to check zip file --- yaksh/file_utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'yaksh') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 1e03f1f..8f6f6e5 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -13,7 +13,7 @@ def copy_files(file_paths): file_name = os.path.basename(file_path) files.append(file_name) shutil.copy(file_path, os.getcwd()) - if extract: + if extract and zipfile.is_zipfile(file_name): unzip = zipfile.ZipFile(file_name) for zip_files in unzip.namelist(): files.append(zip_files) @@ -25,9 +25,9 @@ def copy_files(file_paths): def delete_files(files): """ Delete Files from current directory """ - for content in files: - if os.path.exists(content): - if os.path.isfile(content): - os.remove(content) + for file in files: + if os.path.exists(file): + if os.path.isfile(file): + os.remove(file) else: - shutil.rmtree(content) + shutil.rmtree(file) -- cgit From 5507ac28187fbb2605334bce618cc543725e8334 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:53:08 +0530 Subject: changes in file upload class --- yaksh/models.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 18565c2..73d4b27 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -221,9 +221,9 @@ class Question(models.Model): question_data['test_case_data'] = test_case_data question_data['user_answer'] = user_answer - files = QuestionsFileUpload.objects.filter(question=self) + files = FileUpload.objects.filter(question=self) if files: - question_data['file_paths'] = [(file.files.path, file.extract) + question_data['file_paths'] = [(file.file.path, file.extract) for file in files] return json.dumps(question_data) @@ -283,26 +283,24 @@ class Question(models.Model): ############################################################################### -class QuestionsFileUpload(models.Model): - files = models.FileField(upload_to=get_upload_dir, blank=True) +class FileUpload(models.Model): + file = models.FileField(upload_to=get_upload_dir, blank=True) question = models.ForeignKey(Question, related_name="question") extract = models.BooleanField(default=False) - def delete_files(self, files): - for file in files: - if os.path.exists(file.files.path): - os.remove(file.files.path) - if os.listdir(os.path.dirname(file.files.path)) == []: - os.rmdir(os.path.dirname(file.files.path)) - file.delete() - - def set_extract_status(self, files): - for file in files: - if file.extract: - file.extract = False - else: - file.extract = True - file.save() + def remove(self): + if os.path.exists(self.file.path): + os.remove(self.file.path) + if os.listdir(os.path.dirname(self.file.path)) == []: + os.rmdir(os.path.dirname(self.file.path)) + self.delete() + + def set_extract_status(self): + if self.extract: + self.extract = False + else: + self.extract = True + self.save() ############################################################################### -- cgit From 26157fd30edf834aa53c1bda58cdf277255d1fc4 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:54:36 +0530 Subject: changes in views questions file upload --- yaksh/views.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'yaksh') diff --git a/yaksh/views.py b/yaksh/views.py index c659369..e1ec44e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,7 @@ import json # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload,\ +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ @@ -160,7 +160,7 @@ def add_question(request): files = request.FILES.getlist('file_field') if files: for file in files: - QuestionsFileUpload.objects.get_or_create(question=new_question, files=file) + FileUpload.objects.get_or_create(question=new_question, file=file) return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id)) else: return my_render_to_response('yaksh/add_question.html', @@ -188,9 +188,9 @@ def edit_question(request, question_id=None): if request.method == "POST" and 'delete_files' in request.POST: remove_files_id = request.POST.getlist('clear') if remove_files_id: - files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) - file = QuestionsFileUpload() - file.delete_files(files) + files = FileUpload.objects.filter(id__in=remove_files_id) + for file in files: + file.remove() if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) form = FileForm(request.POST, request.FILES) @@ -198,11 +198,11 @@ def edit_question(request, question_id=None): extract_files_id = request.POST.getlist('extract') if files: for file in files: - QuestionsFileUpload.objects.get_or_create(question=question_instance, files=file) + FileUpload.objects.get_or_create(question=question_instance, file=file) if extract_files_id: - files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) - file = QuestionsFileUpload() - file.set_extract_status(files) + files = FileUpload.objects.filter(id__in=extract_files_id) + for file in files: + file.set_extract_status() if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -220,7 +220,7 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance) - uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) + uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, @@ -236,7 +236,7 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(instance=question_instance) - uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) + uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, @@ -435,7 +435,7 @@ def show_question(request, question, paper, error_message=None): reason='Your time is up!' return complete(request, reason, paper.attempt_number, paper.question_paper.id) test_cases = question.get_test_cases() - files = QuestionsFileUpload.objects.filter(question_id=question.id) + files = FileUpload.objects.filter(question_id=question.id) context = {'question': question, 'paper': paper, 'error_message': error_message, 'test_cases': test_cases, 'files': files, 'last_attempt': question.snippet.encode('unicode-escape')} @@ -859,11 +859,10 @@ def show_all_questions(request): data = request.POST.getlist('question') if data is not None: questions = Question.objects.filter(id__in=data, user_id=user.id) - for question in questions: - files = QuestionsFileUpload.objects.filter(question_id=question.id) - if files: - file = QuestionsFileUpload() - file.delete_files(files) + files = FileUpload.objects.filter(question_id__in=questions) + if files: + for file in files: + file.remove() questions.delete() if request.POST.get('upload') == 'upload': -- cgit From 83bee9d89e163e98504c8aa210ce60200bd1cd1d Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:55:35 +0530 Subject: changes in templates to get question file url and name --- yaksh/templates/yaksh/add_question.html | 2 +- yaksh/templates/yaksh/question.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh') diff --git a/yaksh/templates/yaksh/add_question.html b/yaksh/templates/yaksh/add_question.html index 858a8f3..f003256 100644 --- a/yaksh/templates/yaksh/add_question.html +++ b/yaksh/templates/yaksh/add_question.html @@ -33,7 +33,7 @@  delete  {% if file.extract %} dont extract{% else %} extract{% endif %}
- {{ file.files.name }} + {{ file.file.name }}
{% endfor %}{% endif %}
diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index 24de815..2d52009 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -146,7 +146,7 @@ function call_skip(url)

Files to download for this question

{% for file in files %} {% if file.question_id == question.id %} -
{{file.files.name}}
+
{{file.file.name}}
{% endif %} {% endfor %} {% endif %} -- cgit