planlistview.py 7.3 KB

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