taskview.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 PLAN_ROOT, SOFTWARE_ROOT, HTTP_HEAD, TASK_ROOT, CASE_TEMPLATE_ROOT, CASE_FILE_ROOT
  6. from apps.file.models import FileManager
  7. from apps.log.models import get_log, gen_log
  8. from apps.task.models import TestTask
  9. from apps.user.middleware.rolecontrol import RoleControl
  10. from apps.user.models import User
  11. from utils.util_add_id import get_id
  12. from utils.util_file.util_fileio import write_file
  13. import logging
  14. logger = logging.getLogger('django')
  15. class TaskView(APIView):
  16. # 登录权限验证
  17. authentication_classes = []
  18. # delete task
  19. @staticmethod
  20. @RoleControl
  21. def delete(request, task_id, *args, **kwargs):
  22. task = TestTask.objects.filter(id=task_id, delete=False)
  23. if not task:
  24. logger.error("测试任务已删除或不存在")
  25. return HttpResponse(status=404, content='测试任务已删除或不存在')
  26. task = task[0]
  27. task_sids = task.statement_file.split('&&')
  28. if task_sids[0]:
  29. for statement_id in task_sids:
  30. statement = FileManager.objects.get(id=statement_id)
  31. statement.delete = True
  32. statement.save()
  33. task.delete = True
  34. task.save()
  35. executor, action, method = get_log(request)
  36. gen_log(action, "测试任务", task.title, method, executor)
  37. return HttpResponse("删除完成")
  38. # view task
  39. @staticmethod
  40. @RoleControl
  41. def get(request, task_id, *args, **kwargs):
  42. task = TestTask.objects.filter(id=task_id, delete=False)
  43. if not task:
  44. logger.error("测试计划已删除或不存在")
  45. return HttpResponse(status=404, content='测试计划已删除或不存在')
  46. task = task[0]
  47. version = task.plan.software.version_set.filter(number=task.plan.version, delete=False)
  48. if not version:
  49. logger.error("测试任务对应软件版本不存在或已删除")
  50. return HttpResponse(status=404, content='测试任务对应软件版本不存在或已删除')
  51. version = version[0]
  52. version_fid = version.version_file
  53. version_file = FileManager.objects.filter(id=version_fid)
  54. if not version_file:
  55. logger.error("无法定位测试任务对应的软件实体")
  56. return HttpResponse(status=404, content='无法定位测试任务对应的软件实体')
  57. version_file = version_file[0]
  58. plan_sids = task.plan.statement_file.split('&&')
  59. plan_file_names = []
  60. plan_file_urls = []
  61. if plan_sids[0]:
  62. for plan_statement_id in plan_sids:
  63. plan_statement = FileManager.objects.get(id=plan_statement_id)
  64. plan_file_urls.append(PLAN_ROOT + plan_statement.path)
  65. plan_file_names.append(plan_statement.path.split('/')[-1])
  66. task_sids = task.statement_file.split('&&')
  67. task_file_names = []
  68. task_file_urls = []
  69. if task_sids[0]:
  70. for task_statement_id in task_sids:
  71. task_statement = FileManager.objects.get(id=task_statement_id)
  72. task_file_urls.append(TASK_ROOT + task_statement.path)
  73. task_file_names.append(task_statement.path.split('/')[-1])
  74. case_file_info = {}
  75. if task.case_file:
  76. case_file = FileManager.objects.get(id=task.case_file)
  77. case_file_info = {'file_name': case_file.path.split('/')[-1], 'file_url': HTTP_HEAD + CASE_FILE_ROOT + case_file.path}
  78. executor, action, method = get_log(request)
  79. gen_log(action, "测试任务", task.title, method, executor)
  80. return Response({
  81. 'software_id': task.plan.software.id,
  82. 'software_name': task.plan.software.name,
  83. 'version': task.plan.version,
  84. 'version_file': {'file_name': version_file.path.split('/')[-1], 'file_url': HTTP_HEAD + SOFTWARE_ROOT
  85. + version_file.path},
  86. 'plan': {
  87. 'id': task.plan.id,
  88. 'title': task.plan.title,
  89. 'description': task.plan.description,
  90. 'state': task.plan.state,
  91. 'statements': [{'file_id': file_sid, 'file_name': file_name, 'file_url': HTTP_HEAD + file_url} for
  92. file_sid, file_name, file_url in zip(plan_sids, plan_file_names, plan_file_urls)],
  93. 'creator': {'executor_id': task.plan.creator.id, 'executor_name': task.plan.creator.username},
  94. },
  95. 'task': {
  96. 'id': task.id,
  97. 'title': task.title,
  98. 'description': task.description,
  99. 'state': task.state,
  100. 'statements': [{'file_id': file_sid, 'file_name': file_name, 'file_url': HTTP_HEAD + file_url} for
  101. file_sid, file_name, file_url in zip(task_sids, task_file_names, task_file_urls)],
  102. 'case_template': HTTP_HEAD + CASE_TEMPLATE_ROOT,
  103. 'case_file': case_file_info,
  104. 'executor': {'id': task.executor.id, 'name': task.executor.username},
  105. 'case_all': task.case_all,
  106. 'case_not_execute': task.case_not_execute,
  107. 'case_success': task.case_success,
  108. 'case_fail': task.case_fail,
  109. },
  110. 'create_time': task.create_time,
  111. 'update_time': task.update_time,
  112. })
  113. class TaskEdit(APIView):
  114. # 登录权限验证
  115. authentication_classes = []
  116. # edit task
  117. @staticmethod
  118. @RoleControl
  119. def post(request, task_id, *args, **kwargs):
  120. title = request.POST.get('title')
  121. state = request.POST.get('state')
  122. description = request.POST.get('description')
  123. files = request.FILES.getlist('files')
  124. edit_state_only = request.POST.get('edit_state_only')
  125. task = TestTask.objects.filter(id=task_id, delete=False)
  126. if not task:
  127. logger.error("测试任务已删除或不存在")
  128. return HttpResponse(status=404, content='测试任务已删除或不存在')
  129. task = task[0]
  130. if edit_state_only == '1':
  131. task.state = int(state)
  132. task.save()
  133. return HttpResponse(status=200, content='编辑已保存')
  134. if title:
  135. task.title = title
  136. if state:
  137. task.state = int(state)
  138. if description:
  139. task.description = description
  140. fids = ''
  141. for file in files:
  142. create_time = update_time = datetime.datetime.now()
  143. try:
  144. write_file(file,
  145. TASK_ROOT + "/" + task_id + "/" + str(create_time))
  146. except:
  147. logger.error("文件上传失败")
  148. return HttpResponse(status=500, content="文件上传失败")
  149. id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
  150. try:
  151. file_new = FileManager.objects.create(id=id_file,
  152. category="plan_statement",
  153. path=task_id + "/" + str(
  154. create_time) + "/" + file.name,
  155. create_time=create_time,
  156. update_time=update_time)
  157. except:
  158. logger.error("文件路径写入数据库失败")
  159. return HttpResponse(status=500, content="文件路径写入数据库失败")
  160. fids += id_file + '&&'
  161. fids = fids[:-2]
  162. task_sids = task.statement_file.split('&&')
  163. print(task_sids)
  164. if task_sids is not None and task_sids[0] != '':
  165. for statement_id in task_sids:
  166. statement = FileManager.objects.get(id=statement_id)
  167. statement.delete = True
  168. statement.save()
  169. task.statement_file = fids
  170. task.save()
  171. executor, action, method = get_log(request)
  172. gen_log(action, "测试任务", task.title, method, executor)
  173. return HttpResponse("编辑已保存")