serializers.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # coding=utf-8
  2. from rest_framework import serializers
  3. from .models import *
  4. class PractiseLogSerializer(serializers.ModelSerializer):
  5. begin_answer = serializers.SerializerMethodField()
  6. end_answer = serializers.SerializerMethodField()
  7. next_practise = serializers.SerializerMethodField()
  8. def get_next_practise(self, obj):
  9. # 继续练习的下一题id
  10. next_practise = ''
  11. if obj.end_answer:
  12. now_type = obj.end_answer.question.type
  13. questions = ExamQuestion.objects.filter(type=now_type, delete=False).order_by('id').values_list('id', flat=True)
  14. if obj.chapter:
  15. questions = questions.filter(chapter=obj.chapter)
  16. else:
  17. questions = questions.filter(chapter__subject=obj.subject)
  18. try:
  19. # ValueError: 1 is not in list
  20. now_question_index = list(questions).index(obj.end_answer.question.id)
  21. except ValueError:
  22. now_question_index = 0
  23. if now_question_index < (len(questions) -1):
  24. # 该题型未练习完,继续返回该题型下的习题
  25. next_practise = questions[now_question_index + 1]
  26. else:
  27. # 该题型已练习完,返回下一个题型下的习题
  28. while now_type <= ExamQuestion.JUDGMENT:
  29. now_type += 1
  30. questions = ExamQuestion.objects.filter(type=now_type, delete=False)
  31. if obj.chapter:
  32. questions = questions.filter(chapter=obj.chapter)
  33. else:
  34. questions = questions.filter(chapter__subject=obj.subject)
  35. questions = questions.order_by('id').first()
  36. if questions:
  37. next_practise = questions.id
  38. break
  39. return next_practise
  40. def get_end_answer(self, obj):
  41. if obj.end_answer:
  42. now_type = obj.end_answer.question.type
  43. # todo 按id查
  44. questions = ExamQuestion.objects.filter(type=now_type, delete=False).order_by('id').values_list('id', flat=True)
  45. if obj.chapter:
  46. questions = questions.filter(chapter=obj.chapter)
  47. else:
  48. questions = questions.filter(chapter__subject=obj.subject)
  49. try:
  50. # ValueError: 1 is not in list
  51. now_question_index = list(questions).index(obj.end_answer.question.id)
  52. except ValueError:
  53. now_question_index = 0
  54. name = '{0}/{1} {2}第{3}题'.format(obj.end_answer.question.chapter.name,
  55. obj.end_answer.question.chapter.subject.name,
  56. ExamQuestion.TYPE_CHOICES[obj.end_answer.question.type - 1][1],
  57. now_question_index + 1
  58. )
  59. return name
  60. return ''
  61. def get_begin_answer(self, obj):
  62. if obj.begin_answer:
  63. now_type = obj.begin_answer.question.type
  64. questions = ExamQuestion.objects.filter(type=now_type, delete=False).order_by('id').values_list('id', flat=True)
  65. if obj.chapter:
  66. questions = questions.filter(chapter=obj.chapter)
  67. else:
  68. questions = questions.filter(chapter__subject=obj.subject)
  69. try:
  70. # ValueError: 1 is not in list
  71. now_question_index = list(questions).index(obj.begin_answer.question.id)
  72. except ValueError:
  73. now_question_index = 0
  74. name = '{0}/{1} {2}第{3}题'.format(obj.begin_answer.question.chapter.name,
  75. obj.begin_answer.question.chapter.subject.name,
  76. ExamQuestion.TYPE_CHOICES[obj.begin_answer.question.type - 1][1],
  77. now_question_index + 1
  78. )
  79. return name
  80. return ''
  81. class Meta:
  82. model = PractiseLog
  83. fields = '__all__'