planview.py 5.8 KB

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