diff options
-rw-r--r-- | yaksh/bash_code_evaluator.py | 2 | ||||
-rw-r--r-- | yaksh/file_utils.py | 27 | ||||
-rw-r--r-- | yaksh/models.py | 47 | ||||
-rw-r--r-- | yaksh/views.py | 4 |
4 files changed, 48 insertions, 32 deletions
diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index e148fa8..e4b961c 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -22,10 +22,10 @@ class BashCodeEvaluator(CodeEvaluator): def teardown(self): # Delete the created file. - super(BashCodeEvaluator, self).teardown() os.remove(self.submit_code_path) if self.files: delete_files(self.files) + super(BashCodeEvaluator, self).teardown() def check_code(self, user_answer, file_paths, test_case): """ Function validates student script using instructor script as diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index afcf9e8..ba3ead0 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -1,7 +1,8 @@ import shutil import os import zipfile - +import tempfile +from online_test.settings import OUTPUT_DIR def copy_files(file_paths): """ Copy Files to current directory, takes @@ -14,16 +15,19 @@ def copy_files(file_paths): files.append(file_name) shutil.copy(file_path, os.getcwd()) if extract: - z_files = extract_files(file_name) + z_files, path = extract_files(file_name, OUTPUT_DIR) for file in z_files: files.append(file) return files -def delete_files(files): - """ Delete Files from current directory """ - - for file in files: +def delete_files(files, file_path=None): + """ Delete Files from directory """ + for file_name in files: + if file_path: + file = os.path.join(file_path, file_name) + else: + file = file_name if os.path.exists(file): if os.path.isfile(file): os.remove(file) @@ -31,13 +35,18 @@ def delete_files(files): shutil.rmtree(file) -def extract_files(zip_file): +def extract_files(zip_file, path=None): + """ extract files from zip """ zfiles = [] if zipfile.is_zipfile(zip_file): zip_file = zipfile.ZipFile(zip_file, 'r') for z_file in zip_file.namelist(): zfiles.append(z_file) - zip_file.extractall() + if path: + extract_path = path + else: + extract_path = tempfile.gettempdir() + zip_file.extractall(extract_path) zip_file.close() - return zfiles + return zfiles, extract_path diff --git a/yaksh/models.py b/yaksh/models.py index f098cd2..95683c5 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -22,7 +22,7 @@ from os.path import join, abspath, dirname, exists import shutil import zipfile import tempfile -from .file_utils import extract_files +from .file_utils import extract_files, delete_files from yaksh.xmlrpc_clients import code_server from django.conf import settings @@ -174,7 +174,7 @@ class Course(models.Model): demo_ques = Question() demo_ques.create_demo_questions(user) demo_que_ppr = QuestionPaper() - demo_que_ppr.create_demo_quiz_ppr(demo_quiz) + demo_que_ppr.create_demo_quiz_ppr(demo_quiz, user) success = True else: success = False @@ -290,7 +290,7 @@ class Question(models.Model): question._add_json_to_zip(zip_file, questions_dict) return zip_file_name - def load_questions(self, questions_list, user): + def load_questions(self, questions_list, user, file_path=None, files_list=None): questions = json.loads(questions_list) for question in questions: question['user'] = user @@ -298,10 +298,12 @@ class Question(models.Model): test_cases = question.pop('testcase') que, result = Question.objects.get_or_create(**question) if file_names: - que._add_files_to_db(file_names) + que._add_files_to_db(file_names, file_path) model_class = get_model_class(que.test_case_type) for test_case in test_cases: model_class.objects.get_or_create(question=que, **test_case) + if files_list: + delete_files(files_list, file_path) def get_test_cases(self, **kwargs): test_case_ctype = ContentType.objects.get(app_label="yaksh", @@ -333,15 +335,18 @@ class Question(models.Model): files_list.append(((os.path.basename(f.file.path)), f.extract)) return files_list - def _add_files_to_db(self, file_names): + def _add_files_to_db(self, file_names, path): for file_name, extract in file_names: - que_file = open(file_name, 'r') - #Converting to Python file object with some Django-specific additions - django_file = File(que_file) - FileUpload.objects.get_or_create(file=django_file, - question=self, - extract=extract) - os.remove(file_name) + q_file = os.path.join(path, file_name) + if os.path.exists(q_file): + que_file = open(q_file, 'r') + #Converting to Python file object with + # some Django-specific additions + django_file = File(que_file) + file_upload = FileUpload() + file_upload.question = self + file_upload.extract = extract + file_upload.file.save(file_name, django_file, save=True) def _add_json_to_zip(self, zip_file, q_dict): json_data = json.dumps(q_dict, indent=2) @@ -353,18 +358,19 @@ class Question(models.Model): zip_file.close() shutil.rmtree(tmp_file_path) - def read_json(self, json_file, user): + def read_json(self, file_path, user, files=None): + json_file = os.path.join(file_path, "questions_dump.json") if os.path.exists(json_file): with open(json_file, 'r') as q_file: questions_list = q_file.read() - self.load_questions(questions_list, user) - os.remove(json_file) + print(questions_list) + self.load_questions(questions_list, user, file_path, files) def create_demo_questions(self, user): zip_file_path = os.path.join(os.getcwd(), 'yaksh', - 'fixtures', 'demo_questions.zip') - extract_files(zip_file_path) - self.read_json("questions_dump.json", user) + 'fixtures', 'demo_questions.zip') + files, extract_path = extract_files(zip_file_path) + self.read_json(extract_path, user, files) def __str__(self): @@ -674,13 +680,14 @@ class QuestionPaper(models.Model): prerequisite = self._get_prequisite_paper() return prerequisite._is_questionpaper_passed(user) - def create_demo_quiz_ppr(self, demo_quiz): + def create_demo_quiz_ppr(self, demo_quiz, user): question_paper = QuestionPaper.objects.create(quiz=demo_quiz, total_marks=5.0, shuffle_questions=True ) questions = Question.objects.filter(active=True, - summary="Yaksh Demo Question") + summary="Yaksh Demo Question", + user=user) # add fixed set of questions to the question paper for question in questions: question_paper.fixed_questions.add(question) diff --git a/yaksh/views.py b/yaksh/views.py index 049788a..611403d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -929,8 +929,8 @@ def show_all_questions(request): file_name = questions_file.name.split('.') if file_name[-1] == "zip": ques = Question() - extract_files(questions_file) - ques.read_json("questions_dump.json", user) + files, extract_path = extract_files(questions_file) + ques.read_json(extract_path, user, files) else: message = "Please Upload a ZIP file" context['message'] = message |