123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import datetime
- from rest_framework.response import Response
- from rest_framework.views import APIView
- from django.http import HttpResponse
- from TestLaboratory.settings import PLAN_ROOT, HTTP_HEAD
- from apps.log.models import get_log, gen_log
- from apps.plan.models import TestPlan
- from apps.file.models import FileManager
- from utils.util_add_id import get_id
- from utils.util_file.util_fileio import write_file
- import logging
- logger = logging.getLogger('django')
- class PlanView(APIView):
- # 登录权限验证
- authentication_classes = []
- # view plan
- @staticmethod
- def get(request, plan_id, *args, **kwargs):
- plan = TestPlan.objects.filter(id=plan_id, delete=False)
- if not plan:
- logger.error("测试计划不存在")
- return HttpResponse(status=404, content='测试计划不存在')
- plan = plan[0]
- plan_sids = plan.statement_file.split('&&')
- file_names = []
- file_urls = []
- if plan_sids[0]:
- for statement_id in plan_sids:
- statement = FileManager.objects.get(id=statement_id)
- file_urls.append(PLAN_ROOT + statement.path)
- file_names.append(statement.path.split('/')[-1])
- tasks_info = []
- tasks = plan.testtask_set.filter(delete=False)
- if tasks:
- for task in tasks:
- tasks_info.append({
- 'id': task.id,
- 'title': task.title,
- 'description': task.description,
- 'state': task.state,
- 'executor': {'id': task.executor.id, 'name': task.executor.username},
- 'create_time': task.create_time,
- 'update_time': task.update_time
- })
- executor, action, method = get_log(request)
- gen_log(action, "测试计划", plan.title, method, executor)
- return Response({
- '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,
- 'title': plan.title,
- 'description': plan.description,
- 'state': plan.state,
- 'tasks': tasks_info,
- 'create_time': plan.create_time,
- 'update_time': plan.update_time,
- 'statements': [{'file_id': file_sid, 'name': file_name, 'url': HTTP_HEAD + file_url} for file_sid, file_name
- , file_url in zip(plan_sids, file_names, file_urls)]
- })
- # delete plan_list
- @staticmethod
- def delete(request, plan_id, *args, **kwargs):
- plan = TestPlan.objects.filter(id=plan_id, delete=False)
- if not plan:
- logger.error("测试计划已删除或不存在")
- return HttpResponse(status=404, content='测试计划已删除或不存在')
- plan = plan[0]
- plan_sids = plan.statement_file.split('&&')
- if plan_sids[0]:
- for statement_id in plan_sids:
- statement = FileManager.objects.get(id=statement_id)
- statement.delete = True
- statement.save()
- plan.delete = True
- plan.save()
- executor, action, method = get_log(request)
- gen_log(action, "测试计划", plan.title, method, executor)
- return HttpResponse("删除完成")
- class PlanEdit(APIView):
- # 登录权限验证
- authentication_classes = []
- # edit plan_list
- @staticmethod
- def post(request, plan_id, *args, **kwargs):
- title = request.POST.get('title')
- state = request.POST.get('state')
- description = request.POST.get('description')
- files = request.FILES.getlist('files')
- plan = TestPlan.objects.filter(id=plan_id, delete=False)
- if not plan:
- logger.error("测试计划已删除或不存在")
- return HttpResponse(status=404, content='测试计划已删除或不存在')
- plan = plan[0]
- plan.title = title
- plan.state = state
- plan.description = description
- fids = ''
- for file in files:
- create_time = update_time = datetime.datetime.now()
- try:
- write_file(file,
- PLAN_ROOT + "/" + plan_id + "/" + str(create_time))
- except:
- logger.error("文件上传失败")
- return HttpResponse(status=500, content="文件上传失败")
- id_file = get_id(FileManager, "TestLaboratory_V1_File_1")
- try:
- file_new = FileManager.objects.create(id=id_file,
- category="plan_statement",
- path=plan_id + "/" + str(
- create_time) + "/" + file.name,
- create_time=create_time,
- update_time=update_time)
- except:
- logger.error("文件路径写入数据库失败")
- return HttpResponse(status=500, content="文件路径写入数据库失败")
- fids += id_file + '&&'
- fids = fids[:-2]
- plan.statement_file += fids
- plan.save()
- executor, action, method = get_log(request)
- gen_log(action, "测试计划", plan.title, method, executor)
- return HttpResponse("编辑已保存")
|