feat:建立考试

This commit is contained in:
zty 2024-05-14 14:45:44 +08:00
parent 466261b480
commit 01ec329f65
6 changed files with 161 additions and 21 deletions

View File

@ -0,0 +1,38 @@
# Generated by Django 3.2.12 on 2024-05-11 05:49
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('edu', '0005_certificate_培训结束日期'),
('system', '0023_alter_user_first_name'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('exam', '0005_exam_is_open'),
]
operations = [
migrations.AddField(
model_name='exam',
name='certificate',
field=models.BooleanField(default=False, verbose_name='是否生成证书'),
),
migrations.AddField(
model_name='exam',
name='course_name',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='edu.course', verbose_name='课程名称'),
),
migrations.AddField(
model_name='exam',
name='participant_dep',
field=models.ManyToManyField(related_name='exam_dep', to='system.Organization', verbose_name='考试公司'),
),
migrations.AddField(
model_name='exam',
name='participant_user',
field=models.ManyToManyField(related_name='exam_user', to=settings.AUTH_USER_MODEL, verbose_name='考试人员'),
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.12 on 2024-05-14 06:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edu', '0005_certificate_培训结束日期'),
('exam', '0006_auto_20240511_1349'),
]
operations = [
migrations.RemoveField(
model_name='exam',
name='course_name',
),
migrations.AddField(
model_name='exam',
name='course_name',
field=models.ManyToManyField(blank=True, to='edu.Course'),
),
]

View File

@ -2,7 +2,8 @@ from django.db import models
from apps.system.models import CommonAModel from apps.system.models import CommonAModel
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField
from utils.model import BaseModel from utils.model import BaseModel
from apps.edu.models import Course
from apps.system.models import User, Organization
# Create your models here. # Create your models here.
class Questioncat(CommonAModel): class Questioncat(CommonAModel):
name = models.CharField(max_length=200, verbose_name='名称') name = models.CharField(max_length=200, verbose_name='名称')
@ -87,11 +88,16 @@ class Exam(CommonAModel):
proctor_phone = models.CharField('监考人联系方式', max_length=100, null=True, blank=True) proctor_phone = models.CharField('监考人联系方式', max_length=100, null=True, blank=True)
chance = models.IntegerField('考试机会', default=3) chance = models.IntegerField('考试机会', default=3)
paper = models.ForeignKey(Paper, verbose_name='使用的试卷', on_delete=models.CASCADE, null=True, blank=True) paper = models.ForeignKey(Paper, verbose_name='使用的试卷', on_delete=models.CASCADE, null=True, blank=True)
certificate = models.BooleanField('是否生成证书', default=False)
course_name = models.ManyToManyField(Course, blank=True)
participant_dep = models.ManyToManyField(Organization, verbose_name='考试公司', related_name='exam_dep')
participant_user = models.ManyToManyField(User, verbose_name='考试人员', related_name='exam_user')
is_open = models.BooleanField('是否公开', default=True) is_open = models.BooleanField('是否公开', default=True)
def __str__(self): def __str__(self):
return self.name return self.name
class ExamRecord(CommonAModel): class ExamRecord(CommonAModel):
''' '''
考试记录表 考试记录表

View File

@ -2,6 +2,9 @@ from rest_framework.serializers import ModelSerializer, CharField, Serializer, S
from rest_framework import serializers from rest_framework import serializers
from apps.exam.models import Question, Questioncat, Paper, Exam, PaperQuestion, ExamRecord, AnswerDetail from apps.exam.models import Question, Questioncat, Paper, Exam, PaperQuestion, ExamRecord, AnswerDetail
from apps.edu.serializers import CourseSerializer
from apps.system.serializers import OrganizationSerializer, UserListSerializer
from apps.system.models import Organization, User
class QuestioncatSerializer(ModelSerializer): class QuestioncatSerializer(ModelSerializer):
@ -72,15 +75,56 @@ class PaperDetailSerializer(ModelSerializer):
class ExamCreateUpdateSerializer(ModelSerializer): class ExamCreateUpdateSerializer(ModelSerializer):
# participant_dep = serializers.PrimaryKeyRelatedField(many=True, queryset=Organization.objects.all())
# participant_user = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
# participant_dep = serializers.CharField(source='participant_dep.id', read_only=True)
# participant_user = serializers.CharField(source='participant_user.id', read_only=True)
class Meta: class Meta:
model = Exam model = Exam
fields = ['name', 'place', 'open_time', fields = ['name', 'place', 'open_time',
'close_time', 'proctor_name', 'proctor_phone', 'chance', 'paper'] 'close_time', 'proctor_name', 'proctor_phone', 'chance', 'paper', 'participant_dep', 'participant_user']
# def create(self, validated_data):
# print("------------", validated_data)
# eus_data = validated_data.pop('participant_user', None)
# deps_data = validated_data.pop('participant_dep', None)
# print("------------", eus_data, deps_data)
# exam_obj = Exam.objects.create(**validated_data)
# if eus_data:
# exam_obj.participant_user.set(eus_data) # 哪些用户参与考试, 使用set方法来设置ManyToMany关系
# if deps_data:
# exam_obj.participant_dep.set(deps_data) # 哪些部门参与考试
# return super().create(validated_data)
# def update(self, instance, validated_data):
# eus = validated_data.pop('exam_user', None)
# deps = validated_data.pop('dep', None)
# if eus:
# instance.participant_user.set(eus)
# if deps:
# instance.participant_dep.set(deps)
# # for attr, value in validated_data.items():
# # setattr(instance, attr, value)
# # instance.save()
# return instance
# class ExamUserSerializer(ModelSerializer):
# class Meta:
# model = ExamUser
# fields = ['id', 'exam_id','user_id']
# class ExamOriSerializer(ModelSerializer):
# class Meta:
# model = ExamDep
# fields = ['id', 'exam_id','organization_id']
class ExamListSerializer(ModelSerializer): class ExamListSerializer(ModelSerializer):
create_by_name = CharField(source='create_by.name', read_only=True) create_by_name = CharField(source='create_by.name', read_only=True)
paper_ = PaperSerializer(source='paper', read_only=True) paper_ = PaperSerializer(source='paper', read_only=True)
course_ = CourseSerializer(source='course_name', read_only=True)
participant_user = UserListSerializer(many=True, read_only=True)
class Meta: class Meta:
model = Exam model = Exam
@ -96,6 +140,7 @@ class ExamDetailSerializer(ModelSerializer):
fields = '__all__' fields = '__all__'
class ExamAttendSerializer(Serializer): class ExamAttendSerializer(Serializer):
code = CharField(label="考试编号") code = CharField(label="考试编号")

