import datetime import os from django.db.models import Q from django.http import HttpResponse, HttpResponseBadRequest from rest_framework.response import Response from rest_framework.views import APIView from TestLaboratory.settings import CASE_FILE_ROOT, CASE_FILE_EXPORT, HTTP_HEAD from apps.file.models import FileManager from apps.log.models import get_log, gen_log from apps.task.models import TestTask, TestCase, BeidouCase from apps.user.middleware.rolecontrol import RoleControl from util_xlsx import load_xlsx, write_to_xlsx from utils.util_add_id import get_id from utils.util_file.util_fileio import write_file import logging logger = logging.getLogger('django') class BeidouCaseExecuteView(APIView): # 登录权限验证 authentication_classes = [] # execute task @staticmethod @RoleControl def post(request, task_id, *args, **kwargs): executor, action, method = get_log(request) task = TestTask.objects.filter(id=task_id, delete=False) if not task: logger.error("测试任务已删除或不存在") return HttpResponseBadRequest(status=404, content='测试任务已删除或不存在') task = task[0] print(task.id) params_dict = request.POST.dict() for key in params_dict.keys(): if key == 'trace_channels' or key == 'state': params_dict[key] = int(params_dict[key]) elif key != 'remark': params_dict[key] = float(params_dict[key]) create_time = update_time = datetime.datetime.now() beidou_case = BeidouCase.objects.filter(task_id=task_id) if beidou_case: params_dict['update_time'] = update_time BeidouCase.objects.filter(task_id=task_id, executor_id=task.executor_id, delete=False).update(**params_dict) else: params_dict['task_id'] = task_id params_dict['title'] = task.title params_dict['id'] = get_id(BeidouCase, "TestLaboratory_V1_BeidouCase_1") params_dict['create_time'] = create_time params_dict['update_time'] = update_time params_dict['executor_id'] = task.executor_id params_dict['software_id'] = task.plan.software.id params_dict['version'] = task.plan.version BeidouCase.objects.create(**params_dict) task.state = 2 task.save() gen_log(action, "北斗测试任务", task.title, method, executor) return HttpResponse("执行成功")