def destroy(self, request, pk): """Delete a scan, its jobs, and the results.""" # pylint: disable=too-many-nested-blocks try: scan = Scan.objects.get(pk=pk) logger.info('Deleting scan jobs associated with scan %s', pk) if scan.jobs is not None: jobs_to_cancel = scan.jobs.exclude( Q(status=ScanTask.FAILED) | Q(status=ScanTask.CANCELED) | Q(status=ScanTask.COMPLETED)) if jobs_to_cancel: for job in jobs_to_cancel: job.cancel() cancel_scan.send(sender=self.__class__, instance=job) for job in scan.jobs.all(): logger.info('Deleting job %s and its results', job.id) if job.connection_results is not None: logger.info( 'Deleting connection results associated ' 'with job %s', job.id) for task_connection_result in \ job.connection_results.task_results.all(): task_connection_result.systems.all().delete() task_connection_result.delete() if job.inspection_results is not None: logger.info( 'Deleting inspection results associated ' 'with job %s', job.id) for task_inspection_result in \ job.inspection_results.task_results.all(): logger.info( 'Deleting inspection results associated ' 'with job %s', job.id) for system in task_inspection_result.systems.all(): system.facts.all().delete() system.delete() task_inspection_result.delete() if job.tasks is not None: logger.info( 'Deleting scan tasks associated ' 'with job %s', job.id) job.tasks.all().delete() job.delete() logger.info('Deleting scan %s', pk) scan.delete() return Response(status=status.HTTP_204_NO_CONTENT) except Scan.DoesNotExist: raise Http404
def cancel(self, request, pk=None): """Cancel the running scan.""" scan = get_object_or_404(self.queryset, pk=pk) if (scan.status == ScanTask.COMPLETED or scan.status == ScanTask.FAILED or scan.status == ScanTask.CANCELED): err_msg = _(messages.NO_CANCEL) return JsonResponse({'non_field_errors': [err_msg]}, status=400) scan.cancel() cancel_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) return Response(json_scan, status=200)
def cancel(self, request, pk=None): """Cancel the running scan.""" if not pk or (pk and not is_int(pk)): error = {'id': [_(messages.COMMON_ID_INV)]} raise ValidationError(error) scan = get_object_or_404(self.queryset, pk=pk) if (scan.status == ScanTask.COMPLETED or scan.status == ScanTask.FAILED or scan.status == ScanTask.CANCELED): err_msg = _(messages.NO_CANCEL) return JsonResponse({'non_field_errors': [err_msg]}, status=400) scan.cancel() cancel_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(json_scan) return Response(json_scan, status=200)