def submit_inputs(request, db, get_inputs, meta_param_dict, profile): class MockDisplayer(Displayer): def package_defaults(self): return get_inputs project = Project.objects.get(title=request.param) ioutils = get_ioutils(project, Displayer=MockDisplayer, Parser=APIParser) factory = APIRequestFactory() data = { "meta_parameters": { "metaparam": 3 }, "adjustment": { "majorsection1": { "intparam": 2 } }, } mockrequest = factory.post("/modeler/Used-for-testing/api/v1/", data=data, format="json") mockrequest.user = profile.user mockrequest.data = data compute = MockCompute() return SubmitInputs(mockrequest, project, ioutils, compute)
def post(self, request, *args, **kwargs): compute = Compute() project = get_object_or_404( self.queryset, owner__user__username=kwargs["username"], title=kwargs["title"], ) ioutils = get_ioutils(project, Parser=APIParser) try: submit_inputs = SubmitInputs(request, project, ioutils, compute) result = submit_inputs.submit() except BadPostException as bpe: return Response(bpe.errors, status=status.HTTP_400_BAD_REQUEST) except AppError as ae: try: send_mail( f"Compute Studio AppError", (f"An error has occurred:\n {ae.parameters}\n causing: " f"{ae.traceback}\n user:{request.user.username}\n " f"project: {project.app_url}."), "*****@*****.**", ["*****@*****.**"], fail_silently=True, ) # Http 401 exception if mail credentials are not set up. except Exception: pass return Response(ae.traceback, status=status.HTTP_500_INTERNAL_SERVER_ERROR) inputs = InputsSerializer(result) return Response(inputs.data, status=status.HTTP_201_CREATED)
def submit(request, success_status, project, sim): compute = Compute() ioutils = get_ioutils(project, Parser=APIParser) try: submit_inputs = SubmitInputs(request, project, ioutils, compute, sim) result = submit_inputs.submit() except BadPostException as bpe: return Response(bpe.errors, status=status.HTTP_400_BAD_REQUEST) except AppError as ae: try: send_mail( f"Compute Studio AppError", (f"An error has occurred:\n {ae.parameters}\n causing: " f"{ae.traceback}\n user:{request.user.username}\n " f"project: {project.app_url}."), "*****@*****.**", ["*****@*****.**"], fail_silently=True, ) # Http 401 exception if mail credentials are not set up. except Exception: pass return Response(ae.traceback, status=success_status) inputs = InputsSerializer(result) return Response(inputs.data, status=status.HTTP_201_CREATED)
def test_api_parser(db, get_inputs, valid_meta_params): class MockMp(ModelParameters): def get_inputs(self, meta_parameters=None): return get_inputs project = Project.objects.get(title="Used-for-testing") ioutils = get_ioutils(project, ModelParameters=MockMp, Parser=LocalAPIParser) ioutils.model_parameters.init_meta_parameters = valid_meta_params clean_inputs = { "majorsection1": {"intparam": 3, "boolparam": True}, "majorsection2": {"mj2param": 4}, } parser = LocalAPIParser( project, ioutils.model_parameters, clean_inputs, **valid_meta_params ) res = parser.parse_parameters() adjustment = res["adjustment"] errors_warnings = res["errors_warnings"] assert adjustment == { "majorsection1": {"intparam": 3, "boolparam": True}, "majorsection2": {"mj2param": 4}, } exp_errors_warnings = {"errors": {}, "warnings": {}} assert errors_warnings["majorsection1"] == exp_errors_warnings assert errors_warnings["majorsection2"] == exp_errors_warnings assert errors_warnings["API"] == exp_errors_warnings assert errors_warnings["GUI"] == exp_errors_warnings
def test_api_parser(db, get_inputs, valid_meta_params): class MockDisplayer(Displayer): def package_defaults(self): return get_inputs project = Project.objects.get(title="Used-for-testing") ioutils = get_ioutils(project, Displayer=MockDisplayer, Parser=LocalAPIParser) ioutils.displayer.meta_parameters = valid_meta_params clean_inputs = { "majorsection1": {"intparam": 3, "boolparam": True}, "majorsection2": {"mj2param": 4}, } parser = LocalAPIParser( project, ioutils.displayer, clean_inputs, **valid_meta_params ) res = parser.parse_parameters() adjustment = res["adjustment"] errors_warnings = res["errors_warnings"] assert adjustment == { "majorsection1": {"intparam": 3, "boolparam": True}, "majorsection2": {"mj2param": 4}, } exp_errors_warnings = {"errors": {}, "warnings": {}} assert errors_warnings["majorsection1"] == exp_errors_warnings assert errors_warnings["majorsection2"] == exp_errors_warnings assert errors_warnings["API"] == exp_errors_warnings assert errors_warnings["GUI"] == exp_errors_warnings
def test_get_inputs(self, api_client, worker_url): defaults = self.defaults() resp_data = {"status": "SUCCESS", **defaults} with requests_mock.Mocker() as mock: mock.register_uri( "POST", f"{worker_url}{self.owner}/{self.title}/inputs", text=json.dumps(resp_data), ) resp = api_client.get(f"/{self.owner}/{self.title}/api/v1/inputs/") assert resp.status_code == 200 ioutils = get_ioutils(self.project) exp = ioutils.displayer.package_defaults() assert exp == resp.data
def get_inputs(self, kwargs, meta_parameters=None): project = get_object_or_404( self.queryset, owner__user__username=kwargs["username"], title=kwargs["title"], ) ioutils = get_ioutils(project) if meta_parameters is not None: try: parsed_mp = ioutils.displayer.parsed_meta_parameters() ioutils.displayer.meta_parameters = parsed_mp.validate( meta_parameters, throw_errors=True) except ValidationError as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) defaults = ioutils.displayer.package_defaults() if "year" in defaults["meta_parameters"]: defaults.update({"extend": True}) return Response(defaults)
def get_inputs(self, kwargs, meta_parameters=None): project = get_project_or_404( self.queryset, user=self.request.user, owner__user__username__iexact=kwargs["username"], title__iexact=kwargs["title"], ) ioutils = get_ioutils(project) try: defaults = ioutils.model_parameters.defaults(meta_parameters) except NotReady: print("NOT READY") return Response(status=202) except pt.ValidationError as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) if "year" in defaults["meta_parameters"]: defaults.update({"extend": True}) return Response(defaults)
def _submit_inputs( title, get_inputs, meta_param_dict, profile, parent_model_pk=None, notify_on_completion=None, ): class MockDisplayer(Displayer): def package_defaults(self): return get_inputs project = Project.objects.get(title=title) ioutils = get_ioutils(project, Displayer=MockDisplayer, Parser=APIParser) factory = APIRequestFactory() data = { "meta_parameters": { "metaparam": 3 }, "adjustment": { "majorsection1": { "intparam": 2 } }, } if notify_on_completion is not None: data["notify_on_completion"] = notify_on_completion if parent_model_pk is not None: data["parent_model_pk"] = parent_model_pk mockrequest = factory.post("/modeler/Used-for-testing/api/v1/", data=data, format="json") mockrequest.user = profile.user mockrequest.data = data sim = Simulation.objects.new_sim(profile.user, project) compute = MockCompute() return SubmitInputs(mockrequest, project, ioutils, compute, sim)
def test_post_inputs(self, api_client, worker_url): defaults = self.defaults() resp_data = {"status": "SUCCESS", **defaults} meta_params = {"meta_parameters": self.inputs_ok()["meta_parameters"]} with requests_mock.Mocker() as mock: print("mocking", f"{worker_url}{self.owner}/{self.title}/inputs") mock.register_uri( "POST", f"{worker_url}{self.owner}/{self.title}/inputs", text=json.dumps(resp_data), ) resp = api_client.post( f"/{self.owner}/{self.title}/api/v1/inputs/", data=meta_params, format="json", ) assert resp.status_code == 200 ioutils = get_ioutils(self.project) ioutils.displayer.meta_parameters.update(meta_params["meta_parameters"]) exp = ioutils.displayer.package_defaults() assert exp == resp.data
def put(self, request, *args, **kwargs): print("myinputs api method=PUT", kwargs) print("authenticator", request.user, request.successful_authenticator) ser = ModelConfigSerializer(data=request.data) if ser.is_valid(): data = ser.validated_data model_config = get_object_or_404( ModelConfig.objects.prefetch_related("project"), job_id=data["job_id"]) if model_config.status in ("PENDING", "INVALID", "FAIL"): ioutils = get_ioutils(model_config.project) model_config.meta_parameters_values = ioutils.model_parameters.cleanup_meta_parameters( model_config.meta_parameters_values, data["meta_parameters"]) model_config.meta_parameters = data["meta_parameters"] model_config.model_parameters = data["model_parameters"] model_config.status = data["status"] model_config.save() return Response(status=status.HTTP_200_OK) else: print("model config put error", ser.errors) return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)