|
@@ -62,78 +62,77 @@ class PractiseLogViewSet(CustomModelViewSet):
|
|
|
now_practise = request.data.get('now_practise') # 当前提交的练习题。第一题或继续答题时,该参数为空
|
|
|
answers = json.loads(request.data.get('answers')) # 答案, 第一题或继续答题时,该参数为空
|
|
|
next_number = int(request.data.get('next_number')) or 0 # 下一题序号, 第一题提交为空
|
|
|
- # next_type = int(request.data.get('next_type')) # 下一题题型,默认1为单选题
|
|
|
- button_type = request.data.get('button_type') # 上一题、下一题按钮类型,next下一题,previous上一题
|
|
|
next_practise = request.data.get('next_practise') # 下一题id,首次加载第一题,传空
|
|
|
try:
|
|
|
with transaction.atomic():
|
|
|
instance = self.get_object()
|
|
|
# 点击下一题,保存、判断当前习题答案
|
|
|
- if now_practise and button_type == 'next':
|
|
|
- if len(answers) == 0:
|
|
|
- raise CustomError('请选择该题的答案!')
|
|
|
+ if now_practise:
|
|
|
now_question = ExamQuestion.objects.filter(id=now_practise).first()
|
|
|
if not now_question:
|
|
|
raise CustomError('提交的习题有误,请刷新重试!')
|
|
|
- answer_log, create = PractiseAnswerLog.objects.get_or_create(main=instance, question=now_question,
|
|
|
- status=PractiseAnswerLog.WRONG)
|
|
|
- if now_question.type == ExamQuestion.SINGLE:
|
|
|
- # 单选
|
|
|
- if len(answers) != 1:
|
|
|
- raise CustomError(u'请提交一个答案!')
|
|
|
- answer = answers[0]
|
|
|
- PractiseAnswerOptionLog.objects.create(main=answer_log, option_id=answer)
|
|
|
- right = ExamQuestionOption.objects.filter(main=now_question, id=answer, right=True, )
|
|
|
- if right:
|
|
|
- instance.right_count += 1
|
|
|
- answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
+ if len(answers) > 0:
|
|
|
+ answer_log, create = PractiseAnswerLog.objects.get_or_create(main=instance, question=now_question,)
|
|
|
+ if now_question.type == ExamQuestion.SINGLE:
|
|
|
+ # 单选
|
|
|
+ PractiseAnswerOptionLog.objects.filter(main=answer_log).delete()
|
|
|
+ answer = answers[0]
|
|
|
+ PractiseAnswerOptionLog.objects.create(main=answer_log, option_id=answer)
|
|
|
+ right = ExamQuestionOption.objects.filter(main=now_question, id=answer, right=True, delete=False )
|
|
|
+ if right:
|
|
|
+ instance.right_count += 1
|
|
|
+ answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
+ else:
|
|
|
+ answer_log.status = PractiseAnswerLog.WRONG
|
|
|
+ instance.wrong_count += 1
|
|
|
+ elif now_question.type == ExamQuestion.MULTIPLE:
|
|
|
+ # 多选
|
|
|
+ answers.sort()
|
|
|
+ PractiseAnswerOptionLog.objects.filter(main=answer_log).delete()
|
|
|
+ for a in answers:
|
|
|
+ PractiseAnswerOptionLog.objects.create(main=answer_log, option_id=a)
|
|
|
+ right = ExamQuestionOption.objects.filter(main=now_question, right=True,
|
|
|
+ delete=False).values_list('id', flat=True)
|
|
|
+ list(right).sort()
|
|
|
+ if answers == right:
|
|
|
+ answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
+ instance.right_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = PractiseAnswerLog.WRONG
|
|
|
+ instance.wrong_count += 1
|
|
|
+ elif now_question.type == ExamQuestion.FILL:
|
|
|
+ # 填空
|
|
|
+ answers_len = len(answers)
|
|
|
+ right = 1
|
|
|
+ PractiseAnswerFillLog.objects.filter(main=answer_log).delete()
|
|
|
+ for a in range(0, answers_len):
|
|
|
+ PractiseAnswerFillLog.objects.create(main=answer_log, content=answers[a], order=a+1)
|
|
|
+ right_answer = ExamQuestionFill.objects.filter(main=now_question, content=answers[a], order=a+1)
|
|
|
+ if not right_answer:
|
|
|
+ # 有一个填空错误,整题错误
|
|
|
+ right = 0
|
|
|
+ if right:
|
|
|
+ answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
+ instance.right_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = PractiseAnswerLog.WRONG
|
|
|
+ instance.wrong_count += 1
|
|
|
else:
|
|
|
- instance.wrong_count += 1
|
|
|
- elif now_question.type == ExamQuestion.MULTIPLE:
|
|
|
- # 多选
|
|
|
- if len(answers) <= 1:
|
|
|
- raise CustomError(u'请提交两个以上答案!')
|
|
|
- answers.sort()
|
|
|
- for a in answers:
|
|
|
- PractiseAnswerOptionLog.objects.create(main=answer_log, option_id=a)
|
|
|
- right = ExamQuestionOption.objects.filter(main=now_question, right=True,
|
|
|
- delete=False).values_list('id', flat=True)
|
|
|
- right.sort()
|
|
|
- if answers == right:
|
|
|
- answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
- instance.right_count += 1
|
|
|
- else:
|
|
|
- instance.wrong_count += 1
|
|
|
- elif now_question.type == ExamQuestion.FILL:
|
|
|
- # 填空
|
|
|
- answers_len = len(answers)
|
|
|
- right = 1
|
|
|
- for a in range(1, answers_len):
|
|
|
- PractiseAnswerFillLog.objects.create(main=answer_log, content=answers[a], order=a)
|
|
|
- right_answer = ExamQuestionFill.objects.filter(main=now_question, content=answers[a], order=a)
|
|
|
- if not right_answer:
|
|
|
- # 有一个填空错误,整题错误
|
|
|
- right = 0
|
|
|
- if right:
|
|
|
- answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
- instance.right_count += 1
|
|
|
- else:
|
|
|
- instance.wrong_count += 1
|
|
|
- else:
|
|
|
- # 判断
|
|
|
- if now_question.judgment == (answers[0] == 1):
|
|
|
- answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
- instance.right_count += 1
|
|
|
- else:
|
|
|
- instance.wrong_count += 1
|
|
|
- instance.total_count += 1
|
|
|
- # 第一题
|
|
|
- if next_number == 1:
|
|
|
- instance.begin_answer = answer_log
|
|
|
- instance.end_answer = answer_log
|
|
|
- instance.submit_time = timezone.now()
|
|
|
- instance.save()
|
|
|
- answer_log.save()
|
|
|
+ # 判断
|
|
|
+ if now_question.judgment == (answers[0] == 1):
|
|
|
+ answer_log.status = PractiseAnswerLog.RIGHT
|
|
|
+ instance.right_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = PractiseAnswerLog.WRONG
|
|
|
+ instance.wrong_count += 1
|
|
|
+ instance.total_count += 1
|
|
|
+ # 第一题
|
|
|
+ if not instance.begin_answer:
|
|
|
+ instance.begin_answer = answer_log
|
|
|
+ instance.end_answer = answer_log
|
|
|
+ instance.submit_time = timezone.now()
|
|
|
+ instance.save()
|
|
|
+ answer_log.save()
|
|
|
|
|
|
question_data = {}
|
|
|
if instance.type == PractiseLog.SUBJECT:
|
|
@@ -144,35 +143,12 @@ class PractiseLogViewSet(CustomModelViewSet):
|
|
|
|
|
|
# 返回下一题
|
|
|
if next_practise:
|
|
|
- # 循环查询4个题型的试题,只到查询到试题
|
|
|
- # question = None
|
|
|
- # while not question and next_type <= ExamQuestion.JUDGMENT:
|
|
|
- # question = questions.filter(type=int(next_type))[next_number - 1:next_number].first()
|
|
|
- # if question:
|
|
|
- # break
|
|
|
- # next_type += 1
|
|
|
- # next_number = 1
|
|
|
question = questions.filter(id=next_practise).first()
|
|
|
else:
|
|
|
+ # 首次加载,选择第一个题
|
|
|
question = questions.filter().first()
|
|
|
|
|
|
if question:
|
|
|
- # 查询到下一题,返回题目和答案
|
|
|
- # 根据下一题,查询下下一题类型
|
|
|
- # next_question = None
|
|
|
- # while not next_question and next_type <= ExamQuestion.JUDGMENT:
|
|
|
- # next_question = questions.filter(type=int(next_type))[next_number:next_number + 1].first()
|
|
|
- # if next_question:
|
|
|
- # if next_number == 0:
|
|
|
- # next_number = 1
|
|
|
- # break
|
|
|
- # next_type += 1
|
|
|
- # next_number = 0
|
|
|
- # next_question = questions.filter()[next_number:next_number + 1]
|
|
|
- # if next_question:
|
|
|
- # next_number += 1
|
|
|
- # else:
|
|
|
- # next_number = 0
|
|
|
question_data = {
|
|
|
'id': question.id,
|
|
|
'title': question.title,
|
|
@@ -185,12 +161,12 @@ class PractiseLogViewSet(CustomModelViewSet):
|
|
|
item1 = {
|
|
|
'id': 1,
|
|
|
'content': '正确',
|
|
|
- 'answer': True if answer_log.status == PractiseAnswerLog.RIGHT else False
|
|
|
+ 'answer': True if answer_log and answer_log.status == PractiseAnswerLog.RIGHT else False
|
|
|
}
|
|
|
item0 = {
|
|
|
'id': 0,
|
|
|
'content': '错误',
|
|
|
- 'answer': True if answer_log.status == PractiseAnswerLog.WRONG else False
|
|
|
+ 'answer': True if answer_log and answer_log.status == PractiseAnswerLog.WRONG else False
|
|
|
}
|
|
|
question_data['option'].append(item1)
|
|
|
question_data['option'].append(item0)
|
|
@@ -248,7 +224,7 @@ class PractiseLogViewSet(CustomModelViewSet):
|
|
|
)
|
|
|
# 判断题
|
|
|
judgment_questions_list = []
|
|
|
- for judgment in questions.filter(type=ExamQuestion.MULTIPLE):
|
|
|
+ for judgment in questions.filter(type=ExamQuestion.JUDGMENT):
|
|
|
answer_log = PractiseAnswerLog.objects.filter(main=instance, question_id=judgment)
|
|
|
judgment_questions_list.append(
|
|
|
{
|