Browse Source

Merge remote-tracking branch 'origin/master'

jiaweiqi 3 years ago
parent
commit
93c4fd9d66

+ 13 - 15
apps/api/staff/practise/views.py

@@ -59,12 +59,12 @@ class PractiseLogViewSet(CustomModelViewSet):
 
     @action(methods=['post'], detail=True)
     def get_next_practise(self, request, pk):
-        now_practise = request.data.get('now_practise')  # 当前提交的练习题, 第一题或继续答题时,该参数为空
+        now_practise = request.data.get('now_practise')  # 当前提交的练习题第一题或继续答题时,该参数为空
         answers = json.loads(request.data.get('answers'))  # 答案, 第一题或继续答题时,该参数为空
-        next_number = int(request.data.get('next_number'))  # 下一题序号,最后一题提交时,此参数为0
-        next_type = int(request.data.get('next_type'))  # 下一题题型,默认1为单选题
+        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上一题
-        previous_practise = request.data.get('previous_practise')  #  上一题id
+        next_practise = request.data.get('next_practise')  #  下一题id,首次加载第一题,传空
         try:
             with transaction.atomic():
                 instance = self.get_object()
@@ -133,20 +133,17 @@ class PractiseLogViewSet(CustomModelViewSet):
                     instance.end_answer = answer_log
                     instance.submit_time = timezone.now()
                     instance.save()
-                    answer_log.order = next_number
                     answer_log.save()
 
                 question_data = {}
-                if previous_practise:
-                    # 上一题
-                    questions = ExamQuestion.objects.filter(id=previous_practise)
-                elif instance.type == PractiseLog.SUBJECT:
+                if instance.type == PractiseLog.SUBJECT:
                     questions = ExamQuestion.objects.filter(chapter__subject=instance.subject, delete=False).order_by(
                         'type')
                 else:
                     questions = ExamQuestion.objects.filter(chapter=instance.chapter, delete=False).order_by('type')
+
                 # 返回下一题
-                if next_number:
+                if next_practise:
                     # 循环查询4个题型的试题,只到查询到试题
                     # question = None
                     # while not question and next_type <= ExamQuestion.JUDGMENT:
@@ -155,9 +152,11 @@ class PractiseLogViewSet(CustomModelViewSet):
                     #         break
                     #     next_type += 1
                     #     next_number = 1
-                    question = questions.filter()[next_number - 1:next_number].first()
+                    question = questions.filter(id=next_practise).first()
+                else:
+                    question = questions.filter().first()
 
-                    if question:
+                if question:
                         # 查询到下一题,返回题目和答案
                         # 根据下一题,查询下下一题类型
                         # next_question = None
@@ -177,9 +176,8 @@ class PractiseLogViewSet(CustomModelViewSet):
                         question_data = {
                             'id': question.id,
                             'title': question.title,
-                            'next_type': question.type,  # 下一题类别 id,不在使用
-                            'next_number': next_number + 1,  # 下下一题序号,如果为0,则没有下一题了
-                            'previous_practise': now_practise,  # 上一题 id
+                            'next_type': question.type,  # 下一题习题类别
+                            'next_number': next_number + 1,  # 下下一题序号,
                             'option': [],
                         }
                         answer_log = PractiseAnswerLog.objects.filter(main=instance, question=question).first()

+ 0 - 1
apps/practise/practiselog/models.py

@@ -46,7 +46,6 @@ class PractiseAnswerLog(models.Model):
     main = models.ForeignKey(PractiseLog, verbose_name=u"练习记录", on_delete=models.PROTECT)
     question = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
     status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'回答状态')
-    order = models.IntegerField(verbose_name=u'当前答题序号', default=1)
 
     class Meta:
         db_table = "practise_answer_log"

+ 50 - 0
apps/practise/practiselog/serializers.py

@@ -2,7 +2,57 @@
 from rest_framework import serializers
 from .models import *
 
+
 class PractiseLogSerializer(serializers.ModelSerializer):
+    begin_answer = serializers.SerializerMethodField()
+    end_answer = serializers.SerializerMethodField()
+    next_practise = serializers.SerializerMethodField()
+
+    def get_next_practise(self, obj):
+        # 继续练习的下一题id
+        next_practise = ''
+        if obj.end_answer:
+            now_type = obj.end_answer.question.type
+            questions = ExamQuestion.objects.filter(type=now_type).order_by('id').values_list('id', flat=True)
+            now_question_index = list(questions).index(obj.end_answer.question.id)
+            if now_question_index < (len(questions) -1):
+                # 该题型未练习完,继续返回该题型下的习题
+                next_practise = questions[now_question_index + 1]
+            else:
+                # 该题型已练习完,返回下一个题型下的习题
+                while now_type <= ExamQuestion.JUDGMENT:
+                    now_type += 1
+                    questions = ExamQuestion.objects.filter(type=now_type).order_by('id').first()
+                    if questions:
+                        next_practise = questions.id
+                        break
+        return next_practise
+
+    def get_end_answer(self, obj):
+        if obj.end_answer:
+            now_type = obj.end_answer.question.type
+            questions = ExamQuestion.objects.filter(type=now_type).order_by('id').values_list('id', flat=True)
+            now_question_index = list(questions).index(obj.end_answer.question.id)
+            name = '{0}/{1} {2}第{3}题'.format(obj.end_answer.question.chapter.name,
+                                         obj.end_answer.question.chapter.subject.name,
+                                         ExamQuestion.TYPE_CHOICES[obj.end_answer.question.type - 1][1],
+                                             now_question_index + 1
+                                         )
+            return name
+        return ''
+
+    def get_begin_answer(self, obj):
+        if obj.begin_answer:
+            now_type = obj.begin_answer.question.type
+            questions = ExamQuestion.objects.filter(type=now_type).order_by('id').values_list('id', flat=True)
+            now_question_index = list(questions).index(obj.begin_answer.question.id)
+            name = '{0}/{1} {2}第{3}题'.format(obj.begin_answer.question.chapter.name,
+                                         obj.begin_answer.question.chapter.subject.name,
+                                         ExamQuestion.TYPE_CHOICES[obj.begin_answer.question.type - 1][1],
+                                         now_question_index + 1
+                                         )
+            return name
+        return ''
 
     class Meta:
         model = PractiseLog