View File

@ -19,7 +19,8 @@ from datetime import datetime
from apps.exam.filters import ExamRecordFilter, ExamFilter from apps.exam.filters import ExamRecordFilter, ExamFilter
from datetime import timedelta from datetime import timedelta
from apps.system.mixins import CreateUpdateCustomMixin from apps.system.mixins import CreateUpdateCustomMixin
from apps.edu.serializers import CertificateSerializer
from datetime import datetime
# Create your views here. # Create your views here.
@ -295,6 +296,16 @@ class ExamViewSet(CreateUpdateCustomMixin, ModelViewSet):
开始考试具体题目信息 开始考试具体题目信息
""" """
exam = self.get_object() exam = self.get_object()
# 查询本次考试对应哪些人
participants = exam.participant_user.all()
dep = exam.participant_dep.all()
print(request.user.id, request.user.dept.id, "request.user.dept")
if request.user.id in participants or request.user.dept in dep:
pass
else:
raise ParseError('不在考试人员范围内')
dep = exam.participant_dep.all()
print(participants, "participants")
now = timezone.now() now = timezone.now()
if now < exam.open_time or now > exam.close_time: if now < exam.open_time or now > exam.close_time:
raise ParseError('不在考试时间范围') raise ParseError('不在考试时间范围')
@ -414,6 +425,23 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
er.score = total_score er.score = total_score
if er.score > 0.6*er.total_score: if er.score > 0.6*er.total_score:
er.is_pass = True er.is_pass = True
# 如果是自动发证
if exam.certificate:
now = datetime.now()
course = exam.course_name.all()
print(course, "----------course")
data_dict = {
'姓名': request.user.name,
'证书编号': 'CTCZL'+ now.year+now.month+now.day,
'单位名称': request.user.dept.name,
'所属单位': '国检测试控股集团'+request.user.dept.name,
'发证日期': now.year+'-'+now.month+'-'+now.day,
'课程列表': course,
}
serializer = CertificateSerializer(data=data_dict)
serializer.is_valid(raise_exception=True)
serializer.save()
er.took = (now - er.create_time).total_seconds() er.took = (now - er.create_time).total_seconds()
er.end_time = now er.end_time = now
er.is_submited = True er.is_submited = True

View File

@ -1,14 +1,14 @@
from .settings import * from .settings import *
DEBUG = True DEBUG = True
DATABASES = { DATABASES = {
# 'default': { 'default': {
# 'ENGINE': 'django.db.backends.postgresql', 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'cma', 'NAME': 'cma',
# 'USER': 'postgres', 'USER': 'postgres',
# 'PASSWORD': 'zctest1234', 'PASSWORD': 'zcDsj2021',
# 'HOST': '47.95.0.242', 'HOST': '49.232.14.174',
# 'PORT': '5432', 'PORT': '5432',
# }, },
# 'default': { # 'default': {
# 'ENGINE': 'django.db.backends.postgresql', # 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'cma', # 'NAME': 'cma',
@ -18,15 +18,15 @@ DATABASES = {
# # 'HOST': '1.203.161.102', # # 'HOST': '1.203.161.102',
# 'PORT': '5432', # 'PORT': '5432',
# } # }
'default': { # 'default': {
'ENGINE': 'django.db.backends.postgresql', # 'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cma', # 'NAME': 'cma',
'USER': 'cma', # 'USER': 'cma',
'PASSWORD': 'cma123', # 'PASSWORD': 'cma123',
#'HOST': '49.232.14.174', # #'HOST': '49.232.14.174',
'HOST': '127.0.0.1', # 'HOST': '127.0.0.1',
'PORT': '5432', # 'PORT': '5432',
} # }
# 'default': { # 'default': {
# 'ENGINE': 'django.db.backends.postgresql', # 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'cma', # 'NAME': 'cma',