planlistview.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import datetime
  2. import json
  3. import os
  4. from rest_framework.response import Response
  5. from rest_framework.views import APIView
  6. from django.http import HttpResponse
  7. from apps.file.models import FileManager
  8. from apps.log.models import get_log, gen_log
  9. from apps.plan.models import TestPlan
  10. from apps.software.models import Software
  11. from apps.user.middleware.rolecontrol import RoleControl
  12. from apps.user.models import User
  13. from utils.util_add_id import gen_next_id, get_id
  14. from TestLaboratory.settings import PLAN_ROOT
  15. import logging
  16. logger = logging.getLogger('django')
  17. class PlanListView(APIView):
  18. # 登录权限验证
  19. authentication_classes = []
  20. # create plan
  21. @staticmethod
  22. @RoleControl
  23. def post(request, *args, **kwargs):
  24. software_id = request.POST.get('software_id')
  25. version_number = request.POST.get('version_number')
  26. title = request.POST.get('title')
  27. description = request.POST.get('description')
  28. files = request.FILES.getlist('files')
  29. creator_id = request.POST.get('creator')
  30. # step1:判断所属软件及版本是否存在
  31. software = Software.objects.filter(id=software_id, delete=False)
  32. if not software:
  33. logger.error("软件已删除或不存在")
  34. return HttpResponse(status=404, content='软件已删除或不存在')
  35. software = software[0]
  36. # 判断版本是否存在
  37. software_version = software.version_set.filter(number=version_number, delete=False)
  38. if not software_version:
  39. logger.error("软件版本不存在或已删除,请重新选择")
  40. return HttpResponse(status=404, content='软件版本不存在或已删除,请重新选择')
  41. software_version = software_version[0]
  42. # 判断用户是否存在
  43. creator = User.objects.filter(id=creator_id, delete=False)
  44. if not creator:
  45. logger.error("用户不存在,创建失败")
  46. return HttpResponse(status=404, content='用户不存在,创建失败')
  47. creator = creator[0]
  48. # step5:设置创建时间和修改时间
  49. create_time = update_time = datetime.datetime.now()
  50. # step6:创建测试计划的数据库记录
  51. id_plan = get_id(TestPlan, "TestLaboratory_V1_Plan_1")
  52. # step7:创建文件的数据库字段
  53. fids = ''
  54. file_paths = []
  55. file_ids = []
  56. for file in files:
  57. try:
  58. file_dir = PLAN_ROOT + "/" + id_plan + "/" + str(create_time)
  59. if not os.path.exists(file_dir):
  60. os.makedirs(file_dir)
  61. file_path = file_dir + "/" + file.name
  62. with open(file_path, 'wb') as f:
  63. f.write(file.read())
  64. f.close()
  65. except:
  66. logger.error("文件上传失败")
  67. return HttpResponse(status=500, content="文件上传失败")
  68. if not FileManager.objects.last():
  69. id_file = "TestLaboratory_V1_File_1"
  70. else:
  71. id_file = gen_next_id(FileManager.objects.order_by('create_time').last().id)
  72. try:
  73. file_new = FileManager.objects.create(id=id_file, category="plan_statement",
  74. path=id_plan + "/" + str(create_time) + "/" + file.name,
  75. create_time=create_time,
  76. update_time=update_time)
  77. except:
  78. logger.error("文件路径写入数据库失败")
  79. return HttpResponse(status=500, content="文件路径写入数据库失败")
  80. file_ids.append(id_file)
  81. file_paths.append(PLAN_ROOT + file_new.path)
  82. fids += id_file + "&&"
  83. fids = fids[:-2]
  84. try:
  85. plan = TestPlan.objects.create(id=id_plan, title=title, description=description, state='0',
  86. creator=creator, software=software, version=software_version.number,
  87. statement_file=fids,
  88. create_time=create_time, update_time=update_time)
  89. except:
  90. logger.error("测试计划写入数据库失败")
  91. return HttpResponse(status=500, content='测试计划写入数据库失败')
  92. executor, action, method = get_log(request)
  93. gen_log(action, "测试计划", plan.title, method, executor)
  94. return Response({
  95. 'software_id': software.id,
  96. 'software_name': software.name,
  97. 'version': version_number,
  98. 'creator_id': creator.id,
  99. 'creator': creator.username,
  100. 'id': plan.id,
  101. 'title': title,
  102. 'description': description,
  103. 'state': '执行中',
  104. 'files': [{'file_id': file_id, 'file_name': file.name, 'file_url': file_path} for (file_id, file, file_path)
  105. in zip(file_ids, files, file_paths)]
  106. })
  107. # view plan_list
  108. @staticmethod
  109. @RoleControl
  110. def get(request, *args, **kwargs):
  111. title = request.GET.get('title')
  112. software_id = request.GET.get('software_id')
  113. state = request.GET.get('state')
  114. creator_id = request.GET.get('creator_id')
  115. sort = request.GET.get('sort')
  116. plan_all = TestPlan.objects.order_by('-create_time')
  117. if title:
  118. plan_all = plan_all.filter(title__contains=title)
  119. if software_id:
  120. plan_all = plan_all.filter(software__id=software_id)
  121. if state:
  122. plan_all = plan_all.filter(state=state)
  123. if creator_id:
  124. plan_all = plan_all.filter(creator__id__contains=creator_id)
  125. """
  126. sort=update_time, -update_time, create_time, -create_time, title, -title, state
  127. """
  128. if sort:
  129. plan_all = plan_all.order_by(sort)
  130. plan_all = plan_all.filter(delete=False)
  131. if not plan_all:
  132. return Response([])
  133. info = []
  134. for plan in plan_all.all():
  135. info.append({
  136. 'software_id': plan.software.id,
  137. 'software_name': plan.software.name,
  138. 'version': plan.version,
  139. 'creator_id': plan.creator.id,
  140. 'creator': plan.creator.username,
  141. 'id': plan.id,
  142. 'title': plan.title,
  143. 'description': plan.description,
  144. 'state': plan.state,
  145. 'create_time': plan.create_time,
  146. 'update_time': plan.update_time,
  147. })
  148. return Response(info)