import datetime import os from rest_framework.response import Response from rest_framework.views import APIView from django.http import HttpResponse, JsonResponse, Http404 from TestLaboratory.settings import SOFTWARE_ROOT from apps.file.models import FileManager from apps.log.models import gen_log, get_log from apps.software.models import Software, create_software, get_software, Version import json from apps.user.middleware.rolecontrol import RoleControl from utils.util_add_id import gen_next_id import logging logger = logging.getLogger('django') class SoftwareListView(APIView): # create software @staticmethod @RoleControl def post(request, *args, **kwargs): name = request.POST.get('name') software_type = request.POST.get('type') version_num = request.POST.get('version') file = request.FILES.get('file') if not file: logger.error("软件上传失败") return HttpResponse(status=404, content='软件上传失败') # create_msg = create_software(name, software_type, version, file) id_ = None if not Software.objects.order_by('create_time').last(): id_ = "TestLaboratory_V1_Software_1" else: id_ = gen_next_id(Software.objects.order_by('create_time').last().id) create_time = update_time = datetime.datetime.now() # step4:创建版本id id_version = None if not Version.objects.last(): id_version = "TestLaboratory_V1_Version_1" else: id_version = gen_next_id(Version.objects.order_by('create_time').last().id) # step6:上传文件: file_dir = SOFTWARE_ROOT + id_ + "/" + id_version + "/" + 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() # step7:创建文件对象 id_file = None 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="software", path=id_ + "/" + id_version + "/" + str(create_time) + "/" + file.name, create_time=create_time, update_time=update_time) # step3:数据库新增软件字段 software = Software.objects.create(name=name, id=id_, type=software_type, create_time=create_time, update_time=update_time) # step5:创建版本 version = Version.objects.create(id=id_version, number=version_num, software=software, version_file=id_file, create_time=create_time, update_time=update_time) except: logger.error("测试项目创建失败") return HttpResponse(content='创建失败', status=500) executor, action, method = get_log(request) gen_log(action, "测试目标", software.name, method, executor) return Response({ 'id': software.id, 'name': software.name, 'type': software.type, 'create_time': software.create_time, 'update_time': software.update_time, }) # software list @staticmethod @RoleControl def get(request, *args, **kwargs): name = request.GET.get('name') software_type = request.GET.get('type') sort = request.GET.get('sort') software_all = Software.objects.order_by('-create_time') if name: software_all = software_all.filter(name__contains=name) if software_type: software_all = software_all.filter(type__contains=software_type) """ sort=update_time, -update_time, create_time, -create_time, name, -name, type, -type """ if sort: software_all = software_all.order_by(sort) software_all = software_all.filter(delete=False) if not software_all: return Response([]) info = [] for software in software_all.all(): info.append({ 'id': software.id, 'name': software.name, 'type': software.type, 'create_time': software.create_time, 'update_time': software.update_time, }) return Response(info)