Esempio n. 1
0
    def create(self, request, *args, **kwargs):
        """
        Overrides the ``create`` method of ``mixins.CreateModelMixin`` in order
        to add the new job to the Redis queue.

        Side effects
            Via ``queue_eregs_job`` and ``PipelineJobSerializer.save``, alters
            the redis queue and the DB.

        :arg HttpRequest request: the incoming request.

        :rtype: Response
        :returns: JSON or HTML of the information about the job (status 201),
            or about why the job couldn't be added (status 400).
        """
        serialized = self.get_serializer(data=request.data)
        serialized.is_valid(raise_exception=True)

        eregs_args = self.build_eregs_args(serialized.validated_data)
        job = queue_eregs_job(eregs_args, timeout=60 * 30, result_ttl=-1)

        # Paranoia--validate the values we provide:
        job_id = job.id
        for validator in serialized.get_fields()["job_id"].validators:
            validator(job_id)
        statusurl = status_url(job_id, sub_path=self.sub_path)
        for validator in serialized.get_fields()["url"].validators:
            validator(statusurl)

        if serialized.validated_data.get("notification_email"):
            queue_notification_email(
                job, statusurl,
                serialized.validated_data["notification_email"])
        serialized.save(job_id=job_id, url=statusurl,
                        destination=eregs_site_api_url)
        headers = self.get_success_headers(serialized.data)
        """
        Adding the Refresh header here so that the browser does the
        user-friendly thing of redirecting the user to the page for the
        newly-created object, even though use of the Refresh header is
        frowned upon in some circles.

        Not using redirect via 302 or 303 so that non-browser users get the
        201 status code they expect upon a successful POST.

        I'm open to debate on this decision.
        """
        headers["Refresh"] = "0;url=%s" % statusurl
        return Response(serialized.data, status=status.HTTP_201_CREATED,
                        headers=headers)