From 00f01038b2a4653d87ca2529009fa873f6a4a26d Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Wed, 11 Jul 2018 18:48:31 +0530 Subject: Support for multiple yaml files --- yaksh/file_utils.py | 2 +- yaksh/fixtures/demo_questions.zip | Bin 3055 -> 3230 bytes yaksh/models.py | 30 ++++++----- yaksh/templates/yaksh/showquestions.html | 29 +++++++++- yaksh/test_models.py | 4 +- yaksh/test_views.py | 89 ++++++++++++++++++++++++++++++- 6 files changed, 136 insertions(+), 18 deletions(-) diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 6c3fd5d..7c31c70 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -46,7 +46,7 @@ def extract_files(zip_file, path=None): if path: extract_path = path else: - extract_path = tempfile.gettempdir() + extract_path = tempfile.mkdtemp() zip_file.extractall(extract_path) zip_file.close() return zfiles, extract_path diff --git a/yaksh/fixtures/demo_questions.zip b/yaksh/fixtures/demo_questions.zip index 4e86485..1618341 100644 Binary files a/yaksh/fixtures/demo_questions.zip and b/yaksh/fixtures/demo_questions.zip differ diff --git a/yaksh/models.py b/yaksh/models.py index 5d17dba..3e4644b 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -13,6 +13,8 @@ from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone from django.core.files import File +import glob + try: from StringIO import StringIO as string_io except ImportError: @@ -1020,7 +1022,7 @@ class Question(models.Model): tags = question.pop('tags') if 'tags' in question else None test_cases = question.pop('testcase') que, result = Question.objects.get_or_create(**question) - if file_names: + if file_names and file_path: que._add_files_to_db(file_names, file_path) if tags: que.tags.add(*tags) @@ -1089,13 +1091,15 @@ class Question(models.Model): files = FileUpload.objects.filter(question=self) files_list = [] for f in files: - zip_file.write(f.file.path, (os.path.basename(f.file.path))) + zip_file.write(f.file.path, os.path.join("additional_files", + os.path.basename(f.file.path)) + ) files_list.append(((os.path.basename(f.file.path)), f.extract)) return files_list def _add_files_to_db(self, file_names, path): for file_name, extract in file_names: - q_file = os.path.join(path, file_name) + q_file = glob.glob(os.path.join(path, "**", file_name))[0] if os.path.exists(q_file): que_file = open(q_file, 'rb') # Converting to Python file object with @@ -1130,16 +1134,16 @@ class Question(models.Model): shutil.rmtree(tmp_file_path) def read_yaml(self, file_path, user, files=None): - yaml_file = os.path.join(file_path, "questions_dump.yaml") - msg = "" - if os.path.exists(yaml_file): - with open(yaml_file, 'r') as q_file: - questions_list = q_file.read() - msg = self.load_questions(questions_list, user, - file_path, files - ) - else: - msg = "Please upload zip file with questions_dump.yaml in it." + msg = "Failed to upload Questions" + for ext in ["yaml", "yml"]: + for yaml_file in glob.glob( + os.path.join(file_path, "*.{0}".format(ext))): + if os.path.exists(yaml_file): + with open(yaml_file, 'r') as q_file: + questions_list = q_file.read() + msg = self.load_questions(questions_list, user, + file_path, files + ) if files: delete_files(files, file_path) diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index 4240b2e..06c088c 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -20,8 +20,35 @@
You can upload question files the following ways - +
One can upload Yaml file with extensions .yaml or .yml. Please note + that you cannot upload files associated to a question. Yaml file can + have any name. +
+One can also upload zip with the following zip structure - +
+ .zip + |-- .yaml or .yml + |-- .yaml or .yml + |-- folder1 + | |-- Files required by questions + |-- folder2 + | |-- Files required by questions ++
+ Click here to download a sample YAML, edit and upload it +
+