123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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.company.models import Company
- 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')
- creator_id = request.POST.get('creator_id')
- company_id = request.POST.get('company_id')
- file = request.FILES.get('file')
- if company_id == '':
- company_id = -1
- 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, company_id=company_id,
- creator_id=creator_id,
- 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,
- 'company_id': software.company_id,
- })
- # 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')
- creator_id = request.GET.get('creator_id')
- company_id = request.GET.get('company_id')
- 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)
- if creator_id:
- software_all = software_all.filter(creator_id=creator_id)
- if company_id:
- company_id = int(company_id)
- software_all = software_all.filter(company_id=company_id)
- """
- 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():
- company_name = ''
- try:
- company = Company.objects.get(id=software.company_id)
- company_name = company.name
- except:
- pass
- info.append({
- 'id': software.id,
- 'name': software.name,
- 'type': software.type,
- 'create_time': software.create_time,
- 'update_time': software.update_time,
- 'company_id': software.company_id,
- 'company_name': company_name,
- })
- return Response(info)
|