فهرست منبع

模拟考试,下一题,提交答卷

wushaodong 3 سال پیش
والد
کامیت
641c2d7403
1فایلهای تغییر یافته به همراه59 افزوده شده و 0 حذف شده
  1. 59 0
      apps/api/staff/mock/views.py

+ 59 - 0
apps/api/staff/mock/views.py

@@ -250,6 +250,65 @@ class ExamLogViewSet(CustomModelViewSet):
             traceback.print_exc()
             return response_error(str(e))
 
+    @action(methods=['post'], detail=True)
+    def submit_practise(self, request, pk):
+        # 习题交卷,把上个接口的判断答案,放到此处
+        try:
+            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
+                    if question.type == ExamQuestion.SINGLE:
+                        # 单选
+                        answers = ExamAnswerOptionLog.objects.filter(main=answer_log).first()
+                        if answers:
+                            if answers.option.right:
+                                answer_log.status = ExamAnswerLog.RIGHT
+                            else:
+                                answer_log.status = ExamAnswerLog.WRONG
+                    elif question.type == ExamQuestion.MULTIPLE:
+                        # 多选
+                        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
+                        else:
+                            answer_log.status = ExamAnswerLog.WRONG
+                    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
+                        else:
+                            answer_log.status = ExamAnswerLog.WRONG
+                    else:
+                        # 判断
+                        if question.judgment == (answer_log.status == ExamAnswerLog.RIGHT):
+                            answer_log.status = ExamAnswerLog.RIGHT
+                        else:
+                            answer_log.status = ExamAnswerLog.WRONG
+                    answer_log.save()
+
+                instance.submit_time = timezone.now()
+                instance.save()
+                SysLog.objects.addnew(request.user, SysLog.INSERT, u"提交模拟考试题答案,id=%d" % (instance.id))
+        except CustomError as e:
+            return response_error(e.get_error_msg())
+        except Exception as e:
+            traceback.print_exc()
+            return response_error(str(e))
+        return response_ok()
+
 class DictView(APIView):
     permission_classes = [IsStaff, ]