report.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import datetime
  2. import os
  3. from django.http import HttpResponse
  4. from rest_framework.views import APIView
  5. import util_doc
  6. from TestLaboratory.settings import SOFTWARE_ROOT
  7. from apps.software.models import Software
  8. import logging
  9. from apps.task.models import BeidouCase
  10. from apps.user.models import User
  11. from util_xlsx import write_to_xlsx
  12. logger = logging.getLogger('django')
  13. class Report(APIView):
  14. authentication_classes = []
  15. @staticmethod
  16. def convert_to_string(word_list):
  17. for i in range(len(word_list)):
  18. if word_list[i] is None:
  19. word_list[i] = ''
  20. elif not isinstance(word_list[i], str):
  21. word_list[i] = str(word_list[i])
  22. return word_list
  23. # generate report
  24. @staticmethod
  25. # @RoleControl
  26. def get(request, software_id, *args, **kwargs):
  27. software = Software.objects.filter(id=software_id, delete=False)
  28. if not software:
  29. logger.error("软件已删除或不存在")
  30. return HttpResponse(status=404, content='软件已删除或不存在')
  31. software = software[0]
  32. beidou_cases = BeidouCase.objects.order_by('-create_time')
  33. beidou_cases = beidou_cases.filter(software_id=software_id)
  34. version = request.GET.get('version')
  35. if version:
  36. beidou_cases = beidou_cases.filter(version=version)
  37. export_type = request.GET.get('export_type')
  38. file_name = software.name + '-全部测试用例.xls'
  39. if export_type:
  40. if export_type == '2':
  41. file_name = software.name + '-不合格测试用例.xls'
  42. beidou_cases = beidou_cases.filter(state=2)
  43. if not beidou_cases:
  44. logger.error("无符合要求的测试用例")
  45. return HttpResponse(status=404, content='无符合要求的测试用例')
  46. update_time = datetime.datetime.now()
  47. file_dir = SOFTWARE_ROOT + "/" + str(update_time)
  48. if not os.path.exists(file_dir):
  49. os.makedirs(file_dir)
  50. file_path = file_dir + "/" + file_name
  51. cases_info = [
  52. ['测试项目', '版本', '测试内容', '定位精度', '定时精度', '伪距测量精度', '载波测量精度', '测速精度',
  53. '捕获灵敏度', '接收灵敏度', '跟踪灵敏度', '功率范围', '自主完好性', '系统完好性', '首次捕获时间',
  54. '失锁重捕时间', '定位测速更新率', '跟踪通道数', '最大速度', '最大加速度', '执行结果', '测试人员', '备注']]
  55. for case in beidou_cases:
  56. power = ''
  57. if case.power_range_low and case.power_range_high:
  58. power = str(case.power_range_low) + '-' + str(case.power_range_high)
  59. state = '成功'
  60. if case.state == 2:
  61. state = '失败'
  62. executor_name = User.objects.filter(id=case.executor_id)[0].username
  63. cases_info.append(Report.convert_to_string(
  64. [software.name, case.version, case.title, case.positioning_accuracy, case.timing_accuracy,
  65. case.pseudo_range_measurement_accuracy, case.carrier_measurement_accuracy,
  66. case.speed_measurement_accuracy, case.sensitivity_capture, case.receiving_sensitivity,
  67. case.sensitivity_track, power, case.self_integrity, case.system_integrity, case.capture_time,
  68. case.recapture_time, case.update_rate, case.trace_channels, case.max_velocity, case.max_acceleration,
  69. state, executor_name, case.remark]
  70. ))
  71. sio = write_to_xlsx(file_path, cases_info)
  72. sio.seek(0)
  73. response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
  74. # response['Content-Disposition'] = 'attachment; filename*=UTF-8''{}'.format(escape_uri_path(file_name))
  75. response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8',
  76. 'ISO-8859-1')
  77. response.write(sio.getvalue())
  78. # executor, action, method = get_log(request)
  79. # gen_log(action, "测试任务", task.title, method, executor)
  80. return response