taskview.py 8.5 KB

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