Пример #1
0
    def inspection(self, request, pk=None):
        """Get the inspection results of a scan job."""
        ordering_filter, status_filter, source_id_filter = \
            self.handle_result_filters(request)

        try:
            scan_job = get_object_or_404(self.queryset, pk=pk)
            all_tasks = scan_job.inspection_results.task_results.all()
        except ValueError:
            return Response(status=400)
        system_result_queryset = None
        for task_result in all_tasks:
            if system_result_queryset is None:
                system_result_queryset = task_result.systems.all()
            else:
                system_result_queryset = \
                    system_result_queryset | task_result.systems.all()
        # create ordered queryset and assign the paginator
        paginator = StandardResultsSetPagination()
        ordered_query_set = system_result_queryset.order_by(ordering_filter)
        if status_filter:
            ordered_query_set = ordered_query_set.filter(status=status_filter)
        if source_id_filter:
            ordered_query_set = \
                ordered_query_set.filter(source__id=source_id_filter)

        page = paginator.paginate_queryset(ordered_query_set, request)

        if page is not None:
            serializer = SystemInspectionResultSerializer(page, many=True)
            for system in serializer.data:
                expand_system_inspection(system)
            return paginator.get_paginated_response(serializer.data)
        return Response(status=404)
Пример #2
0
def jobs(request, pk=None):
    """Get the jobs of a scan."""
    # pylint: disable=invalid-name
    if pk is not None:
        if not is_int(pk):
            return Response(status=status.HTTP_404_NOT_FOUND)
    result = []
    scan = get_object_or_404(Scan.objects.all(), pk=pk)
    if request.method == 'GET':
        job_queryset = get_job_queryset_query_set(scan,
                                                  request.query_params)
        paginator = StandardResultsSetPagination()
        page = paginator.paginate_queryset(job_queryset, request)

        if page is not None:
            serializer = ScanJobSerializer(page, many=True)
            for scan in serializer.data:
                json_scan = expand_scanjob(scan)
                result.append(json_scan)
            return paginator.get_paginated_response(serializer.data)

        for job in job_queryset:
            job_serializer = ScanJobSerializer(job)
            job_json = job_serializer.data
            job_json = expand_scanjob(job_serializer.data)
            result.append(job_json)
        return Response(result)
    else:
        job_data = {}
        job_data['scan'] = pk
        job_serializer = ScanJobSerializer(data=job_data)
        job_serializer.is_valid(raise_exception=True)
        job_serializer.save()
        scanjob_obj = ScanJob.objects.get(pk=job_serializer.data['id'])
        scanjob_obj.log_current_status()
        start_scan.send(sender=ScanViewSet.__class__, instance=scanjob_obj)

        return Response(job_serializer.data,
                        status=status.HTTP_201_CREATED)