softwareview.py 5.1 KB

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