feat: 批量删除和导出question
This commit is contained in:
parent
7449f98cb6
commit
fe0c82c185
|
@ -0,0 +1,29 @@
|
|||
from openpyxl.workbook import Workbook
|
||||
from django.conf import settings
|
||||
from datetime import datetime
|
||||
from openpyxl.styles import Font, Fill
|
||||
import json
|
||||
|
||||
BASE_DIR = settings.BASE_DIR
|
||||
|
||||
|
||||
def export_question(questions):
|
||||
'''
|
||||
params: serializer questions
|
||||
return: xlsx path
|
||||
'''
|
||||
wb = Workbook()
|
||||
ws1 = wb.active
|
||||
ws1.title = '题目表'
|
||||
ws1.append(['分类','题型', '题干', '选项', '正确答案', '解析'])
|
||||
row = ws1.row_dimensions[1]
|
||||
row.font = Font(bold=True)
|
||||
for i in questions:
|
||||
# options=''
|
||||
# for key in i.options:
|
||||
# pass
|
||||
ws1.append([i.questioncat.name, i.type, i.name, json.dumps(i.options, ensure_ascii=False), ''.join(sorted(i.right)), i.resolution])
|
||||
filename = 'questions' + datetime.now().strftime("%Y%m%d%H%M%S") +'.xlsx'
|
||||
path = '/media/export/' + filename
|
||||
wb.save((BASE_DIR + path).replace('\\', '/'))
|
||||
return path
|
|
@ -23,6 +23,7 @@ from apps.edu.serializers import CertificateSerializer
|
|||
from utils.queryset import get_child_queryset2
|
||||
from apps.system.permission import has_permission
|
||||
from apps.exam.parse_word import interpret_text
|
||||
from .export import export_question
|
||||
import os
|
||||
import shutil
|
||||
from django.db.models import Q
|
||||
|
@ -91,6 +92,27 @@ class QuestionViewSet(CreateUpdateCustomMixin, ModelViewSet):
|
|||
if ids:
|
||||
Question.objects.filter(pk__in=ids).update(enabled=True)
|
||||
return Response(status=200)
|
||||
|
||||
@action(methods=['post'], detail=False, perms_map=[{'*':'question_delete'}])
|
||||
def deletes(self, request):
|
||||
"""
|
||||
批量删除
|
||||
"""
|
||||
ids = request.data.get('ids', [])
|
||||
if request.user.is_superuser:
|
||||
Question.objects.filter(id__in=ids).update(is_delete=True)
|
||||
return Response()
|
||||
return Response({'error':'权限不足'})
|
||||
|
||||
@action(methods=['get'], detail=False,
|
||||
url_path='export', url_name='export_question', perms_map=[{'*':'export_question'}])
|
||||
def export(self, request):
|
||||
"""
|
||||
导出题目
|
||||
"""
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
path = export_question(queryset)
|
||||
return Response({'path': path})
|
||||
|
||||
@action(methods=['post'], detail=False,
|
||||
url_path='import', url_name='import_question', perms_map={'post': 'question'}, serializer_class=Serializer)
|
||||
|
|
Loading…
Reference in New Issue