123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import json
- from django.http import HttpResponse
- from rest_framework.response import Response
- from rest_framework.views import APIView
- from TestLaboratory.settings import SOFTWARE_ROOT, HTTP_HEAD
- from apps.file.models import FileManager
- from apps.log.models import get_log, gen_log
- from apps.software.models import Software
- from django.http.request import QueryDict
- from apps.user.middleware.rolecontrol import RoleControl
- import logging
- logger = logging.getLogger('django')
- class SoftwareView(APIView):
- # 登录权限验证
- authentication_classes = []
- # view software
- @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='软件已删除或不存在')
- software = software[0]
- try:
- versions = []
- for version in software.version_set.filter(delete=False):
- version_fid = version.version_file
- file_path = FileManager.objects.filter(id=version_fid)[0].path
- versions.append({
- 'id': version.id, 'version': version.number,
- 'file': file_path.split('/')[-1],
- 'file_url': HTTP_HEAD+SOFTWARE_ROOT+file_path,
- 'create_time': version.create_time, 'update_time': version.update_time
- })
- task_num = 0
- if software.testplan_set.filter(delete=False):
- for plan in software.testplan_set.filter(delete=False):
- task_num += len(plan.testtask_set.filter(delete=False))
- software_info = {'name': software.name, 'type': software.type, 'create_time': software.create_time,
- 'update_time': software.update_time,
- 'versions': versions,
- 'case_num': len(software.testcase_set.filter(delete=False)),
- 'task_num': task_num}
- except:
- logger.error("测试项目查询失败")
- return HttpResponse(status=500, content='查询失败')
- executor, action, method = get_log(request)
- gen_log(action, "测试目标", software.name, method, executor)
- return Response(software_info)
- # delete software
- @staticmethod
- @RoleControl
- def delete(request, software_id, *args, **kwargs):
- software = Software.objects.filter(id=software_id, delete=False)
- if not software:
- logger.error("软件已删除或不存在")
- return HttpResponse(status=404, content='软件{}已删除或不存在'.format(software_id))
- software = software[0]
- if software.version_set.exists():
- for version in software.version_set.filter():
- version_fid = version.version_file
- file_version = FileManager.objects.filter(id=version_fid)[0]
- file_version.delete = True
- file_version.save()
- # 删除测试计划
- plan_set = software.testplan_set.filter(delete=False)
- if plan_set:
- for plan in plan_set:
- task_set = plan.testtask_set.filter(delete=False)
- if task_set:
- for task in task_set:
- task.delete = True
- task.save()
- plan.delete = True
- plan.save()
- version.delete = True
- version.save()
- software.delete = True
- software.save()
- executor, action, method = get_log(request)
- gen_log(action, "测试目标", software.name, method, executor)
- return HttpResponse(status=200, content='删除完成')
- class SoftwareEdit(APIView):
- # 登录权限验证
- authentication_classes = []
- # edit software
- @staticmethod
- @RoleControl
- def post(request, software_id, *args, **kwargs):
- software = Software.objects.filter(id=software_id, delete=False)
- if not software:
- logger.error("软件已删除或不存在")
- return HttpResponse(status=404, content='软件已删除或不存在')
- software = software[0]
- name = request.POST.get('name')
- software_type = request.POST.get('type')
- if not name:
- logger.error("软件名不能为空")
- return HttpResponse(status=500, content='软件名不能为空')
- if name == software.name and software_type == software.type:
- logger.error("未监测到软件名称及类型变化,编辑不保存")
- return HttpResponse(status=500, content='未监测到软件名称及类型变化,编辑不保存')
- software.name = name
- software.type = software_type
- software.save()
- # print(software)
- executor, action, method = get_log(request)
- gen_log(action, "测试目标", software.name, method, executor)
- return HttpResponse(status=200, content='编辑已保存')
|