from django.db import models from django.db.models import Count from university.db.fields import JSONField class OnlineExam(models.Model): FLAG_PCTA = 'PCTA' FLAG_PCTP = 'PCTP' FLAG_CHOICES = ( (FLAG_PCTA, 'PCTA'), (FLAG_PCTP, 'PCTP') ) name = models.CharField(max_length=255) slug = models.SlugField(blank=False, null=False, allow_unicode=True) flag = models.CharField( max_length=32, default=FLAG_PCTA, choices=FLAG_CHOICES) cover_image = models.ForeignKey('university.Upload', on_delete=models.PROTECT) short_description = models.CharField(max_length=255) detailed_description = models.TextField() rules = models.TextField() registration_deadline = models.DateTimeField(blank=False, null=False) start_at = models.DateTimeField(blank=False, null=False) end_at = models.DateTimeField(blank=False, null=False) total_score = models.IntegerField(default=0) passing_score = models.IntegerField(default=0) number_of_questions = models.IntegerField(default=0) answers = JSONField(default={}) structure = JSONField(default=[]) allowed_cheating_times = models.IntegerField(default=4) alert_wechat_webhook = models.URLField(blank=True, null=True) ip_allowlist = JSONField(default=[]) # [{ # "title": "一、单选题", # "description": "", # "number_of_questions": 10, # "questions": [{ # "id": "Q_1", // 试题 ID # "type": 0, // 试题类型: 0 -> 单选题, 1 -> 多选题 # "score": 1, // 分数 # "problem": "关于TiDB 的索引 Key 编码,下列哪一项是对的?", // 问题 # "options": [ # { # "value": "O_1", # "text": "需要 Mem compareble 的编码算法" # }, # { # "value": "O_2", # "text": "索引 Key 中一定包含 RowID" # }, # { # "value": "O_3", # "text": "TiDB 支持对索引 Key 进行逆序编码,以支持逆序索引" # } # ] # }, { # "title": "二、多选题", # "description": "", # "number_of_questions": 20, # "questions": [{ # "id": "Q_2", // 试题 ID # "type": 1, // 试题类型: 0 -> 单选题, 1 -> 多选题 # "score": 1, // 分数 # "problem": "TiDB 集群建议使用的 Linux 操作系统是?", // 问题 # "options": [ # { # "value": "O_12", # "text": "Red Hat Enterprise Linux" # }, # { # "value": "O_2", # "text": "CentOS" # }, # { # "value": "O_3", # "text": "Debian Linux" # } # ] # }] # }], # "draft": { // 草稿, 恢复用户之前已完成的答案 # "Q_1": "O_1", // Key 为试题 ID, value 为答案 # "Q_2": "O_1", // 单选题答案 # "Q_3": ["O_1", "O_2"], // 多选题答案 # } # }] class Meta: db_table = 'online_exams' def get_status_distribution(self): from university.models import UserOnlineExam data = list(self.useronlineexam_set.all().values('status').annotate(count=Count('status'))) for d in data: d['status_display'] = dict(UserOnlineExam.STATUS_CHOICES).get(d['status'], '未知状态') return data