def create_mcq_answer(question, options): ''' Create a answer of a MCQuestion ex. options = [{'content':'text','correct':True},{'content':'text','correct':False},{'content':'text','correct':False}] ''' logger.info('QnA.services.answer_engine.create_mcq_answer') if any([op['correct'] for op in options]): for option in options: option.update({'question': question}) serializer = AnswerSerializer(data=option) if serializer.is_valid(): serializer.save() else: logger.error('QnA.services.answer_engine.create_mcq_answer ' + str(serializer.errors)) return ( False, serializer.errors, ) return ( True, None, ) else: logger.error( 'QnA.services.answer_engine.create_mcq_answer error MAX Options size=6 and please select only one option as correct ans.' ) return (False, { 'errors': 'MAX Options size=6 and please select only one option as correct ans.' })
def answers_operations(request, userid, question_id): """ Get answers to a question or update. """ if verify_user_hash(userid, request.query_params.get('hash')): logger.info('under quiz.answers_operations '+str(request.method)) try: result = {} question = Question.objects.get(pk = question_id) if request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[0][0]: answers = Answer.objects.filter(question = question) elif request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[1][0]: answer = ObjectiveQuestion.objects.get(pk=question) except Question.DoesNotExist: logger.error('under quiz.answers_operations '+str(e.args)) return Response({'errors': 'Question not found'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': if request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[0][0]: answerserializer = AnswerSerializer(answers, many = True) result['options'] = answerserializer.data elif request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[1][0]: result['correct'] = answer.correct result['content'] = question.content result['sub_category_name'] = question.sub_category.sub_category_name result['sub_category'] = question.sub_category.id return Response({ 'answers' : result }, status = status.HTTP_200_OK) elif request.method == 'PUT': if request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[0][0]: optionsContent = dict(request.data.get('optionsContent')) result['options'] = [] result['content'] = question.content for answer in answers: d = { 'correct' : False, 'content' : optionsContent[str(answer.id)], 'question' : question.id } if request.data.get('correctOption') == str(answer.id): d['correct'] = True serializer = AnswerSerializer(answer,data=d) if serializer.is_valid(): serializer.save() result['options'].append(serializer.data) elif request.query_params['que_type'] == QUESTION_TYPE_OPTIONS[1][0]: serializer = ObjectiveQuestionSerializer(answer, request.data) if serializer.is_valid(): serializer.save() result['options'].append(serializer.data) else: logger.error('under quiz.answers_operations '+str(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(result, status = status.HTTP_200_OK) logger.error('under quiz.answers_operations wrong hash') return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)
def create_mcq_answer(question, options): ''' Create a answer of a MCQuestion ex. options = [{'content':'text','correct':True},{'content':'text','correct':False},{'content':'text','correct':False}] ''' logger.info('QnA.services.answer_engine.create_mcq_answer') if any([op['correct'] for op in options]): for option in options: option.update({ 'question' : question }) serializer = AnswerSerializer(data = option) if serializer.is_valid(): serializer.save() else: logger.error('QnA.services.answer_engine.create_mcq_answer '+str(serializer.errors)) return (False, serializer.errors,) return (True, None,) else: logger.error('QnA.services.answer_engine.create_mcq_answer error MAX Options size=6 and please select only one option as correct ans.') return (False, {'errors':'MAX Options size=6 and please select only one option as correct ans.'})
def question_operations(request, userid, question_id): """ Get a single question, delete or update """ _hash = request.query_params.get('hash') if not _hash: _hash = request.data.get('hash') if verify_user_hash(userid, _hash): logger.info('under quiz.question_operations '+str(request.method)) try: question = Question.objects.get(id = question_id) except Question.DoesNotExist: logger.error('under quiz.question_operations '+str(e.args)) return Response({'errors': 'Question not found.'}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': questionserializer = QuestionSerializer(question, many = False) answers = Answer.objects.filter(question = question) answerserializer = AnswerSerializer(answers, many = True) result = dict(questionserializer.data) if question.que_type == QUESTION_TYPE_OPTIONS[2][0]: result['heading'] = Comprehension.objects.get(question = question).heading else: result['problem_type'] = MCQuestion.objects.get(question_ptr = question.id).problem_type result.update( { 'options' : answerserializer.data } ) result['sub_category_name'] = question.sub_category.sub_category_name result['sub_category_id'] = question.sub_category.id return Response({ 'question' : result }, status = status.HTTP_200_OK) elif request.method == 'PUT': result = {} if request.data.get('figure', None): if question.figure: os.remove(str(question.figure)) if question.que_type == QUESTION_TYPE_OPTIONS[1][0]: if BLANK_HTML in request.data['content']: serializer = ObjectiveQuestionSerializer(question, data = request.data) else: logger.error('under quiz.question_operations error no blank field present') return Response({ "content" : ["No blank field present.Please add one."] } , status = status.HTTP_400_BAD_REQUEST) else: serializer = QuestionSerializer(question, data = request.data) if question.que_type == QUESTION_TYPE_OPTIONS[2][0]: d = { 'heading': request.data['heading'], 'question': question.id } comprehension_serializer = ComprehensionSerializer(Comprehension.objects.get(question = question), data = d) if comprehension_serializer.is_valid(): comprehension_serializer.save() result = comprehension_serializer.data else: return Response(comprehension_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: mcq_question_serailizer = MCQuestionSerializer(MCQuestion.objects.get(question_ptr = question.id), data = request.data) result['problem_type'] = request.data.get('problem_type') if mcq_question_serailizer.is_valid(): mcq_question_serailizer.save() if serializer.is_valid(): serializer.save() if result: result.update(serializer.data) else: result = serializer.data return Response(result, status = status.HTTP_200_OK) else: logger.error('under quiz.question_operations '+str(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if question.figure: os.remove(str(question.figure)) question.delete() return Response(status=status.HTTP_204_NO_CONTENT) logger.error('under quiz.question_operations wrong hash') return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)