import datetime import os from django.http import HttpResponse from rest_framework.views import APIView from TestLaboratory.settings import PLAN_ROOT, TASK_ROOT from apps.file.models import FileManager from apps.log.models import get_log, gen_log from apps.plan.models import TestPlan from apps.task.models import TestTask from apps.user.middleware.rolecontrol import RoleControl from utils.util_add_id import gen_next_id import logging logger = logging.getLogger('django') class FileView(APIView): # create file @staticmethod @RoleControl def post(request, *args, **kwargs): file_type = request.POST.get('type') plan_id = request.POST.get('plan_id') task_id = request.POST.get('task_id') files = request.FILES.getlist('files') model_object = None path_head = None if file_type == 'plan_statement': model_object = TestPlan.objects.filter(id=plan_id, delete=False) if not model_object: logger.error("测试计划已删除或不存在") return HttpResponse(status=404, content='测试计划已删除或不存在') model_object = model_object[0] path_head = PLAN_ROOT elif file_type == 'task_statement': model_object = TestTask.objects.filter(id=task_id, delete=False) if not model_object: logger.error("测试任务已删除或不存在") return HttpResponse(status=404, content='测试任务已删除或不存在') model_object = model_object[0] path_head = TASK_ROOT # step7:创建文件的数据库字段 fids = '' file_paths = [] for file in files: try: create_time = update_time = datetime.datetime.now() file_dir = path_head + "/" + model_object.id + "/" + str(create_time) if not os.path.exists(file_dir): os.makedirs(file_dir) file_path = file_dir + "/" + file.name with open(file_path, 'wb') as f: f.write(file.read()) f.close() except: logger.error("文件上传失败") return HttpResponse(status=500, content="文件上传失败") if not FileManager.objects.last(): id_file = "TestLaboratory_V1_File_1" else: id_file = gen_next_id(FileManager.objects.order_by('create_time').last().id) try: file_new = FileManager.objects.create(id=id_file, category=file_type, path=model_object.id+ "/" + str(create_time) + "/" + file.name, create_time=create_time, update_time=update_time) except: logger.error("文件路径写入数据库失败") return HttpResponse(status=500, content="文件路径写入数据库失败") file_paths.append(PLAN_ROOT + file_new.path) fids += id_file + "&&" fids = fids[:-2] model_object.statement_file += ("&&" + fids) model_object.save() executor, action, method = get_log(request) gen_log(action, "说明文档", model_object.title, method, executor) return HttpResponse(status=200, content='新建文件成功') # create file @staticmethod @RoleControl def delete(request, *args, **kwargs): file_id = request.GET.get('file_id') file_type = request.GET.get('type') plan_id = request.GET.get('plan_id') task_id = request.GET.get('task_id') model_object = None if file_type == 'plan_statement': model_object = TestPlan.objects.filter(id=plan_id, delete=False) if not model_object: logger.error("测试计划已删除或不存在") return HttpResponse(status=404, content='测试计划已删除或不存在') model_object = model_object[0] elif file_type == 'task_statement': model_object = TestTask.objects.filter(id=task_id, delete=False) if not model_object: logger.error("测试任务已删除或不存在") return HttpResponse(status=404, content='测试任务已删除或不存在') model_object = model_object[0] file = FileManager.objects.filter(id=file_id, delete=False) if not file: logger.error("文件已删除不存在") return HttpResponse(status=404, content='文件已删除不存在') file = file[0] file_list = model_object.statement_file.split('&&') if file.id not in file_list: logger.error("该文件不存在于测试计划或测试任务中,数据库错误") return HttpResponse(status=404, content='该文件不存在于测试计划或测试任务中,数据库错误') file_list.remove(file.id) file_str = '' for file_name in file_list: file_str += file_name + "&&" file_str = file_str[:-2] model_object.statement_file = file_str model_object.save() executor, action, method = get_log(request) gen_log(action, "说明文档", file.path.split('/')[-1], method, executor) return HttpResponse(status=200, content='删除成功')