userexportview.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import datetime
  2. import logging
  3. import os
  4. from django.http import HttpResponse
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from TestLaboratory.settings import USER_FILE_ROOT
  8. from apps.log.models import get_log, gen_log
  9. from apps.user.middleware.rolecontrol import RoleControl
  10. from apps.user.models import User
  11. from util_xlsx import write_to_xlsx
  12. from utils.util_add_id import get_id
  13. from utils.util_jwt import create_token_for_login
  14. logger = logging.getLogger('django')
  15. class UserExportView(APIView):
  16. authentication_classes = []
  17. @staticmethod
  18. # @RoleControl
  19. def get(request, *args, **kwargs):
  20. user_all = User.objects.order_by('-create_time')
  21. users_info = [['用户名', '企业', '联系方式', '系统角色']]
  22. identify = ['系统管理员', '测试管理员', '测试工程师']
  23. for user in user_all:
  24. users_info.append([user.username, user.name, user.tel, identify[user.identify]])
  25. file_name = "用户信息.xls"
  26. update_time = datetime.datetime.now()
  27. file_dir = USER_FILE_ROOT + str(update_time)
  28. if not os.path.exists(file_dir):
  29. os.makedirs(file_dir)
  30. file_path = file_dir + "/" + file_name
  31. sio = write_to_xlsx(file_path, users_info)
  32. sio.seek(0)
  33. response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
  34. response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8', 'ISO-8859-1')
  35. response.write(sio.getvalue())
  36. # executor, action, method = get_log(request)
  37. # gen_log(action, "导出用户信息", "全部用户", method, executor)
  38. return response