def load_questions_xml(filename): """Load questions from the given XML file.""" q_bank = parse(filename).getElementsByTagName("question") for question in q_bank: summary_node = question.getElementsByTagName("summary")[0] summary = (summary_node.childNodes[0].data).strip() desc_node = question.getElementsByTagName("description")[0] description = (desc_node.childNodes[0].data).strip() type_node = question.getElementsByTagName("type")[0] type = (type_node.childNodes[0].data).strip() points_node = question.getElementsByTagName("points")[0] points = float((points_node.childNodes[0].data).strip()) \ if points_node else 1.0 test_node = question.getElementsByTagName("test")[0] test = decode_html((test_node.childNodes[0].data).strip()) opt_node = question.getElementsByTagName("options")[0] opt = decode_html((opt_node.childNodes[0].data).strip()) new_question = Question(summary=summary, description=description, points=points, options=opt, type=type, test=test) new_question.save()
class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, test='Test Cases', snippet='def myfunc()') self.question.save() self.question.tags.add('python', 'function') def test_question(self): """ Test question """ self.assertEqual(self.question.summary, 'Demo question') self.assertEqual(self.question.language, 'Python') self.assertEqual(self.question.type, 'Code') self.assertFalse(self.question.options) self.assertEqual(self.question.description, 'Write a function') self.assertEqual(self.question.points, 1.0) self.assertTrue(self.question.active) self.assertEqual(self.question.test, 'Test Cases') self.assertEqual(self.question.snippet, 'def myfunc()') tag_list = [] for tag in self.question.tags.all(): tag_list.append(tag.name) self.assertEqual(tag_list, ['python', 'function'])
def load_questions_xml(filename): """Load questions from the given XML file.""" q_bank = parse(filename).getElementsByTagName("entry") for questions in q_bank: question_node = questions.getElementsByTagName("question")[0] question = (question_node.childNodes[0].data).strip() desc_node = questions.getElementsByTagName("description")[0] description = (desc_node.childNodes[0].data).strip() points_node = questions.getElementsByTagName("points")[0] points = float((points_node.childNodes[0].data).strip()) \ if points_node else 1.0 neg_points_node = questions.getElementsByTagName("neg_points")[0] neg_points = float((neg_points_node.childNodes[0].data).strip())\ if neg_points_node else 0.25 right_response_node = questions.getElementsByTagName("right_response")[0] right_response = decode_html((right_response_node.childNodes[0].data).strip()) opt_node = questions.getElementsByTagName("options")[0] opt = decode_html((opt_node.childNodes[0].data).strip()) new_question = Question(question=question, description=description, points=points, neg_points=neg_points, options=opt, right_response=right_response) new_question.save()
def post(self, request): desc = request.data.get("desc") true_answer = request.data.get("true_answer") exam = request.data.get("exam") choices = request.data.get("choices") score = request.data.get("score") question = Question() question.desc = desc question.true_answer = true_answer question.Exam = exam question.choices = choices question.score = score question.save() return Response(status=status.HTTP_201_CREATED)
def post(self, request): exam_id = datetime.datetime.now().strftime("EX%y%m%d%H%M%S%f") username = request.user.first_name emailid = request.user examName = request.POST['examname'] contact_no = 0 examDuration = '00:20' exam_creation_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S.%f') # with transaction: examDetail_set = ExamCreateDetails(username=username, email_id=emailid, exam_create_date=exam_creation_time, exam_id=exam_id, exam_duration=examDuration, contact_no=contact_no, exam_name=examName) # try: examDetail_set.save() for i in range(1, num_range + 1): ques = request.POST['ques' + str(i)].strip() ansa = request.POST['ans' + str(i) + 'a'].strip() ansb = request.POST['ans' + str(i) + 'b'].strip() ansc = request.POST['ans' + str(i) + 'c'].strip() ansd = request.POST['ans' + str(i) + 'd'].strip() ansCorrect = request.POST['ans' + str(i) + 'correct'].strip() question_set = Question(question_number=i, question_text=ques, exam_id_id=exam_id, exam_create_date=exam_creation_time) answer_set = Answers(question_number=i, choice_1=ansa, choice_2=ansb, choice_3=ansc, choice_4=ansd, correct_choice=ansCorrect, exam_id_id=exam_id) # if(question_set.save() & answer_set.save() & examDetail_set.save()): # return HttpResponseRedirect(reverse('createonlineexam:submit_success', args=(exam_id,))) question_set.save() answer_set.save() return render(request, 'user/exam_create_success.html', context={'exam_id': exam_id})
def save(self): summary = self.cleaned_data["summary"] description = self.cleaned_data["description"] points = self.cleaned_data['points'] test = self.cleaned_data["test"] options = self.cleaned_data['options'] language = self.cleaned_data['language'] type = self.cleaned_data["type"] active = self.cleaned_data["active"] snippet = self.cleaned_data["snippet"] new_question = Question() new_question.summary = summary new_question.description = description new_question.points = points new_question.test = test new_question.options = options new_question.language = language new_question.type = type new_question.active = active new_question.snippet = snippet new_question.save()