softwareplanview.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import datetime
  2. import json
  3. import logging
  4. from django.http import HttpResponse
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from apps.log.models import get_log, gen_log
  8. from apps.plan.models import TestPlan
  9. from apps.software.models import Software
  10. from apps.task.models import TestTask
  11. from apps.user.middleware.rolecontrol import RoleControl
  12. from django.forms.models import model_to_dict
  13. from apps.user.models import User
  14. from utils.util_add_id import get_id
  15. logger = logging.getLogger('django')
  16. class SoftwarePlanView(APIView):
  17. authentication_classes = []
  18. # generate report
  19. @staticmethod
  20. @RoleControl
  21. def get(request, software_id, *args, **kwargs):
  22. software = Software.objects.filter(id=software_id, delete=False)
  23. if not software:
  24. logger.error("软件已删除或不存在")
  25. return HttpResponse(status=404, content='软件已删除或不存在')
  26. plan_all = TestPlan.objects.order_by('-create_time')
  27. plan_all = plan_all.filter(software__id__contains=software_id)
  28. info = []
  29. plan_all = plan_all.filter(delete=False)
  30. names = set()
  31. for plan in plan_all.all():
  32. if plan.title not in names:
  33. names.add(plan.title)
  34. info.append({
  35. 'software_id': plan.software.id,
  36. 'software_name': plan.software.name,
  37. 'version': plan.version,
  38. 'creator_id': plan.creator.id,
  39. 'creator': plan.creator.username,
  40. 'id': plan.id,
  41. 'type': plan.type,
  42. 'title': plan.title,
  43. 'description': plan.description,
  44. 'state': plan.state,
  45. 'create_time': plan.create_time,
  46. 'update_time': plan.update_time,
  47. })
  48. return Response(info)
  49. class SoftwareVersionPlanView(APIView):
  50. authentication_classes = []
  51. # generate report
  52. @staticmethod
  53. @RoleControl
  54. def post(request, software_id, *args, **kwargs):
  55. params = json.loads(request.body)
  56. version_number = params['version_number']
  57. plan_ids = params['plan_ids']
  58. software = Software.objects.filter(id=software_id, delete=False)
  59. if not software:
  60. logger.error("软件已删除或不存在")
  61. return HttpResponse(status=404, content='软件已删除或不存在')
  62. software = software[0]
  63. plan_all = TestPlan.objects.order_by('-create_time').filter(delete=False)
  64. plan_all = plan_all.filter(id__in=plan_ids)
  65. for plan in plan_all:
  66. create_time = update_time = datetime.datetime.now()
  67. plan_info = model_to_dict(plan)
  68. plan_info['id'] = get_id(TestPlan, "TestLaboratory_V1_Plan_1")
  69. plan_info['version'] = version_number
  70. plan_info['state'] = 0
  71. plan_info['create_time'] = create_time
  72. plan_info['update_time'] = update_time
  73. plan_info['software'] = software
  74. plan_info['creator'] = plan.creator
  75. new_plan = TestPlan.objects.create(**plan_info)
  76. tasks = plan.testtask_set.filter(delete=False)
  77. if tasks:
  78. for task in tasks:
  79. task_info = model_to_dict(task)
  80. task_info['id'] = get_id(TestTask, "TestLaboratory_V1_Task_1")
  81. task_info['state'] = 0
  82. task_info['create_time'] = create_time
  83. task_info['update_time'] = update_time
  84. task_info['plan'] = new_plan
  85. task_info['executor'] = task.executor
  86. new_task = TestTask.objects.create(**task_info)
  87. executor, action, method = get_log(request)
  88. gen_log(action, "版本测试计划", plan.title, method, executor)
  89. return HttpResponse("关联成功")