From 2955c57b10c33a12b9c6def60169ee2105ca20e4 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 10 Mar 2017 11:10:09 +0530 Subject: Changes in models and views - Display error message if questions_dump.json is not found in zip file - Provide a sample zip file to for easy zip file creation --- yaksh/models.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 398f508..0a84d4e 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -402,6 +402,9 @@ class Question(models.Model): with open(json_file, 'r') as q_file: questions_list = q_file.read() self.load_questions(questions_list, user, file_path, files) + return "Questions Uploaded Successfully" + else: + return "Please upload zip file with questions_dump.json in it." def create_demo_questions(self, user): zip_file_path = os.path.join( -- cgit From b37244f2c6573b0f16b9dcea614400ee1ae24cf8 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 16 Mar 2017 16:43:12 +0530 Subject: Changes in models views and urls - Handle Json parsing error - Remove Sample file download from views and urls --- yaksh/models.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 0a84d4e..dc015d5 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -318,17 +318,19 @@ class Question(models.Model): file_names = question.pop('files') test_cases = question.pop('testcase') que, result = Question.objects.get_or_create(**question) - if file_names: - que._add_files_to_db(file_names, file_path) - for test_case in test_cases: - test_case_type = test_case.pop('test_case_type') - model_class = get_model_class(test_case_type) - new_test_case, obj_create_status = \ - model_class.objects.get_or_create( - question=que, **test_case - ) - new_test_case.type = test_case_type - new_test_case.save() + if not result: + if file_names: + que._add_files_to_db(file_names, file_path) + for test_case in test_cases: + test_case_type = test_case.pop('test_case_type') + model_class = get_model_class(test_case_type) + new_test_case, obj_create_status = \ + model_class.objects.get_or_create( + question=que, **test_case + ) + new_test_case.type = test_case_type + new_test_case.save() + if files_list: delete_files(files_list, file_path) @@ -401,7 +403,11 @@ class Question(models.Model): 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, file_path, files) + try: + self.load_questions(questions_list, user, file_path, files) + except ValueError: + return "Syntax Error in Json. Please check your json file." + return "Questions Uploaded Successfully" else: return "Please upload zip file with questions_dump.json in it." -- cgit From 69ab40cc3d4d769d0d3f1c783f950cac52f7935e Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 16 Mar 2017 17:15:38 +0530 Subject: Fix test cases for loading questions --- yaksh/models.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index dc015d5..c10f953 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -318,18 +318,17 @@ class Question(models.Model): file_names = question.pop('files') test_cases = question.pop('testcase') que, result = Question.objects.get_or_create(**question) - if not result: - if file_names: - que._add_files_to_db(file_names, file_path) - for test_case in test_cases: - test_case_type = test_case.pop('test_case_type') - model_class = get_model_class(test_case_type) - new_test_case, obj_create_status = \ - model_class.objects.get_or_create( - question=que, **test_case - ) - new_test_case.type = test_case_type - new_test_case.save() + if file_names: + que._add_files_to_db(file_names, file_path) + for test_case in test_cases: + test_case_type = test_case.pop('test_case_type') + model_class = get_model_class(test_case_type) + new_test_case, obj_create_status = \ + model_class.objects.get_or_create( + question=que, **test_case + ) + new_test_case.type = test_case_type + new_test_case.save() if files_list: delete_files(files_list, file_path) -- cgit From 8547c5bfe7be64256b412f484ab6a9f60628ef06 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 16 Mar 2017 19:00:48 +0530 Subject: Handle json file syntax errors in uploading question --- yaksh/models.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index c10f953..80e215e 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -15,6 +15,8 @@ except ImportError: from io import BytesIO as string_io import pytz import os +import sys +import traceback import stat from os.path import join, exists import shutil @@ -312,7 +314,13 @@ class Question(models.Model): def load_questions(self, questions_list, user, file_path=None, files_list=None): - questions = json.loads(questions_list) + try: + questions = json.loads(questions_list) + except json.decoder.JSONDecodeError: + exc_type, exc_value, exc_tb = sys.exc_info() + tb_list = traceback.format_exception(exc_type, exc_value, exc_tb) + msg = "Error Parsing Json: {0}".format(tb_list[-1]) + return msg for question in questions: question['user'] = user file_names = question.pop('files') @@ -329,9 +337,7 @@ class Question(models.Model): ) new_test_case.type = test_case_type new_test_case.save() - - if files_list: - delete_files(files_list, file_path) + return "Questions Uploaded Successfully" def get_test_cases(self, **kwargs): tc_list = [] @@ -399,17 +405,17 @@ class Question(models.Model): def read_json(self, file_path, user, files=None): json_file = os.path.join(file_path, "questions_dump.json") + msg = "" if os.path.exists(json_file): with open(json_file, 'r') as q_file: questions_list = q_file.read() - try: - self.load_questions(questions_list, user, file_path, files) - except ValueError: - return "Syntax Error in Json. Please check your json file." - - return "Questions Uploaded Successfully" + msg = self.load_questions(questions_list, user, file_path, files) else: - return "Please upload zip file with questions_dump.json in it." + msg = "Please upload zip file with questions_dump.json in it." + + if files: + delete_files(files, file_path) + return msg def create_demo_questions(self, user): zip_file_path = os.path.join( -- cgit From 6c53c90ee5dd7184889fd3f99130b194e75fef4c Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 17 Mar 2017 13:57:21 +0530 Subject: Change Exception message for load questions --- yaksh/models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 80e215e..970f136 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -316,10 +316,8 @@ class Question(models.Model): files_list=None): try: questions = json.loads(questions_list) - except json.decoder.JSONDecodeError: - exc_type, exc_value, exc_tb = sys.exc_info() - tb_list = traceback.format_exception(exc_type, exc_value, exc_tb) - msg = "Error Parsing Json: {0}".format(tb_list[-1]) + except ValueError as exc_msg: + msg = "Error Parsing Json: {0}".format(exc_msg) return msg for question in questions: question['user'] = user -- cgit