softwarelistview.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import datetime
  2. import os
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. from django.http import HttpResponse, JsonResponse, Http404
  6. from TestLaboratory.settings import SOFTWARE_ROOT
  7. from apps.company.models import Company
  8. from apps.file.models import FileManager
  9. from apps.log.models import gen_log, get_log
  10. from apps.software.models import Software, create_software, get_software, Version
  11. import json
  12. from apps.user.middleware.rolecontrol import RoleControl
  13. from utils.util_add_id import gen_next_id
  14. import logging
  15. logger = logging.getLogger('django')
  16. class SoftwareListView(APIView):
  17. # create software
  18. @staticmethod
  19. @RoleControl
  20. def post(request, *args, **kwargs):
  21. name = request.POST.get('name')
  22. software_type = request.POST.get('type')
  23. version_num = request.POST.get('version')
  24. creator_id = request.POST.get('creator_id')
  25. company_id = request.POST.get('company_id')
  26. file = request.FILES.get('file')
  27. if company_id == '':
  28. company_id = -1
  29. if not file:
  30. logger.error("软件上传失败")
  31. return HttpResponse(status=404, content='软件上传失败')
  32. # create_msg = create_software(name, software_type, version, file)
  33. id_ = None
  34. if not Software.objects.order_by('create_time').last():
  35. id_ = "TestLaboratory_V1_Software_1"
  36. else:
  37. id_ = gen_next_id(Software.objects.order_by('create_time').last().id)
  38. create_time = update_time = datetime.datetime.now()
  39. # step4:创建版本id
  40. id_version = None
  41. if not Version.objects.last():
  42. id_version = "TestLaboratory_V1_Version_1"
  43. else:
  44. id_version = gen_next_id(Version.objects.order_by('create_time').last().id)
  45. # step6:上传文件:
  46. file_dir = SOFTWARE_ROOT + id_ + "/" + id_version + "/" + str(create_time)
  47. if not os.path.exists(file_dir):
  48. os.makedirs(file_dir)
  49. file_path = file_dir + "/" + file.name
  50. with open(file_path, 'wb') as f:
  51. f.write(file.read())
  52. f.close()
  53. # step7:创建文件对象
  54. id_file = None
  55. if not FileManager.objects.last():
  56. id_file = "TestLaboratory_V1_File_1"
  57. else:
  58. id_file = gen_next_id(FileManager.objects.order_by('create_time').last().id)
  59. try:
  60. file_new = FileManager.objects.create(id=id_file, category="software",
  61. path=id_ + "/" + id_version + "/" +
  62. str(create_time) + "/" + file.name,
  63. create_time=create_time, update_time=update_time)
  64. # step3:数据库新增软件字段
  65. software = Software.objects.create(name=name, id=id_, type=software_type, company_id=company_id,
  66. creator_id=creator_id,
  67. create_time=create_time,
  68. update_time=update_time)
  69. # step5:创建版本
  70. version = Version.objects.create(id=id_version, number=version_num, software=software,
  71. version_file=id_file,
  72. create_time=create_time,
  73. update_time=update_time)
  74. except:
  75. logger.error("测试项目创建失败")
  76. return HttpResponse(content='创建失败', status=500)
  77. executor, action, method = get_log(request)
  78. gen_log(action, "测试目标", software.name, method, executor)
  79. return Response({
  80. 'id': software.id,
  81. 'name': software.name,
  82. 'type': software.type,
  83. 'create_time': software.create_time,
  84. 'update_time': software.update_time,
  85. 'company_id': software.company_id,
  86. })
  87. # software list
  88. @staticmethod
  89. @RoleControl
  90. def get(request, *args, **kwargs):
  91. name = request.GET.get('name')
  92. software_type = request.GET.get('type')
  93. sort = request.GET.get('sort')
  94. creator_id = request.GET.get('creator_id')
  95. company_id = request.GET.get('company_id')
  96. software_all = Software.objects.order_by('-create_time')
  97. if name:
  98. software_all = software_all.filter(name__contains=name)
  99. if software_type:
  100. software_all = software_all.filter(type__contains=software_type)
  101. if creator_id:
  102. software_all = software_all.filter(creator_id=creator_id)
  103. if company_id:
  104. company_id = int(company_id)
  105. software_all = software_all.filter(company_id=company_id)
  106. """
  107. sort=update_time, -update_time, create_time, -create_time, name, -name, type, -type
  108. """
  109. if sort:
  110. software_all = software_all.order_by(sort)
  111. software_all = software_all.filter(delete=False)
  112. if not software_all:
  113. return Response([])
  114. info = []
  115. for software in software_all.all():
  116. company_name = ''
  117. try:
  118. company = Company.objects.get(id=software.company_id)
  119. company_name = company.name
  120. except:
  121. pass
  122. info.append({
  123. 'id': software.id,
  124. 'name': software.name,
  125. 'type': software.type,
  126. 'create_time': software.create_time,
  127. 'update_time': software.update_time,
  128. 'company_id': software.company_id,
  129. 'company_name': company_name,
  130. })
  131. return Response(info)