|
@@ -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"
|