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
