liu tao 3 سال پیش
والد
کامیت
929aeece55
2فایلهای تغییر یافته به همراه114 افزوده شده و 0 حذف شده
  1. 73 0
      apps/examination/exampaper/models.py
  2. 41 0
      apps/examination/exampaper/serializers.py

+ 73 - 0
apps/examination/exampaper/models.py

@@ -1,9 +1,12 @@
 # coding=utf-8
 
+import random
 from django.db import models
 from django.utils import timezone
 from django.conf import settings
 
+from utils.exceptions import CustomError
+
 from apps.foundation.models import Subject
 from apps.examination.examquestion.models import ExamQuestion
 
@@ -64,6 +67,48 @@ class ExamPaper(models.Model):
         verbose_name = u"试卷管理"
         default_permissions = ()
 
+    def clear_detail(self):
+        ExamPaperDetail.objects.filter(main=self).update(delete=True)
+
+    def generate_detail(self):
+        begin_order = 1
+        if self.single_simple_count:
+            self._generate_detail(self.single_simple_count, ExamQuestion.SINGLE, ExamQuestion.SIMPLE, begin_order)
+            begin_order += self.single_simple_count
+        if self.single_mid_count:
+            self._generate_detail(self.single_mid_count, ExamQuestion.SINGLE, ExamQuestion.MID, begin_order)
+            begin_order += self.single_mid_count
+        if self.single_hard_count:
+            self._generate_detail(self.single_hard_count, ExamQuestion.SINGLE, ExamQuestion.HARD, begin_order)
+            begin_order += self.single_hard_count
+        if self.multiple_simple_count:
+            self._generate_detail(self.multiple_simple_count, ExamQuestion.MULTIPLE, ExamQuestion.SIMPLE, begin_order)
+            begin_order += self.multiple_simple_count
+        if self.multiple_mid_count:
+            self._generate_detail(self.multiple_mid_count, ExamQuestion.MULTIPLE, ExamQuestion.MID, begin_order)
+            begin_order += self.multiple_mid_count
+        if self.multiple_hard_count:
+            self._generate_detail(self.multiple_hard_count, ExamQuestion.MULTIPLE, ExamQuestion.HARD, begin_order)
+            begin_order += self.multiple_hard_count
+        if self.fill_simple_count:
+            self._generate_detail(self.fill_simple_count, ExamQuestion.FILL, ExamQuestion.SIMPLE, begin_order)
+            begin_order += self.fill_simple_count
+        if self.fill_mid_count:
+            self._generate_detail(self.fill_mid_count, ExamQuestion.FILL, ExamQuestion.MID, begin_order)
+            begin_order += self.fill_mid_count
+        if self.fill_hard_count:
+            self._generate_detail(self.fill_hard_count, ExamQuestion.FILL, ExamQuestion.HARD, begin_order)
+            begin_order += self.fill_hard_count
+        if self.judgment_simple_count:
+            self._generate_detail(self.judgment_simple_count, ExamQuestion.JUDGMENT, ExamQuestion.SIMPLE, begin_order)
+            begin_order += self.judgment_simple_count
+        if self.judgment_mid_count:
+            self._generate_detail(self.judgment_mid_count, ExamQuestion.JUDGMENT, ExamQuestion.MID, begin_order)
+            begin_order += self.judgment_mid_count
+        if self.judgment_hard_count:
+            self._generate_detail(self.judgment_hard_count, ExamQuestion.JUDGMENT, ExamQuestion.HARD, begin_order)
+            begin_order += self.judgment_hard_count
+
     def update_count(self):
         self.single_total_count = self.single_simple_count + self.single_mid_count + self.single_hard_count
         self.multiple_total_count = self.multiple_simple_count + self.multiple_mid_count + self.multiple_hard_count
@@ -77,10 +122,38 @@ class ExamPaper(models.Model):
         self.question_total_scores = self.single_total_scores + self.multiple_total_scores + self.fill_total_scores + self.judgment_total_scores
         return self
 
+    def _generate_detail(self, count, type, difficulty, begin_order):
+        questions = ExamQuestion.objects.filter(
+            chapter__subject=self.subject,
+            difficulty = difficulty,
+            type = type,
+            delete=False
+        )
+        questions_count = questions.count()
+        if questions_count < count:
+            raise CustomError(u'[%s][%s]数量不足!' % (ExamQuestion.DIFFICULTY_CHOICES[difficulty-1][1], ExamQuestion.TYPE_JSON[type-1][1]))
+        question_ids = questions.values_list('id', flat=True)
+
+        rows = random.sample(question_ids, count)
+        random.shuffle(rows)
+
+        data = []
+        for row in rows:
+            item = ExamPaperDetail(
+                main=self,
+                question_id=row,
+                order=begin_order
+            )
+            begin_order += 1
+            data.append(item)
+
+        ExamPaperDetail.objects.bulk_create(data)
+
 class ExamPaperDetail(models.Model):
     main = models.ForeignKey(ExamPaper, verbose_name=u"试卷", on_delete=models.PROTECT)
     question = models.ForeignKey(ExamQuestion, verbose_name=u"试题", on_delete=models.PROTECT)
     order = models.IntegerField(verbose_name=u'序号', editable=False)
+    delete = models.BooleanField(verbose_name=u'删除', default=False, editable=False)
 
     class Meta:
         db_table = "exam_paper_detail"

+ 41 - 0
apps/examination/exampaper/serializers.py

@@ -21,12 +21,53 @@ class ExamPaperSerializer(serializers.ModelSerializer):
         instance = super(ExamPaperSerializer, self).create(validated_data)
         instance.update_count()
         instance.save()
+
+        instance.generate_detail()
         return instance
 
     def update(self, instance, validated_data):
+        old_count = []
+        old_count.append(instance.single_simple_count)
+        old_count.append(instance.single_mid_count)
+        old_count.append(instance.single_hard_count)
+        old_count.append(instance.multiple_simple_count)
+        old_count.append(instance.multiple_mid_count)
+        old_count.append(instance.multiple_hard_count)
+        old_count.append(instance.fill_simple_count)
+        old_count.append(instance.fill_mid_count)
+        old_count.append(instance.fill_hard_count)
+        old_count.append(instance.judgment_simple_count)
+        old_count.append(instance.judgment_mid_count)
+        old_count.append(instance.judgment_hard_count)
+
         if instance.delete:
             raise CustomError(u'试卷[%s]已经被删除,禁止操作' % instance.name)
         instance = super(ExamPaperSerializer, self).update(instance, validated_data)
         instance.update_count()
         instance.save()
+
+        new_count = []
+        new_count.append(instance.single_simple_count)
+        new_count.append(instance.single_mid_count)
+        new_count.append(instance.single_hard_count)
+        new_count.append(instance.multiple_simple_count)
+        new_count.append(instance.multiple_mid_count)
+        new_count.append(instance.multiple_hard_count)
+        new_count.append(instance.fill_simple_count)
+        new_count.append(instance.fill_mid_count)
+        new_count.append(instance.fill_hard_count)
+        new_count.append(instance.judgment_simple_count)
+        new_count.append(instance.judgment_mid_count)
+        new_count.append(instance.judgment_hard_count)
+
+        change_detail = False
+        for i in range(0, len(old_count)):
+            if old_count[i] != new_count[i]:
+                change_detail = True
+                break
+
+        if change_detail:
+            instance.clear_detail()
+            instance.generate_detail()
+
         return instance