softwareview.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import json
  2. from django.http import HttpResponse
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. from TestLaboratory.settings import SOFTWARE_ROOT, HTTP_HEAD
  6. from apps.company.models import Company
  7. from apps.file.models import FileManager
  8. from apps.log.models import get_log, gen_log
  9. from apps.software.models import Software
  10. from django.http.request import QueryDict
  11. from apps.user.middleware.rolecontrol import RoleControl
  12. import logging
  13. logger = logging.getLogger('django')
  14. class SoftwareView(APIView):
  15. # 登录权限验证
  16. authentication_classes = []
  17. # view software
  18. @staticmethod
  19. @RoleControl
  20. def get(request, software_id, *args, **kwargs):
  21. software = Software.objects.filter(id=software_id, delete=False)
  22. if not software:
  23. logger.error("软件已删除或不存在")
  24. return HttpResponse(status=404, content='软件已删除或不存在')
  25. software = software[0]
  26. try:
  27. versions = []
  28. for version in software.version_set.filter(delete=False):
  29. version_fid = version.version_file
  30. file_path = FileManager.objects.filter(id=version_fid)[0].path
  31. versions.append({
  32. 'id': version.id, 'version': version.number,
  33. 'file': file_path.split('/')[-1],
  34. 'file_url': HTTP_HEAD+SOFTWARE_ROOT+file_path,
  35. 'create_time': version.create_time, 'update_time': version.update_time
  36. })
  37. task_num = 0
  38. if software.testplan_set.filter(delete=False):
  39. for plan in software.testplan_set.filter(delete=False):
  40. task_num += len(plan.testtask_set.filter(delete=False))
  41. company_name = ''
  42. try:
  43. company = Company.objects.get(id=software.company_id)
  44. company_name = company.name
  45. except:
  46. pass
  47. software_info = {'name': software.name, 'type': software.type, 'create_time': software.create_time,
  48. 'update_time': software.update_time,
  49. 'versions': versions,
  50. 'company_name':company_name,
  51. 'case_num': len(software.testcase_set.filter(delete=False)),
  52. 'task_num': task_num}
  53. except:
  54. logger.error("测试项目查询失败")
  55. return HttpResponse(status=500, content='查询失败')
  56. executor, action, method = get_log(request)
  57. gen_log(action, "测试目标", software.name, method, executor)
  58. return Response(software_info)
  59. # delete software
  60. @staticmethod
  61. @RoleControl
  62. def delete(request, software_id, *args, **kwargs):
  63. software = Software.objects.filter(id=software_id, delete=False)
  64. if not software:
  65. logger.error("软件已删除或不存在")
  66. return HttpResponse(status=404, content='软件{}已删除或不存在'.format(software_id))
  67. software = software[0]
  68. if software.version_set.exists():
  69. for version in software.version_set.filter():
  70. version_fid = version.version_file
  71. file_version = FileManager.objects.filter(id=version_fid)[0]
  72. file_version.delete = True
  73. file_version.save()
  74. # 删除测试计划
  75. plan_set = software.testplan_set.filter(delete=False)
  76. if plan_set:
  77. for plan in plan_set:
  78. task_set = plan.testtask_set.filter(delete=False)
  79. if task_set:
  80. for task in task_set:
  81. task.delete = True
  82. task.save()
  83. plan.delete = True
  84. plan.save()
  85. version.delete = True
  86. version.save()
  87. software.delete = True
  88. software.save()
  89. executor, action, method = get_log(request)
  90. gen_log(action, "测试目标", software.name, method, executor)
  91. return HttpResponse(status=200, content='删除完成')
  92. class SoftwareEdit(APIView):
  93. # 登录权限验证
  94. authentication_classes = []
  95. # edit software
  96. @staticmethod
  97. @RoleControl
  98. def post(request, software_id, *args, **kwargs):
  99. software = Software.objects.filter(id=software_id, delete=False)
  100. if not software:
  101. logger.error("软件已删除或不存在")
  102. return HttpResponse(status=404, content='软件已删除或不存在')
  103. software = software[0]
  104. name = request.POST.get('name')
  105. software_type = request.POST.get('type')
  106. if not name:
  107. logger.error("软件名不能为空")
  108. return HttpResponse(status=500, content='软件名不能为空')
  109. if name == software.name and software_type == software.type:
  110. logger.error("未监测到软件名称及类型变化,编辑不保存")
  111. return HttpResponse(status=500, content='未监测到软件名称及类型变化,编辑不保存')
  112. software.name = name
  113. software.type = software_type
  114. software.save()
  115. # print(software)
  116. executor, action, method = get_log(request)
  117. gen_log(action, "测试目标", software.name, method, executor)
  118. return HttpResponse(status=200, content='编辑已保存')