Browse Source

Merge remote-tracking branch 'origin/master'

jiaweiqi 3 years ago
parent
commit
3b50134f67
3 changed files with 53 additions and 26 deletions
  1. 18 20
      apps/api/staff/exam/views.py
  2. 29 6
      apps/api/staff/mock/views.py
  3. 6 0
      utils/pagination.py

+ 18 - 20
apps/api/staff/exam/views.py

@@ -40,7 +40,7 @@ class ExamLogViewSet(CustomModelViewSet):
             }
             answer_log.append(item)
         ranks = []
-        rank_data = ExamLog.objects.filter(exam=instance.exam).order_by('rank').values('user__name','scores','rank')
+        rank_data = ExamLog.objects.filter(exam=instance.exam, rank__isnull=False, delete=False).order_by('rank').values('user__name','scores','rank')
         for rank in rank_data:
             item = {
                 'name':rank['user__name'],
@@ -57,6 +57,7 @@ class ExamLogViewSet(CustomModelViewSet):
 
     @action(methods=['get'], detail=False)
     def get_home_exam(self, request):
+        # 首页,考试提示
         start_time = timezone.now() + datetime.timedelta(minutes=30)
         queryset = ExamLog.objects.filter(delete=False,
                                           type=ExamLog.FORMAL,
@@ -83,6 +84,7 @@ class ExamLogViewSet(CustomModelViewSet):
 
     @action(methods=['get'], detail=False)
     def get_exam(self, request):
+        # 正式考试,获取考试列表,提前半小时
         start_time = timezone.now() + datetime.timedelta(minutes=30)
         queryset = ExamLog.objects.filter(delete=False,
                                           type=ExamLog.FORMAL,
@@ -99,6 +101,7 @@ class ExamLogViewSet(CustomModelViewSet):
                 'exam_time':strftime(exam_log.exam.exam_time),
                 'exam_end_time':strftime(exam_log.exam.exam_end_time),
                 'duration':exam_log.exam.duration,
+                'submit_time':exam_log.submit_time and True or False,
             }
             data.append(item)
         return response_ok(data)
@@ -108,23 +111,16 @@ 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,首次加载第一题,传空
+        submit = request.data.get('submit')  # 交卷1,下一题为空
 
         try:
             with transaction.atomic():
                 instance = self.get_object()
-                # d1 = datetime.datetime.strptime(str(timezone.now().strftime('%Y-%m-%d %H:%M:%S')), '%Y-%m-%d %H:%M:%S')
-                # d2 = datetime.datetime.strptime(str(instance.exam.exam_time), '%Y-%m-%d %H:%M:%S')
-                # d3 = datetime.datetime.strptime(str(instance.exam.exam_end_time), '%Y-%m-%d %H:%M:%S')
-                # print(4444444444,(d1-d2).seconds)
-                # print(555555555,(d1-d3).seconds)
-                # exam_time = timezone.now() - instance.exam.exam_time
-                # exam_end_time = timezone.now() - instance.exam.exam_end_time
-                # print(33333333, timezone.now())
-                # print(1111111111,instance.exam.exam_time, exam_time.seconds)
-                # print(22222222,instance.exam.exam_end_time, exam_end_time.seconds)
+                if instance.submit_time:
+                    raise CustomError('您已交卷,禁止重复答题!')
                 if timezone.now() < instance.exam.exam_time:
                     raise CustomError('还未到考试时间,请稍等!')
-                if timezone.now() > instance.exam.exam_end_time:
+                if not submit and timezone.now() > instance.exam.exam_end_time:
                     raise CustomError('考试已结束,禁止答题!')
                 # 点击下一题,保存
                 if now_practise:
@@ -224,31 +220,31 @@ 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)
+                    answers = ExamAnswerOptionLog.objects.filter(main__main=instance, main__detail=single).first()
                     single_questions_list.append(
                         {
                             'question_id': single,
-                            'complete': answer_log and True or False,
+                            'complete': answers and True or False,
                         }
                     )
                 # 多选题
                 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)
+                    answers = ExamAnswerOptionLog.objects.filter(main__main=instance, main__detail=multiple).first()
                     multiple_questions_list.append(
                         {
                             'question_id': multiple,
-                            'complete': answer_log and True or False,
+                            'complete': answers and True or False,
                         }
                     )
                 # 填空题
                 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)
