Ejemplo n.º 1
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.º 2
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