import datetime import logging import os from django.http import HttpResponse from rest_framework.response import Response from rest_framework.views import APIView from TestLaboratory.settings import USER_FILE_ROOT from apps.log.models import get_log, gen_log from apps.user.middleware.rolecontrol import RoleControl from apps.user.models import User from util_xlsx import write_to_xlsx from utils.util_add_id import get_id from utils.util_jwt import create_token_for_login logger = logging.getLogger('django') class UserExportView(APIView): authentication_classes = [] @staticmethod # @RoleControl def get(request, *args, **kwargs): user_all = User.objects.order_by('-create_time') users_info = [['用户名', '企业', '联系方式', '系统角色']] identify = ['系统管理员', '测试管理员', '测试工程师'] for user in user_all: users_info.append([user.username, user.name, user.tel, identify[user.identify]]) file_name = "用户信息.xls" update_time = datetime.datetime.now() file_dir = USER_FILE_ROOT + str(update_time) if not os.path.exists(file_dir): os.makedirs(file_dir) file_path = file_dir + "/" + file_name sio = write_to_xlsx(file_path, users_info) sio.seek(0) response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8', 'ISO-8859-1') response.write(sio.getvalue()) # executor, action, method = get_log(request) # gen_log(action, "导出用户信息", "全部用户", method, executor) return response