planview.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, HTTP_HEAD
  6. from apps.log.models import get_log, gen_log
  7. from apps.plan.models import TestPlan
  8. from apps.file.models import FileManager
  9. from utils.util_add_id import get_id
  10. from utils.util_file.util_fileio import write_file
  11. import logging
  12. logger = logging.getLogger('django')
  13. class PlanView(APIView):
  14. # 登录权限验证
  15. authentication_classes = []
  16. # view plan
  17. @staticmethod
  18. def get(request, plan_id, *args, **kwargs):
  19. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  20. if not plan:
  21. logger.error("测试计划不存在")
  22. return HttpResponse(status=404, content='测试计划不存在')
  23. plan = plan[0]
  24. plan_sids = plan.statement_file.split('&&')
  25. file_names = []
  26. file_urls = []
  27. if plan_sids[0]:
  28. for statement_id in plan_sids:
  29. statement = FileManager.objects.get(id=statement_id)
  30. file_urls.append(PLAN_ROOT + statement.path)
  31. file_names.append(statement.path.split('/')[-1])
  32. tasks_info = []
  33. tasks = plan.testtask_set.filter(delete=False)
  34. if tasks:
  35. for task in tasks:
  36. tasks_info.append({
  37. 'id': task.id,
  38. 'title': task.title,
  39. 'description': task.description,
  40. 'state': task.state,
  41. 'executor': {'id': task.executor.id, 'name': task.executor.username},
  42. 'create_time': task.create_time,
  43. 'update_time': task.update_time
  44. })
  45. executor, action, method = get_log(request)
  46. gen_log(action, "测试计划", plan.title, method, executor)
  47. return Response({
  48. 'software_id': plan.software.id,
  49. 'software_name': plan.software.name,
  50. 'version': plan.version,
  51. 'creator_id': plan.creator.id,
  52. 'creator': plan.creator.username,
  53. 'id': plan.id,
  54. 'title': plan.title,
  55. 'description': plan.description,
  56. 'state': plan.state,
  57. 'type': plan.type,
  58. 'tasks': tasks_info,
  59. 'create_time': plan.create_time,
  60. 'update_time': plan.update_time,
  61. 'statements': [{'file_id': file_sid, 'name': file_name, 'url': HTTP_HEAD + file_url} for file_sid, file_name
  62. , file_url in zip(plan_sids, file_names, file_urls)]
  63. })
  64. # delete plan_list
  65. @staticmethod
  66. def delete(request, plan_id, *args, **kwargs):
  67. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  68. if not plan:
  69. logger.error("测试计划已删除或不存在")
  70. return HttpResponse(status=404, content='测试计划已删除或不存在')
  71. plan = plan[0]
  72. plan_sids = plan.statement_file.split('&&')
  73. if plan_sids[0]:
  74. for statement_id in plan_sids:
  75. statement = FileManager.objects.get(id=statement_id)
  76. statement.delete = True
  77. statement.save()
  78. plan.delete = True
  79. plan.save()
  80. executor, action, method = get_log(request)
  81. gen_log(action, "测试计划", plan.title, method, executor)
  82. return HttpResponse("删除完成")
  83. class PlanEdit(APIView):
  84. # 登录权限验证
  85. authentication_classes = []
  86. # edit plan_list
  87. @staticmethod
  88. def post(request, plan_id, *args, **kwargs):
  89. title = request.POST.get('title')
  90. state = request.POST.get('state')
  91. description = request.POST.get('description')
  92. files = request.FILES.getlist('files')
  93. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  94. if not plan:
  95. logger.error("测试计划已删除或不存在")
  96. return HttpResponse(status=404, content='测试计划已删除或不存在')
  97. plan = plan[0]
  98. plan.title = title
  99. plan.state = state
  100. plan.description = description
  101. fids = ''
  102. for file in files:
  103. create_time = update_time = datetime.datetime.now()
  104. try:
  105. write_file(file,
  106. PLAN_ROOT + "/" + plan_id + "/" + str(create_time))
  107. except:
  108. logger.error("文件上传失败")
  109. return HttpResponse(status=500, content="文件上传失败")
  110. id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
  111. try:
  112. file_new = FileManager.objects.create(id=id_file,
  113. category="plan_statement",
  114. path=plan_id + "/" + str(
  115. create_time) + "/" + file.name,
  116. create_time=create_time,
  117. update_time=update_time)
  118. except:
  119. logger.error("文件路径写入数据库失败")
  120. return HttpResponse(status=500, content="文件路径写入数据库失败")
  121. fids += id_file + '&&'
  122. fids = fids[:-2]
  123. plan.statement_file += fids
  124. plan.save()
  125. executor, action, method = get_log(request)
  126. gen_log(action, "测试计划", plan.title, method, executor)
  127. return HttpResponse("编辑已保存")
  128. class PlanEditType(APIView):
  129. # 登录权限验证
  130. authentication_classes = []
  131. @staticmethod
  132. def post(request, plan_id, *args, **kwargs):
  133. plan_type = request.POST.get('type')
  134. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  135. if not plan:
  136. logger.error("测试计划已删除或不存在")
  137. return HttpResponse(status=404, content='测试计划已删除或不存在')
  138. plan = plan[0]
  139. plan.type = plan_type
  140. plan.save()
  141. executor, action, method = get_log(request)
  142. gen_log(action, "测试计划类型", plan.title, method, executor)
  143. return HttpResponse("编辑已保存")