planview.py 5.4 KB

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