from django.http import HttpResponse from rest_framework.response import Response from rest_framework.views import APIView from apps.software.models import Software, Version from apps.task.models import TestCase from apps.user.middleware.rolecontrol import RoleControl import logging logger = logging.getLogger('django') class VersionListView(APIView): # create version_list @staticmethod @RoleControl def get(request, *args, **kwargs): software_id = request.GET.get('software_id') sort = request.GET.get('sort') version_all = Version.objects.filter(delete=False).order_by('-create_time') if software_id: software = Software.objects.filter(id=software_id, delete=False) if not software: logger.error("软件不存在") return HttpResponse(status=404, content='软件不存在') software = software[0] version_all = version_all.filter(software=software) version_infos = [] if not version_all: return Response(version_infos) if sort: version_all = version_all.order_by(sort) for version in version_all: software = Software.objects.filter(id=version.software_id, delete=False)[0] fail_num = len(TestCase.objects.filter(software=software, version=version.number, state=2, delete=False)) version_infos.append({ 'software_id': software.id, 'software_name': software.name, 'version_number': version.number, 'create_time': version.create_time, 'fail_num': fail_num }) return Response(version_infos)