tasklistview.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import datetime
  2. from rest_framework.response import Response
  3. from rest_framework.views import APIView
  4. from django.http import HttpResponse
  5. from TestLaboratory.settings import TASK_ROOT, CASE_TEMPLATE_ROOT, HTTP_HEAD
  6. from apps.file.models import FileManager
  7. from apps.log.models import get_log, gen_log
  8. from apps.plan.models import TestPlan
  9. from apps.task.models import TestTask, TestCase
  10. from apps.user.middleware.rolecontrol import RoleControl
  11. from apps.user.models import User
  12. from utils.util_add_id import get_id
  13. from utils.util_file.util_fileio import write_file
  14. import logging
  15. logger = logging.getLogger('django')
  16. class TaskListView(APIView):
  17. # 登录权限验证
  18. authentication_classes = []
  19. # create task
  20. @staticmethod
  21. @RoleControl
  22. def post(request, *args, **kwargs):
  23. plan_id = request.POST.get('plan_id')
  24. title = request.POST.get('title')
  25. description = request.POST.get('description')
  26. executors_id = request.POST.get('executors').split('&&')
  27. statement_files = request.FILES.getlist('files')
  28. # step1:判断所属软件及版本是否存在
  29. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  30. if not plan:
  31. logger.error("测试计划已删除或不存在")
  32. return HttpResponse(status=404, content='测试计划已删除或不存在')
  33. # step1:创建文件的数据库字段
  34. fids = ''
  35. file_paths = []
  36. file_ids = []
  37. for file in statement_files:
  38. create_time = update_time = datetime.datetime.now()
  39. try:
  40. write_file(file, TASK_ROOT + "/" + str(create_time))
  41. except:
  42. logger.error("文件上传失败")
  43. return HttpResponse(status=500, content="文件上传失败")
  44. id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
  45. try:
  46. file_new = FileManager.objects.create(id=id_file, category="plan_statement",
  47. path=str(create_time) + "/" + file.name,
  48. create_time=create_time,
  49. update_time=update_time)
  50. except:
  51. logger.error("文件路径写入数据库失败")
  52. return HttpResponse(status=500, content="文件路径写入数据库失败")
  53. file_ids.append(id_file)
  54. file_paths.append(TASK_ROOT + file_new.path)
  55. fids += id_file + '&&'
  56. fids = fids[:-2]
  57. task_files = {'case-template': CASE_TEMPLATE_ROOT, 'statement-fids': fids}
  58. create_time = update_time = datetime.datetime.now()
  59. return_value = []
  60. for executor_id in executors_id:
  61. executor = User.objects.filter(id=executor_id, delete=False)
  62. if not executor:
  63. logger.error("执行者用户不存在")
  64. return HttpResponse(status=404, content='执行者用户不存在')
  65. task_id = get_id(TestTask, 'TestLaboratory_V1_Task_1')
  66. task = TestTask.objects.create(id=task_id, title=title, description=description, state=0, plan_id=plan_id,
  67. statement_file=fids, executor_id=executor_id,
  68. create_time=create_time, update_time=update_time)
  69. executor, action, method = get_log(request)
  70. gen_log(action, "测试任务", task.title, method, executor)
  71. return_value.append({
  72. 'software_id': task.plan.software.id,
  73. 'software_name': task.plan.software.name,
  74. 'version': task.plan.version,
  75. 'id': task.id,
  76. 'executor_id': executor_id,
  77. 'title': title,
  78. 'description': description,
  79. 'state': 0,
  80. 'files': [{'file_id': id_file, 'file_name': file.name, 'file_url': HTTP_HEAD + file_path} for
  81. (id_file, file, file_path)
  82. in zip(file_ids, statement_files, file_paths)],
  83. 'case_template': HTTP_HEAD + CASE_TEMPLATE_ROOT
  84. })
  85. return Response(return_value)
  86. # view task_list
  87. @staticmethod
  88. @RoleControl
  89. def get(request, *args, **kwargs):
  90. title = request.GET.get('title')
  91. software_id = request.GET.get('software_id')
  92. state = request.GET.get('state')
  93. executor_id = request.GET.get('executor_id')
  94. sort = request.GET.get('sort')
  95. task_all = TestTask.objects.order_by('-create_time')
  96. if title:
  97. task_all = task_all.filter(title__contains=title)
  98. if software_id:
  99. task_all = task_all.filter(plan__software__id=software_id)
  100. if state:
  101. task_all = task_all.filter(state=state)
  102. if executor_id:
  103. task_all = task_all.filter(executor_id=executor_id)
  104. """
  105. sort=update_time, -update_time, create_time, -create_time, title, -title, state
  106. """
  107. if sort:
  108. task_all = task_all.order_by(sort)
  109. task_all = task_all.filter(delete=False)
  110. if not task_all:
  111. return Response([])
  112. info = []
  113. for task in task_all.all():
  114. fail_num = len(TestCase.objects.filter(task=task, state=2, delete=False))
  115. info.append({
  116. 'software_id': task.plan.software.id,
  117. 'software_name': task.plan.software.name,
  118. 'version': task.plan.version,
  119. # 'executor_id': task.executor_id,
  120. 'executor': {'id': task.executor.id, 'name': task.executor.username},
  121. 'plan_name': task.plan.title,
  122. 'id': task.id,
  123. 'title': task.title,
  124. 'description': task.description,
  125. 'state': task.state,
  126. 'fail_num': fail_num,
  127. 'create_time': task.create_time,
  128. 'update_time': task.update_time,
  129. })
  130. return Response(info)