import datetime import json import logging from django.http import HttpResponse from rest_framework.response import Response from rest_framework.views import APIView from apps.log.models import get_log, gen_log from apps.plan.models import TestPlan from apps.software.models import Software from apps.task.models import TestTask from apps.user.middleware.rolecontrol import RoleControl from django.forms.models import model_to_dict from apps.user.models import User from utils.util_add_id import get_id logger = logging.getLogger('django') class SoftwarePlanView(APIView): authentication_classes = [] # generate report @staticmethod @RoleControl def get(request, software_id, *args, **kwargs): software = Software.objects.filter(id=software_id, delete=False) if not software: logger.error("软件已删除或不存在") return HttpResponse(status=404, content='软件已删除或不存在') plan_all = TestPlan.objects.order_by('-create_time') plan_all = plan_all.filter(software__id__contains=software_id) info = [] plan_all = plan_all.filter(delete=False) names = set() for plan in plan_all.all(): if plan.title not in names: names.add(plan.title) info.append({ 'software_id': plan.software.id, 'software_name': plan.software.name, 'version': plan.version, 'creator_id': plan.creator.id, 'creator': plan.creator.username, 'id': plan.id, 'type': plan.type, 'title': plan.title, 'description': plan.description, 'state': plan.state, 'create_time': plan.create_time, 'update_time': plan.update_time, }) return Response(info) class SoftwareVersionPlanView(APIView): authentication_classes = [] # generate report @staticmethod @RoleControl def post(request, software_id, *args, **kwargs): params = json.loads(request.body) version_number = params['version_number'] plan_ids = params['plan_ids'] software = Software.objects.filter(id=software_id, delete=False) if not software: logger.error("软件已删除或不存在") return HttpResponse(status=404, content='软件已删除或不存在') software = software[0] plan_all = TestPlan.objects.order_by('-create_time').filter(delete=False) plan_all = plan_all.filter(id__in=plan_ids) for plan in plan_all: create_time = update_time = datetime.datetime.now() plan_info = model_to_dict(plan) plan_info['id'] = get_id(TestPlan, "TestLaboratory_V1_Plan_1") plan_info['version'] = version_number plan_info['state'] = 0 plan_info['create_time'] = create_time plan_info['update_time'] = update_time plan_info['software'] = software plan_info['creator'] = plan.creator new_plan = TestPlan.objects.create(**plan_info) tasks = plan.testtask_set.filter(delete=False) if tasks: for task in tasks: task_info = model_to_dict(task) task_info['id'] = get_id(TestTask, "TestLaboratory_V1_Task_1") task_info['state'] = 0 task_info['create_time'] = create_time task_info['update_time'] = update_time task_info['plan'] = new_plan task_info['executor'] = task.executor new_task = TestTask.objects.create(**task_info) executor, action, method = get_log(request) gen_log(action, "版本测试计划", plan.title, method, executor) return HttpResponse("关联成功")