softwarelistview.py 4.7 KB

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