versionlistview.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.http import HttpResponse
  2. from rest_framework.response import Response
  3. from rest_framework.views import APIView
  4. from apps.software.models import Software, Version
  5. from apps.task.models import TestCase
  6. from apps.user.middleware.rolecontrol import RoleControl
  7. import logging
  8. logger = logging.getLogger('django')
  9. class VersionListView(APIView):
  10. # create version_list
  11. @staticmethod
  12. @RoleControl
  13. def get(request, *args, **kwargs):
  14. software_id = request.GET.get('software_id')
  15. sort = request.GET.get('sort')
  16. version_all = Version.objects.filter(delete=False).order_by('-create_time')
  17. if software_id:
  18. software = Software.objects.filter(id=software_id, delete=False)
  19. if not software:
  20. logger.error("软件不存在")
  21. return HttpResponse(status=404, content='软件不存在')
  22. software = software[0]
  23. version_all = version_all.filter(software=software)
  24. version_infos = []
  25. if not version_all:
  26. return Response(version_infos)
  27. if sort:
  28. version_all = version_all.order_by(sort)
  29. for version in version_all:
  30. software = Software.objects.filter(id=version.software_id, delete=False)[0]
  31. fail_num = len(TestCase.objects.filter(software=software, version=version.number, state=2, delete=False))
  32. version_infos.append({
  33. 'software_id': software.id,
  34. 'software_name': software.name,
  35. 'version_number': version.number,
  36. 'create_time': version.create_time,
  37. 'fail_num': fail_num
  38. })
  39. return Response(version_infos)