예제 #1
0
파일: views.py 프로젝트: grv07/QnA
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)
예제 #2
0
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.'
        })
예제 #3
0
파일: answer_engine.py 프로젝트: grv07/QnA
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.'})