Ejemplo n.º 1
0
    def put(self, request, *args, **kwargs):
        print("myinputs api method=PUT", kwargs)

        if request.user.username == "comp-api-user":
            data = request.data
            ser = InputsSerializer(data=request.data)
            if ser.is_valid():
                data = ser.validated_data
                inputs = get_object_or_404(Inputs, job_id=data["job_id"])
                print("data")
                print(data)
                if inputs.status == "PENDING":
                    # successful run
                    if data["status"] == "SUCCESS":
                        inputs.errors_warnings = data["errors_warnings"]
                        inputs.custom_adjustment = data.get(
                            "custom_adjustment", None)
                        inputs.status = "SUCCESS" if is_valid(
                            inputs) else "INVALID"
                        inputs.save()
                        if inputs.status == "SUCCESS":
                            submit_sim = SubmitSim(inputs, compute=Compute())
                            submit_sim.submit()
                    # failed run, exception was caught
                    else:
                        inputs.status = "FAIL"
                        inputs.traceback = data["traceback"]
                        inputs.save()
                return Response(status=status.HTTP_200_OK)
            else:
                print("inputs put error", ser.errors)
                return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 2
0
class SubmitInputs:

    webapp_version = WEBAPP_VERSION

    def __init__(
        self,
        request: HttpRequest,
        project: Project,
        ioutils: IOClasses,
        compute: Compute,
    ):
        self.request = request
        self.user = self.request.user
        self.project = project
        self.ioutils = ioutils
        self.compute = compute
        self.badpost = None
        self.meta_parameters = ioutils.displayer.parsed_meta_parameters()

    def submit(self):
        print(self.request.data)
        self.ser = InputsSerializer(data=self.request.data)
        is_valid = self.ser.is_valid()
        if not is_valid:
            raise BadPostException(self.ser.errors)

        validated_data = self.ser.validated_data
        meta_parameters = validated_data.get("meta_parameters", {})
        adjustment = validated_data.get("adjustment", {})
        try:
            self.valid_meta_params = self.meta_parameters.validate(
                meta_parameters)
            errors = None
        except ValidationError as ve:
            errors = str(ve)

        if errors:
            raise BadPostException(errors)

        parser = self.ioutils.Parser(
            self.project,
            self.ioutils.displayer,
            adjustment,
            compute=self.compute,
            **self.valid_meta_params,
        )

        result = parser.parse_parameters()
        self.inputs = self.ser.save(
            meta_parameters=self.valid_meta_params,
            adjustment=result["adjustment"],
            errors_warnings=result["errors_warnings"],
            custom_adjustment=result["custom_adjustment"],
            project=self.project,
            owner=getattr(self.request.user, "profile", None),
            job_id=result["job_id"],
            status="PENDING",
        )
        return self.inputs
Ejemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     print("inputs api method=POST", request.POST, kwargs)
     ser = InputsSerializer(data=request.data)
     if ser.is_valid():
         data = ser.validated_data
         if "meta_parameters" in data:
             meta_parameters = data["meta_parameters"]
         else:
             meta_parameters = {}
         return self.get_inputs(kwargs, meta_parameters)
     else:
         return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
    def put(self, request, *args, **kwargs):
        print("myinputs api method=PUT", kwargs)
        print("authenticator", request.user, request.successful_authenticator)
        ser = InputsSerializer(data=request.data)
        if ser.is_valid():
            data = ser.validated_data
            inputs = get_object_or_404(
                Inputs.objects.prefetch_related("project"),
                job_id=data["job_id"])
            if not inputs.project.has_write_access(request.user):
                return Response(status=status.HTTP_401_UNAUTHORIZED)
            if inputs.status in ("PENDING", "INVALID", "FAIL"):
                # successful run
                if data["status"] == "SUCCESS":
                    inputs.errors_warnings = data["errors_warnings"]
                    inputs.custom_adjustment = data.get(
                        "custom_adjustment", None)
                    inputs.status = "SUCCESS" if is_valid(
                        inputs) else "INVALID"
                    inputs.save()
                    if inputs.status == "SUCCESS":
                        submit_sim = SubmitSim(inputs.sim, compute=Compute())
                        submit_sim.submit()
                # failed run, exception was caught
                else:
                    inputs.status = "FAIL"
                    inputs.traceback = data["traceback"]
                    inputs.save()

                    if self.request.is_secure():
                        protocol = "https"
                    else:
                        protocol = "http"  # local dev.
                    base_url = f"{protocol}://{self.request.get_host()}"
                    url = base_url + inputs.get_absolute_url()
                    fail(
                        project=inputs.project,
                        model_pk=inputs.sim.model_pk,
                        traceback=inputs.traceback,
                        url=url,
                    )
            return Response(status=status.HTTP_200_OK)
        else:
            print("inputs put error", ser.errors)
            return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
class SubmitInputs:

    webapp_version = WEBAPP_VERSION

    def __init__(
        self,
        request: HttpRequest,
        project: Project,
        ioutils: IOClasses,
        compute: Compute,
        sim: Simulation,
    ):
        self.request = request
        self.user = self.request.user
        self.project = project
        self.ioutils = ioutils
        self.compute = compute
        self.badpost = None
        self.meta_parameters = ioutils.displayer.parsed_meta_parameters()
        self.sim = sim

    def submit(self):
        self.ser = InputsSerializer(instance=self.sim.inputs,
                                    data=self.request.data)
        is_valid = self.ser.is_valid()
        if not is_valid:
            raise BadPostException(self.ser.errors)

        validated_data = self.ser.validated_data
        meta_parameters = validated_data.get("meta_parameters", {})
        adjustment = validated_data.get("adjustment", {})
        parent_model_pk = validated_data.pop("parent_model_pk", None)
        notify_on_completion = validated_data.pop("notify_on_completion", None)

        if parent_model_pk is not None and self.sim.parent_sim is None:
            parent_sim = get_object_or_404(Simulation,
                                           project=self.project,
                                           model_pk=parent_model_pk)
        else:
            parent_sim = None

        try:
            self.valid_meta_params = self.meta_parameters.validate(
                meta_parameters)
            errors = None
        except ValidationError as ve:
            errors = str(ve)

        if errors:
            raise BadPostException(errors)

        parser = self.ioutils.Parser(
            self.project,
            self.ioutils.displayer,
            adjustment,
            compute=self.compute,
            **self.valid_meta_params,
        )

        result = parser.parse_parameters()
        self.inputs = self.ser.save(
            meta_parameters=self.valid_meta_params,
            adjustment=result["adjustment"],
            errors_warnings=result["errors_warnings"],
            custom_adjustment=result["custom_adjustment"],
            job_id=result["job_id"],
            status="PENDING",
            parent_sim=self.sim.parent_sim or parent_sim,
        )
        # case where parent sim exists and has not yet been assigned
        if not self.sim.parent_sim and parent_sim:
            self.sim.parent_sim = parent_sim
            self.sim.title = parent_sim.title
            self.sim.readme = parent_sim.readme
            if notify_on_completion is not None:
                self.sim.notify_on_completion = notify_on_completion
            self.sim.save()
        elif notify_on_completion is not None:
            self.sim.notify_on_completion = notify_on_completion
            self.sim.save()

        return self.inputs