beidoutask.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, PLAN_ROOT, CASE_FILE_ROOT, SOFTWARE_ROOT
  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, BeidouCase
  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. def model_to_dict(model_obj, ignore=()):
  17. '''
  18. 将一个model对象转换成字典
  19. '''
  20. att_dict = {}
  21. for field in model_obj._meta.fields:
  22. name = field.attname # 获取字段名
  23. value = getattr(model_obj, name) # 获取对象属性
  24. if name in ignore:
  25. continue
  26. # print(name,value)
  27. # 检查传入的数据能否被序列化
  28. if isinstance(value, (int, float)) and name not in ['delete']:
  29. att_dict[name] = value # 生成字典
  30. return att_dict
  31. class BeidouTaskEditView(APIView):
  32. # 登录权限验证
  33. authentication_classes = []
  34. # create task
  35. @staticmethod
  36. @RoleControl
  37. def post(request, task_id=None, *args, **kwargs):
  38. task_name_old = request.POST.get('task_name_old')
  39. task_name_new = request.POST.get('task_name_new')
  40. description = request.POST.get('description')
  41. statement_files = request.FILES.getlist('files')
  42. plan_id = request.POST.get('plan_id')
  43. # step1:判断所属软件及版本是否存在
  44. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  45. if not plan:
  46. logger.error("测试计划已删除或不存在")
  47. return HttpResponse(status=404, content='测试计划已删除或不存在')
  48. task_all = TestTask.objects.filter(plan_id=plan_id, title=task_name_old)
  49. if not task_all:
  50. logger.error("测试任务不存在")
  51. return HttpResponse(status=404, content='测试任务不存在')
  52. for task in task_all.all():
  53. if task_name_new:
  54. task.title = task_name_new
  55. if description:
  56. task.description = description
  57. fids = ''
  58. for file in statement_files:
  59. create_time = update_time = datetime.datetime.now()
  60. try:
  61. write_file(file,
  62. TASK_ROOT + "/" + task.id + "/" + str(create_time))
  63. except:
  64. logger.error("文件上传失败")
  65. return HttpResponse(status=500, content="文件上传失败")
  66. id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
  67. try:
  68. file_new = FileManager.objects.create(id=id_file,
  69. category="plan_statement",
  70. path=task.id + "/" + str(
  71. create_time) + "/" + file.name,
  72. create_time=create_time,
  73. update_time=update_time)
  74. except:
  75. logger.error("文件路径写入数据库失败")
  76. return HttpResponse(status=500, content="文件路径写入数据库失败")
  77. fids += id_file + '&&'
  78. fids = fids[:-2]
  79. task_sids = task.statement_file.split('&&')
  80. if task_sids is not None and task_sids[0] != '':
  81. for statement_id in task_sids:
  82. statement = FileManager.objects.get(id=statement_id)
  83. statement.delete = True
  84. statement.save()
  85. task.statement_file = fids
  86. task.save()
  87. executor, action, method = get_log(request)
  88. gen_log(action, "北斗测试任务", task_name_new, method, executor)
  89. return HttpResponse("编辑已保存")
  90. @staticmethod
  91. @RoleControl
  92. def delete(request, *args, **kwargs):
  93. task_name = request.POST.get('task_name')
  94. plan_id = request.POST.get('plan_id')
  95. # step1:判断所属软件及版本是否存在
  96. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  97. if not plan:
  98. logger.error("测试计划已删除或不存在")
  99. return HttpResponse(status=404, content='测试计划已删除或不存在')
  100. task_all = TestTask.objects.filter(plan_id=plan_id, title=task_name)
  101. if not task_all:
  102. logger.error("测试任务已删除或不存在")
  103. return HttpResponse(status=404, content='测试任务已删除或不存在')
  104. for task in task_all.all():
  105. task_sids = task.statement_file.split('&&')
  106. if task_sids[0]:
  107. for statement_id in task_sids:
  108. statement = FileManager.objects.get(id=statement_id)
  109. statement.delete = True
  110. statement.save()
  111. task.delete = True
  112. task.save()
  113. executor, action, method = get_log(request)
  114. gen_log(action, "北斗测试任务", task.title, method, executor)
  115. return HttpResponse("删除完成")
  116. class BeidouTaskView(APIView):
  117. # 登录权限验证
  118. authentication_classes = []
  119. # create task
  120. @staticmethod
  121. @RoleControl
  122. def get(request, task_id=None, *args, **kwargs):
  123. task = TestTask.objects.filter(id=task_id, delete=False)
  124. if not task:
  125. logger.error("测试计划已删除或不存在")
  126. return HttpResponse(status=404, content='测试计划已删除或不存在')
  127. task = task[0]
  128. version = task.plan.software.version_set.filter(number=task.plan.version, delete=False)
  129. if not version:
  130. logger.error("测试任务对应软件版本不存在或已删除")
  131. return HttpResponse(status=404, content='测试任务对应软件版本不存在或已删除')
  132. version = version[0]
  133. version_fid = version.version_file
  134. version_file = FileManager.objects.filter(id=version_fid)
  135. if not version_file:
  136. logger.error("无法定位测试任务对应的软件实体")
  137. return HttpResponse(status=404, content='无法定位测试任务对应的软件实体')
  138. version_file = version_file[0]
  139. plan_sids = task.plan.statement_file.split('&&')
  140. plan_file_names = []
  141. plan_file_urls = []
  142. if plan_sids[0]:
  143. for plan_statement_id in plan_sids:
  144. plan_statement = FileManager.objects.get(id=plan_statement_id)
  145. plan_file_urls.append(PLAN_ROOT + plan_statement.path)
  146. plan_file_names.append(plan_statement.path.split('/')[-1])
  147. task_sids = task.statement_file.split('&&')
  148. task_file_names = []
  149. task_file_urls = []
  150. if task_sids[0]:
  151. for task_statement_id in task_sids:
  152. task_statement = FileManager.objects.get(id=task_statement_id)
  153. task_file_urls.append(TASK_ROOT + task_statement.path)
  154. task_file_names.append(task_statement.path.split('/')[-1])
  155. case_info = {}
  156. beidou_case = BeidouCase.objects.filter(task_id=task_id)
  157. if beidou_case:
  158. beidou_case = beidou_case[0]
  159. case_info = model_to_dict(beidou_case)
  160. return Response({
  161. 'software_id': task.plan.software.id,
  162. 'software_name': task.plan.software.name,
  163. 'version': task.plan.version,
  164. 'version_file': {'file_name': version_file.path.split('/')[-1], 'file_url': HTTP_HEAD + SOFTWARE_ROOT
  165. + version_file.path},
  166. 'plan': {
  167. 'id': task.plan.id,
  168. 'title': task.plan.title,
  169. 'description': task.plan.description,
  170. 'state': task.plan.state,
  171. 'statements': [{'file_id': file_sid, 'file_name': file_name, 'file_url': HTTP_HEAD + file_url} for
  172. file_sid, file_name, file_url in zip(plan_sids, plan_file_names, plan_file_urls)],
  173. 'creator': {'executor_id': task.plan.creator.id, 'executor_name': task.plan.creator.username},
  174. },
  175. 'task': {
  176. 'id': task.id,
  177. 'title': task.title,
  178. 'description': task.description,
  179. 'state': task.state,
  180. 'statements': [{'file_id': file_sid, 'file_name': file_name, 'file_url': HTTP_HEAD + file_url} for
  181. file_sid, file_name, file_url in zip(task_sids, task_file_names, task_file_urls)],
  182. 'executor': {'id': task.executor.id, 'name': task.executor.username},
  183. 'result': case_info
  184. },
  185. 'create_time': task.create_time,
  186. 'update_time': task.update_time,
  187. })