export question

This commit is contained in:
caoqianming 2020-07-29 22:51:04 +08:00
parent 323c4189e9
commit feb1b17554
3 changed files with 36 additions and 1 deletions

View File

@ -23,4 +23,3 @@ def export_test(tests):
path = '/media/export/' + filename path = '/media/export/' + filename
wb.save((BASE_DIR + path).replace('\\', '/')) wb.save((BASE_DIR + path).replace('\\', '/'))
return path return path

View File

@ -0,0 +1,29 @@
from openpyxl.workbook import Workbook
from django.conf import settings
from datetime import datetime
from openpyxl.styles import Font, Fill
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, i.options, 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

View File

@ -16,6 +16,7 @@ from crm.models import PaySubject
from examtest.models import WorkScope from examtest.models import WorkScope
from server import settings from server import settings
from utils.custom import CommonPagination from utils.custom import CommonPagination
from exports import export_question
from .models import Question, Questioncat from .models import Question, Questioncat
@ -115,6 +116,12 @@ class QuestionViewSet(ModelViewSet):
ret['panduan'] = queryset.filter(type='判断').count() ret['panduan'] = queryset.filter(type='判断').count()
return Response(ret) return Response(ret)
@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, @action(methods=['post'], detail=False,
url_path='import', url_name='import_question',perms_map=[{'post':'question_import'}]) url_path='import', url_name='import_question',perms_map=[{'post':'question_import'}])