planview.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 'tasks': tasks_info,
  58. 'create_time': plan.create_time,
  59. 'update_time': plan.update_time,
  60. 'statements': [{'file_id': file_sid, 'name': file_name, 'url': HTTP_HEAD + file_url} for file_sid, file_name
  61. , file_url in zip(plan_sids, file_names, file_urls)]
  62. })
  63. # delete plan_list
  64. @staticmethod
  65. def delete(request, plan_id, *args, **kwargs):
  66. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  67. if not plan:
  68. logger.error("测试计划已删除或不存在")
  69. return HttpResponse(status=404, content='测试计划已删除或不存在')
  70. plan = plan[0]
  71. plan_sids = plan.statement_file.split('&&')
  72. if plan_sids[0]:
  73. for statement_id in plan_sids:
  74. statement = FileManager.objects.get(id=statement_id)
  75. statement.delete = True
  76. statement.save()
  77. plan.delete = True
  78. plan.save()
  79. executor, action, method = get_log(request)
  80. gen_log(action, "测试计划", plan.title, method, executor)
  81. return HttpResponse("删除完成")
  82. class PlanEdit(APIView):
  83. # 登录权限验证
  84. authentication_classes = []
  85. # edit plan_list
  86. @staticmethod
  87. def post(request, plan_id, *args, **kwargs):
  88. title = request.POST.get('title')
  89. state = request.POST.get('state')
  90. description = request.POST.get('description')
  91. files = request.FILES.getlist('files')
  92. plan = TestPlan.objects.filter(id=plan_id, delete=False)
  93. if not plan:
  94. logger.error("测试计划已删除或不存在")
  95. return HttpResponse(status=404, content='测试计划已删除或不存在')
  96. plan = plan[0]
  97. plan.title = title
  98. plan.state = state
  99. plan.description = description
  100. fids = ''
  101. for file in files:
  102. create_time = update_time = datetime.datetime.now()
  103. try:
  104. write_file(file,
  105. PLAN_ROOT + "/" + plan_id + "/" + str(create_time))
  106. except:
  107. logger.error("文件上传失败")
  108. return HttpResponse(status=500, content="文件上传失败")
  109. id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
  110. try:
  111. file_new = FileManager.objects.create(id=id_file,
  112. category="plan_statement",
  113. path=plan_id + "/" + str(
  114. create_time) + "/" + file.name,
  115. create_time=create_time,
  116. update_time=update_time)
  117. except:
  118. logger.error("文件路径写入数据库失败")
  119. return HttpResponse(status=500, content="文件路径写入数据库失败")
  120. fids += id_file + '&&'
  121. fids = fids[:-2]
  122. plan.statement_file += fids
  123. plan.save()
  124. executor, action, method = get_log(request)
  125. gen_log(action, "测试计划", plan.title, method, executor)
  126. return HttpResponse("编辑已保存")