report.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from django.http import HttpResponse
  2. from rest_framework.views import APIView
  3. import util_doc
  4. from apps.software.models import Software
  5. import logging
  6. logger = logging.getLogger('django')
  7. class Report(APIView):
  8. authentication_classes = []
  9. # generate report
  10. @staticmethod
  11. # @RoleControl
  12. def get(request, software_id, *args, **kwargs):
  13. software = Software.objects.filter(id=software_id, delete=False)
  14. if not software:
  15. logger.error("软件已删除或不存在")
  16. return HttpResponse(status=404, content='软件已删除或不存在')
  17. software = software[0]
  18. executor_id = request.GET.get('executor_id')
  19. if not executor_id:
  20. logger.error("测试人员id不能为空")
  21. return HttpResponse(status=404, content="测试人员id不能为空")
  22. cases_info = {}
  23. cases = software.testcase_set.filter(executor_id=executor_id, delete=False)
  24. executors = set()
  25. if cases:
  26. for case in cases:
  27. state = '未执行'
  28. if case.state == 1:
  29. state = '执行成功'
  30. elif case.state == 2:
  31. state = '执行失败'
  32. cases_info[case.id_in_task] = {
  33. 'id': case.id_in_task,
  34. 'version': case.version,
  35. 'name': case.name,
  36. 'requisite': case.requisite,
  37. 'priority': case.priority,
  38. 'environment': case.environment,
  39. 'type': case.type,
  40. 'process': case.process,
  41. 'expected_result': case.expected_result,
  42. 'actual_result': case.actual_result,
  43. 'state': state,
  44. 'writer': case.writer,
  45. 'assessor': case.assessor,
  46. 'executor': case.executor,
  47. 'remark': case.remark,
  48. 'execute_time': str(case.update_time)[:10]
  49. }
  50. executors.add(case.executor)
  51. creators = []
  52. plans = software.testplan_set.filter(delete=False)
  53. if plans:
  54. for plan in plans:
  55. creators.append(plan.creator.name)
  56. version_number = software.version_set.order_by('-create_time')[0].number
  57. file_name = '软件测试报告.docx'
  58. print(str(software.create_time).split(' ')[0])
  59. document = util_doc.process_doc(software.name, software.type, version_number, str(software.create_time)
  60. .split(' ')[0], executors, cases_info)
  61. response = HttpResponse(content_type='application/octet-stream')
  62. response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name).encode('utf-8', 'ISO-8859-1')
  63. document.save(response)
  64. # executor, action, method = get_log(request)
  65. # gen_log(action, "测试报告", software_id+"-测试报告", method, executor)
  66. return response