fix: edu/Examrecord
This commit is contained in:
parent
18b0429044
commit
5178cb0daa
|
@ -19,6 +19,7 @@ class Train(CommonADModel):
|
|||
duration = models.PositiveIntegerField(verbose_name='时长', default=0, help_text='单位:s', editable=False)
|
||||
description = models.TextField(verbose_name='内容描述', default='', blank=True)
|
||||
is_public = models.BooleanField('是否公开', default=False)
|
||||
attend_departs = models.ManyToManyField(Dept, verbose_name='参与部门', blank=True)
|
||||
files = models.ManyToManyField(File, verbose_name='附件', blank=True)
|
||||
|
||||
class Meta:
|
||||
|
@ -36,6 +37,7 @@ class Questioncat(CommonAModel):
|
|||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Question(CommonAModel):
|
||||
Q_DAN = 10
|
||||
Q_DUO = 20
|
||||
|
@ -120,7 +122,7 @@ class ExamRecord(CommonADModel):
|
|||
is_pass = models.BooleanField(default=True, verbose_name='是否通过')
|
||||
is_submited = models.BooleanField(default=False, verbose_name='是否提交')
|
||||
is_last = models.BooleanField(default=False, verbose_name='是否最后一次考试记录')
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name = '考试记录'
|
||||
verbose_name_plural = verbose_name
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
|
||||
from docx import Document
|
||||
from openpyxl import load_workbook
|
||||
import re
|
||||
|
||||
|
||||
QUES_CLASS = '安全领域'
|
||||
|
||||
OPTION_LIST=[
|
||||
[r"A:\s*(\S+)", "D"],
|
||||
[r"B:\s*(\S+)", "E"],
|
||||
[r"C:\s*(\S+)", "F"],
|
||||
[r"D:\s*(\S+)", "G"],
|
||||
[r"E:\s*(\S+)", "H"],
|
||||
[r"F:\s*(\S+)", "I"],
|
||||
]
|
||||
|
||||
def fill_excel(matches, excel_path, local):
|
||||
wb = load_workbook(excel_path)
|
||||
ws = wb.active
|
||||
if matches:
|
||||
ws[local] = matches
|
||||
wb.save(excel_path)
|
||||
|
||||
|
||||
def match_text(text, pattern):
|
||||
matches = re.search(pattern, text)
|
||||
if matches:
|
||||
results = matches.group(1)
|
||||
return results
|
||||
return ''
|
||||
|
||||
# 解析word文档
|
||||
def interpret_text(start:int, excel_path:str, doc_path:str, field=None):
|
||||
wordfile = Document(doc_path)
|
||||
correct_dict = {}
|
||||
option_dict = {}
|
||||
question_type = {}
|
||||
ques_text = {}
|
||||
for index, p in enumerate(wordfile.paragraphs):
|
||||
correct_answer = match_text(p.text, r"正确答案:\s*(\S+)") # 匹配正确答案
|
||||
if correct_answer:
|
||||
correct_dict.setdefault("correct_answer", []).append(correct_answer)
|
||||
for e in OPTION_LIST: # 匹配选项
|
||||
result = match_text(p.text, e[0])
|
||||
if result:
|
||||
option_dict.setdefault(e[1], []).append(result)
|
||||
# 题目类型
|
||||
# fill_excel(QUES_CLASS, excel_path, 'B'+str(index+start))
|
||||
if p.text[:1]=='【' and p.text[4:5]=='】':
|
||||
q_type = p.text[1:3] # 题目类型
|
||||
question_type.setdefault("question_type", []).append(q_type)
|
||||
if p.text[-2]=='分': #(3分)
|
||||
question_text = p.text[5:-4].strip()
|
||||
result = bool(re.match(r'\d+、', question_text)) # 处理题目前的序号
|
||||
if result:
|
||||
question = re.sub(r'\d+、', '',question_text)
|
||||
ques_text.setdefault("question_text", []).append(question)
|
||||
dict_list = [correct_dict,ques_text,option_dict,question_type]
|
||||
for d in dict_list:
|
||||
for key,value in d.items():
|
||||
if key == "correct_answer":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'J'+str(start+v))
|
||||
elif key == "question_type":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'A'+str(start+v))
|
||||
if field:
|
||||
fill_excel(field, excel_path, 'B'+str(start+v))
|
||||
else:
|
||||
fill_excel(QUES_CLASS, excel_path, 'B'+str(start+v))
|
||||
elif key == "question_text":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'C'+str(start+v))
|
||||
elif key == "D":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'D'+str(start+v))
|
||||
elif key == "E":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'E'+str(start+v))
|
||||
elif key == "F":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'F'+str(start+v))
|
||||
elif key == "G":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'G'+str(start+v))
|
||||
elif key == "H":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'H'+str(start+v))
|
||||
elif key == "I":
|
||||
for v in range(len(value)):
|
||||
fill_excel(value[v], excel_path, 'I'+str(start+v))
|
||||
return 'OK'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
doc_path = "C:\code\data\\test.docx"
|
||||
excel_path = "C:\code\data\question.xlsx"
|
||||
interpret_text(3, excel_path, doc_path)
|
|
@ -1,6 +1,6 @@
|
|||
from apps.utils.serializers import CustomModelSerializer
|
||||
from rest_framework import serializers
|
||||
from .models import Questioncat, Question, Paper, PaperQuestion, Exam, ExamRecord, AnswerDetail
|
||||
from .models import Questioncat, Question, Paper, PaperQuestion, Exam, ExamRecord, AnswerDetail, Train
|
||||
from apps.utils.constants import EXCLUDE_FIELDS, EXCLUDE_FIELDS_BASE
|
||||
from django.db import transaction
|
||||
from apps.utils.snowflake import idWorker
|
||||
|
@ -154,8 +154,13 @@ class TookSerializerMixin:
|
|||
|
||||
class ExamRecordSerializer(CustomModelSerializer, TookSerializerMixin):
|
||||
create_by_name = serializers.CharField(source='create_by.name', read_only=True)
|
||||
exam_open_time = serializers.DateTimeField(source='exam.open_time', read_only=True)
|
||||
exam_close_time = serializers.DateTimeField(source='exam.close_time', read_only=True)
|
||||
total_score = serializers.IntegerField(source='exam.paper.total_score', read_only=True)
|
||||
pass_score = serializers.IntegerField(source='exam.paper.pass_score', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = ExamRecord
|
||||
model = ExamRecord
|
||||
exclude = ["questions"]
|
||||
|
||||
|
||||
|
@ -176,4 +181,10 @@ class ExamRecordSubmitSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = ExamRecord
|
||||
fields = ['detail']
|
||||
fields = ['detail']
|
||||
|
||||
class ExamTrainingSerializer(CustomModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Train
|
||||
fields = "__all__"
|
||||
|
|
|
@ -124,6 +124,7 @@ class ExamViewSet(CustomModelViewSet):
|
|||
AnswerDetail.objects.bulk_create(details)
|
||||
sr = ExamRecordInitSerizlier(er)
|
||||
res_data = sr.data
|
||||
print(res_data)
|
||||
res_data.update({"chance_used": chance_used})
|
||||
return Response(sr.data, status=201)
|
||||
raise ParseError('暂不支持')
|
||||
|
|
Loading…
Reference in New Issue