beidoutaskexecute.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import datetime
  2. import os
  3. from django.db.models import Q
  4. from django.http import HttpResponse, HttpResponseBadRequest
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from TestLaboratory.settings import CASE_FILE_ROOT, CASE_FILE_EXPORT, HTTP_HEAD
  8. from apps.file.models import FileManager
  9. from apps.log.models import get_log, gen_log
  10. from apps.task.models import TestTask, TestCase, BeidouCase
  11. from apps.user.middleware.rolecontrol import RoleControl
  12. from util_xlsx import load_xlsx, write_to_xlsx
  13. from utils.util_add_id import get_id
  14. from utils.util_file.util_fileio import write_file
  15. import logging
  16. logger = logging.getLogger('django')
  17. class BeidouCaseExecuteView(APIView):
  18. # 登录权限验证
  19. authentication_classes = []
  20. # execute task
  21. @staticmethod
  22. @RoleControl
  23. def post(request, task_id, *args, **kwargs):
  24. executor, action, method = get_log(request)
  25. task = TestTask.objects.filter(id=task_id, delete=False)
  26. if not task:
  27. logger.error("测试任务已删除或不存在")
  28. return HttpResponseBadRequest(status=404, content='测试任务已删除或不存在')
  29. task = task[0]
  30. print(task.id)
  31. params_dict = request.POST.dict()
  32. for key in params_dict.keys():
  33. if key == 'trace_channels' or key == 'state':
  34. params_dict[key] = int(params_dict[key])
  35. elif key != 'remark':
  36. params_dict[key] = float(params_dict[key])
  37. create_time = update_time = datetime.datetime.now()
  38. beidou_case = BeidouCase.objects.filter(task_id=task_id)
  39. if beidou_case:
  40. params_dict['update_time'] = update_time
  41. BeidouCase.objects.filter(task_id=task_id, executor_id=task.executor_id, delete=False).update(**params_dict)
  42. else:
  43. params_dict['task_id'] = task_id
  44. params_dict['title'] = task.title
  45. params_dict['id'] = get_id(BeidouCase, "TestLaboratory_V1_BeidouCase_1")
  46. params_dict['create_time'] = create_time
  47. params_dict['update_time'] = update_time
  48. params_dict['executor_id'] = task.executor_id
  49. params_dict['software_id'] = task.plan.software.id
  50. params_dict['version'] = task.plan.version
  51. BeidouCase.objects.create(**params_dict)
  52. task.state = 2
  53. task.save()
  54. gen_log(action, "北斗测试任务", task.title, method, executor)
  55. return HttpResponse("执行成功")