|
@@ -48,7 +48,6 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
with transaction.atomic():
|
|
|
exampaper = ExamPaper.getById(exampaper_id)
|
|
|
data = {
|
|
|
- 'subject': exampaper.subject.id,
|
|
|
'type': ExamPaper.MOCK,
|
|
|
'exampaper': exampaper_id,
|
|
|
'user': request.user.id,
|
|
@@ -60,10 +59,6 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
result = {
|
|
|
'exam_log': instance.id, # 模拟考试 id
|
|
|
}
|
|
|
- # 创建模拟考试答题记录
|
|
|
- paper_details = ExamPaperDetail.objects.filter(main=exampaper, delete=False)
|
|
|
- for detail in paper_details:
|
|
|
- ExamAnswerLog.objects.create(main=instance, detail=detail)
|
|
|
return response_ok(result)
|
|
|
except ValidationError as e:
|
|
|
traceback.print_exc()
|
|
@@ -74,6 +69,7 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
now_practise = request.data.get('now_practise') # 当前提交的模拟考试明细id。第一题或继续答题时,该参数为空
|
|
|
answers = json.loads(request.data.get('answers')) # 答案, 第一题或继续答题时,该参数为空
|
|
|
next_practise = request.data.get('next_practise') # 下一题id,首次加载第一题,传空
|
|
|
+
|
|
|
try:
|
|
|
with transaction.atomic():
|
|
|
instance = self.get_object()
|
|
@@ -84,11 +80,8 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
raise CustomError('提交的考试习题有误,请刷新重试!')
|
|
|
now_question = detail.question
|
|
|
if len(answers) > 0:
|
|
|
- try:
|
|
|
- answer_log = ExamAnswerLog.objects.get(main=instance, detail=detail)
|
|
|
- except ExamAnswerLog.DoesNotExist:
|
|
|
- # traceback.print_exc()
|
|
|
- raise CustomError('考试习题有误,请刷新重试!')
|
|
|
+ answer_log, create = ExamAnswerLog.objects.get_or_create(main=instance,
|
|
|
+ detail=detail, )
|
|
|
if now_question.type <= ExamQuestion.MULTIPLE:
|
|
|
# 单选、多选
|
|
|
answers.sort()
|
|
@@ -177,8 +170,7 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
flat=True)
|
|
|
single_questions_list = []
|
|
|
for single in questions.filter(question__type=ExamQuestion.SINGLE):
|
|
|
- answer_log = ExamAnswerLog.objects.filter(main=instance, detail=single).exclude(
|
|
|
- status=ExamAnswerLog.NOTDONE)
|
|
|
+ answer_log = ExamAnswerLog.objects.filter(main=instance, detail=single)
|
|
|
single_questions_list.append(
|
|
|
{
|
|
|
'question_id': single,
|
|
@@ -188,8 +180,7 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
# 多选题
|
|
|
multiple_questions_list = []
|
|
|
for multiple in questions.filter(question__type=ExamQuestion.MULTIPLE):
|
|
|
- answer_log = ExamAnswerLog.objects.filter(main=instance, detail=multiple).exclude(
|
|
|
- status=ExamAnswerLog.NOTDONE)
|
|
|
+ answer_log = ExamAnswerLog.objects.filter(main=instance, detail=multiple)
|
|
|
multiple_questions_list.append(
|
|
|
{
|
|
|
'question_id': multiple,
|
|
@@ -199,8 +190,7 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
# 填空题
|
|
|
fill_questions_list = []
|
|
|
for fill in questions.filter(question__type=ExamQuestion.FILL):
|
|
|
- answer_log = ExamAnswerLog.objects.filter(main=instance, detail=fill).exclude(
|
|
|
- status=ExamAnswerLog.NOTDONE)
|
|
|
+ answer_log = ExamAnswerLog.objects.filter(main=instance, detail=fill)
|
|
|
fill_questions_list.append(
|
|
|
{
|
|
|
'question_id': fill,
|
|
@@ -210,8 +200,7 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
# 判断题
|
|
|
judgment_questions_list = []
|
|
|
for judgment in questions.filter(question__type=ExamQuestion.JUDGMENT):
|
|
|
- answer_log = ExamAnswerLog.objects.filter(main=instance, detail=judgment).exclude(
|
|
|
- status=ExamAnswerLog.NOTDONE)
|
|
|
+ answer_log = ExamAnswerLog.objects.filter(main=instance, detail=judgment)
|
|
|
judgment_questions_list.append(
|
|
|
{
|
|
|
'question_id': judgment,
|
|
@@ -239,9 +228,13 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
instance = self.get_object()
|
|
|
|
|
|
with transaction.atomic():
|
|
|
- answer_logs = ExamAnswerLog.objects.filter(main=instance)
|
|
|
- for answer_log in answer_logs:
|
|
|
- question = answer_log.detail.question
|
|
|
+ paper_details = ExamPaperDetail.objects.filter(main=instance.exampaper, delete=False)
|
|
|
+ for detail in paper_details:
|
|
|
+ # 创建模拟考试未答题记录,如果没有找到答案记录,则该题保留未答状态
|
|
|
+ answer_log, create = ExamAnswerLog.objects.get_or_create(main=instance, detail=detail)
|
|
|
+ # answer_logs = ExamAnswerLog.objects.filter(main=instance)
|
|
|
+ # for answer_log in answer_logs:
|
|
|
+ question = detail.question
|
|
|
if question.type == ExamQuestion.SINGLE:
|
|
|
# 单选
|
|
|
answers = ExamAnswerOptionLog.objects.filter(main=answer_log).first()
|
|
@@ -256,38 +249,41 @@ class ExamLogViewSet(CustomModelViewSet):
|
|
|
# 多选
|
|
|
answers = ExamAnswerOptionLog.objects.filter(main=answer_log).order_by('option_id').values_list(
|
|
|
'option_id', flat=True)
|
|
|
- right = ExamQuestionOption.objects.filter(main=question, right=True,
|
|
|
- delete=False).values_list('id', flat=True)
|
|
|
- if list(answers) == list(right):
|
|
|
- answer_log.status = ExamAnswerLog.RIGHT
|
|
|
- instance.multiple_answer_count += 1
|
|
|
- else:
|
|
|
- answer_log.status = ExamAnswerLog.WRONG
|
|
|
- ErrorBook.add_error(question, request.user, answer_log)
|
|
|
+ if answers:
|
|
|
+ right = ExamQuestionOption.objects.filter(main=question, right=True,
|
|
|
+ delete=False).values_list('id', flat=True)
|
|
|
+ if list(answers) == list(right):
|
|
|
+ answer_log.status = ExamAnswerLog.RIGHT
|
|
|
+ instance.multiple_answer_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = ExamAnswerLog.WRONG
|
|
|
+ ErrorBook.add_error(question, request.user, answer_log)
|
|
|
elif question.type == ExamQuestion.FILL:
|
|
|
# 填空
|
|
|
fill_logs = ExamAnswerFillLog.objects.filter(main=answer_log)
|
|
|
right = True
|
|
|
- for fill_log in fill_logs:
|
|
|
- right_answer = ExamQuestionFill.objects.filter(main=question, content=fill_log.content,
|
|
|
- order=fill_log.order, delete=False)
|
|
|
- if not right_answer:
|
|
|
- right = False
|
|
|
- break
|
|
|
- if right:
|
|
|
- answer_log.status = ExamAnswerLog.RIGHT
|
|
|
- instance.fill_answer_count += 1
|
|
|
- else:
|
|
|
- answer_log.status = ExamAnswerLog.WRONG
|
|
|
- ErrorBook.add_error(question, request.user, answer_log)
|
|
|
+ if fill_logs:
|
|
|
+ for fill_log in fill_logs:
|
|
|
+ right_answer = ExamQuestionFill.objects.filter(main=question, content=fill_log.content,
|
|
|
+ order=fill_log.order, delete=False)
|
|
|
+ if not right_answer:
|
|
|
+ right = False
|
|
|
+ break
|
|
|
+ if right:
|
|
|
+ answer_log.status = ExamAnswerLog.RIGHT
|
|
|
+ instance.fill_answer_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = ExamAnswerLog.WRONG
|
|
|
+ ErrorBook.add_error(question, request.user, answer_log)
|
|
|
else:
|
|
|
# 判断
|
|
|
- if question.judgment == (answer_log.status == ExamAnswerLog.RIGHT):
|
|
|
- answer_log.status = ExamAnswerLog.RIGHT
|
|
|
- instance.judgment_answer_count += 1
|
|
|
- else:
|
|
|
- answer_log.status = ExamAnswerLog.WRONG
|
|
|
- ErrorBook.add_error(question, request.user, answer_log)
|
|
|
+ if answer_log.status != ExamAnswerLog.NOTDONE:
|
|
|
+ if question.judgment == (answer_log.status == ExamAnswerLog.RIGHT):
|
|
|
+ answer_log.status = ExamAnswerLog.RIGHT
|
|
|
+ instance.judgment_answer_count += 1
|
|
|
+ else:
|
|
|
+ answer_log.status = ExamAnswerLog.WRONG
|
|
|
+ ErrorBook.add_error(question, request.user, answer_log)
|
|
|
answer_log.save()
|
|
|
|
|
|
instance.submit_time = timezone.now()
|