diff options
-rw-r--r-- | yaksh/documentation/moderator_docs/creating_question.rst | 31 | ||||
-rw-r--r-- | yaksh/models.py | 20 | ||||
-rw-r--r-- | yaksh/templates/yaksh/showquestions.html | 4 | ||||
-rw-r--r-- | yaksh/views.py | 8 |
4 files changed, 54 insertions, 9 deletions
diff --git a/yaksh/documentation/moderator_docs/creating_question.rst b/yaksh/documentation/moderator_docs/creating_question.rst index f99bf7f..94bb95c 100644 --- a/yaksh/documentation/moderator_docs/creating_question.rst +++ b/yaksh/documentation/moderator_docs/creating_question.rst @@ -264,6 +264,37 @@ Features in Question Click on the browse button. This will open up a window. Select the zip file of questions and click Ok and then click on Upload file button, questions will be uploaded and displayed on the Questions page. + Zip file should contain **questions_dump.json** from which questions will be loaded. + Zip file can contain files related to questions. + Sample entry in **questions_dump.json** is as shown below. :: + [{ + "snippet": "", + "testcase": [ + { + "test_case_args": "", + "test_case_type": "standardtestcase", + "weight": 1.0, + "test_case": "Test Case here" + }, + ], + "points": 2.0, + "description": "Question Description here", + "language": "python", + "active": true, + "type": "code", + "files": [[demo1.txt, false], [demo2.zip, true]], + "summary": "Question Summary here" + }] + + .. Note:: 1. In **files** entry in json, the list contains two items which + are filename (demo1.txt) and extract status (false) i.e file needs to extracted or not. + + 2. If there are no files then **files** entry can be empty + i.e it should be "files": []. + + 3. From sample, zip file should contain demo1.txt and demo2.zip since it is + required for question. + * **Test Questions** Select questions from the list of question displayed on the Questions page. Click on Test selected button. This will take you to a quiz with the selected questions. diff --git a/yaksh/models.py b/yaksh/models.py index f5da55f..50ecb1c 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,11 @@ 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 ValueError as exc_msg: + msg = "Error Parsing Json: {0}".format(exc_msg) + return msg for question in questions: question['user'] = user file_names = question.pop('files') @@ -329,8 +335,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 = [] @@ -398,10 +403,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() - self.load_questions(questions_list, user, file_path, files) + msg = self.load_questions(questions_list, user, file_path, files) + else: + 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( diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index 3668c9e..157b378 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -14,7 +14,8 @@ <form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ upload_form.as_p }} -<button class="btn btn-primary" type="submit" name="upload" value="upload">Upload File <span class="glyphicon glyphicon-open"></span></button> +<button class="btn btn-primary" type="submit" name="upload" value="upload"> +Upload File <span class="glyphicon glyphicon-open"></span></button> </form> {% if message %} <h4>{{ message }}</h4> @@ -22,6 +23,7 @@ {% if msg %} <h4>{{ msg }}</h4> {% endif %} +<br><br> <form name=frm action="" method="post"> {% csrf_token %} <div class="row" id="selectors"> diff --git a/yaksh/views.py b/yaksh/views.py index 63653e6..daa81eb 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -20,7 +20,6 @@ import pytz from taggit.models import Tag from itertools import chain import json -import zipfile import six # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course @@ -894,7 +893,8 @@ def show_all_questions(request): if request.POST.get('delete') == 'delete': data = request.POST.getlist('question') if data is not None: - questions = Question.objects.filter(id__in=data, user_id=user.id, active=True) + questions = Question.objects.filter(id__in=data, user_id=user.id, + active=True) for question in questions: question.active = False question.save() @@ -907,7 +907,8 @@ def show_all_questions(request): if file_name[-1] == "zip": ques = Question() files, extract_path = extract_files(questions_file) - ques.read_json(extract_path, user, files) + context['message'] = ques.read_json(extract_path, user, + files) else: message = "Please Upload a ZIP file" context['message'] = message @@ -947,7 +948,6 @@ def show_all_questions(request): return my_render_to_response('yaksh/showquestions.html', context, context_instance=ci) - @login_required def user_data(request, user_id, questionpaper_id=None): """Render user data.""" |