Exemple #1
0
    def post(self, request, pk):
        """
        Run a Script identified as "<module>.<script>" and return the pending JobResult as the result
        """
        script = self._get_script(pk)()
        input_serializer = serializers.ScriptInputSerializer(data=request.data)

        # Check that at least one RQ worker is running
        if not Worker.count(get_connection('default')):
            raise RQWorkerNotRunningException()

        if input_serializer.is_valid():
            data = input_serializer.data['data']
            commit = input_serializer.data['commit']

            script_content_type = ContentType.objects.get(app_label='extras',
                                                          model='script')
            job_result = JobResult.enqueue_job(
                run_script,
                script.full_name,
                script_content_type,
                request.user,
                data=data,
                request=copy_safe_request(request),
                commit=commit)
            script.result = job_result
            serializer = serializers.ScriptDetailSerializer(
                script, context={'request': request})

            return Response(serializer.data)

        return Response(input_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
    def run(self, request, pk):
        """
        Run a Report identified as "<module>.<script>" and return the pending JobResult as the result
        """
        # Check that the user has permission to run reports.
        if not request.user.has_perm('extras.run_report'):
            raise PermissionDenied(
                "This user does not have permission to run reports.")

        # Check that at least one RQ worker is running
        if not Worker.count(get_connection('default')):
            raise RQWorkerNotRunningException()

        # Retrieve and run the Report. This will create a new JobResult.
        report = self._retrieve_report(pk)
        report_content_type = ContentType.objects.get(app_label='extras',
                                                      model='report')
        job_result = JobResult.enqueue_job(run_report, report.full_name,
                                           report_content_type, request.user)
        report.result = job_result

        serializer = serializers.ReportDetailSerializer(
            report, context={'request': request})

        return Response(serializer.data)