+                    answers = ExamAnswerFillLog.objects.filter(main__main=instance, main__detail=multiple).first()
                     fill_questions_list.append(
                         {
                             'question_id': fill,
-                            'complete': answer_log and True or False,
+                            'complete': answers and True or False,
                         }
                     )
                 # 判断题
@@ -280,8 +276,10 @@ class ExamLogViewSet(CustomModelViewSet):
         # 习题交卷,把上个接口的判断答案,放到此处
         try:
             instance = self.get_object()
-            # if timezone.now() > instance.exam.exam_end_time:
-            #     raise CustomError('考试已结束,禁止答题!')
+            if timezone.now() > instance.exam.exam_end_time:
+                raise CustomError('考试已结束,禁止答题!')
+            if instance.submit_time:
+                raise CustomError('您已交卷,禁止重复交卷!')
             with transaction.atomic():
                 paper_details = ExamPaperDetail.objects.filter(main=instance.exampaper, delete=False)
                 for detail in paper_details:

+ 29 - 6
apps/api/staff/mock/views.py

@@ -28,9 +28,29 @@ class ExamPaperViewSet(CustomModelViewSet):
     queryset = ExamPaper.objects.filter(delete=False, type=ExamPaper.MOCK)
     serializer_class = StaffExamPaperSerializer
 
-    def filter_queryset(self, queryset):
-        f = ExamPaperFilter(self.request.GET, queryset=queryset, request=self.request)
-        return f.qs
+    def list(self, request, *args, **kwargs):
+        # 底栏合计
+        # 统计做过多少套
+        queryset = self.filter_queryset(self.get_queryset())
+        queryset = ExamPaperFilter(self.request.GET, queryset=queryset, request=self.request).qs
+        exampaper_ids = ExamLog.objects.filter(user=self.request.user, delete=False).values_list('exampaper_id',
+                                                                                                 flat=True)
+        did_paper = queryset.filter(id__in=exampaper_ids).count()
+        totalRow = {'totalRow': 1, 'did_paper': did_paper, }
+        page = self.paginate_queryset(queryset)
+        if page is not None:
+            serializer = self.get_serializer(page, many=True)
+            data = serializer.data
+            # if len(data) > 0:
+            data.append(totalRow)
+            return self.get_paginated_response(data)
+
+        serializer = self.get_serializer(queryset, many=True)
+        return response_ok(serializer.data)
+
+    # def filter_queryset(self, queryset):
+    #     f = ExamPaperFilter(self.request.GET, queryset=queryset, request=self.request)
+    #     return f.qs
 
 
 class ExamLogViewSet(CustomModelViewSet):
@@ -49,8 +69,8 @@ class ExamLogViewSet(CustomModelViewSet):
         answer_logs = ExamAnswerLog.objects.filter(main=instance).order_by('detail__order')
         for al in answer_logs:
             item = {
-                'id':al.id,
-                'status':al.status,
+                'id': al.id,
+                'status': al.status,
             }
             answer_log.append(item)
         result = {
@@ -91,6 +111,8 @@ class ExamLogViewSet(CustomModelViewSet):
         try:
             with transaction.atomic():
                 instance = self.get_object()
+                if instance.submit_time:
+                    raise CustomError('您已交卷,禁止重复答题!')
                 # 点击下一题,保存
                 if now_practise:
                     detail = ExamPaperDetail.objects.filter(id=now_practise).first()
@@ -245,7 +267,8 @@ class ExamLogViewSet(CustomModelViewSet):
         # 习题交卷,把上个接口的判断答案,放到此处
         try:
             instance = self.get_object()
-
+            if instance.submit_time:
+                raise CustomError('您已交卷,禁止重复交卷!')
             with transaction.atomic():
                 paper_details = ExamPaperDetail.objects.filter(main=instance.exampaper, delete=False)
                 for detail in paper_details:

+ 6 - 0
utils/pagination.py

@@ -12,6 +12,11 @@ class CustomPagination(pagination.PageNumberPagination):
 
     def get_paginated_response(self, data):
         ps = self.get_page_size(self.request)
+        # 底栏合计
+        totalRow = {}
+        if len(data) > 0 and 'totalRow' in data[-1:][0]:
+            totalRow = data[-1:][0]
+            data = data[:-1]
         return Response({
             'code':0,
             'showCount': ps,
@@ -19,5 +24,6 @@ class CustomPagination(pagination.PageNumberPagination):
             'totalResult': self.page.paginator.count,
             'currentPage': self.page.number,
             'count': self.page.paginator.count,
+            'totalRow': totalRow,
             'data': data
         })