diff --git a/metadata_backend/api/handlers/object.py b/metadata_backend/api/handlers/object.py index b20cfa741..b9d9640f5 100644 --- a/metadata_backend/api/handlers/object.py +++ b/metadata_backend/api/handlers/object.py @@ -183,11 +183,8 @@ async def post_object(self, req: Request) -> Response: await folder_op.update_folder(folder_id, patch) # Create draft dataset to Metax catalog - if collection in {"study", "dataset"}: - [await self._create_metax_dataset(req, collection, item) for item in objects] - - patch = self._prepare_folder_patch_new_object(collection, ids, patch_params) - await folder_op.update_folder(folder_id, patch, schema_type) + if collection in _allowed_doi: + [await self._create_metax_dataset(req, collection, item, folder_id) for item in objects] body = ujson.dumps(data, escape_forward_slashes=False) @@ -270,6 +267,8 @@ async def put_object(self, req: Request) -> Response: :raises: HTTPUnsupportedMediaType if JSON replace is attempted :returns: JSON response containing accessionId for submitted object """ + _allowed_doi = {"study", "dataset"} + schema_type = req.match_info["schema"] accession_id = req.match_info["accessionId"] self._check_schema_exists(schema_type) @@ -308,7 +307,7 @@ async def put_object(self, req: Request) -> Response: await folder_op.update_folder(folder_id, patch) # Update draft dataset to Metax catalog - if collection in {"study", "dataset"}: + if collection in _allowed_doi: await self._update_metax_dataset(req, collection, accession_id) body = ujson.dumps({"accessionId": accession_id}, escape_forward_slashes=False) @@ -450,7 +449,7 @@ def _prepare_folder_patch_update_object(self, schema: str, data: Dict, filename: return [patch_op] # TODO: update doi related code - async def _create_metax_dataset(self, req: Request, collection: str, object: Dict) -> str: + async def _create_metax_dataset(self, req: Request, collection: str, object: Dict, folder_id: str) -> str: """Handle connection to Metax api handler. Sends Dataset or Study object's data to Metax api handler. @@ -468,10 +467,14 @@ async def _create_metax_dataset(self, req: Request, collection: str, object: Dic # MYPY related if statement, Operator (when not XMLOperator) always returns object_data as dict if isinstance(object, Dict): LOG.info("Creating draft dataset to Metax.") - object["doi"] = await self.create_doi() + object["doi"] = await self._draft_doi(collection) metax_id = await metax_service.post_dataset_as_draft(collection, object) new_info = {"doi": object["doi"], "metaxIdentifier": {"identifier": metax_id, "status": "draft"}} await operator.update_metadata_object(collection, object["accessionId"], new_info) + if folder_id: + folder_op = FolderOperator(req.app["db_client"]) + doi_patch = self._prepare_folder_patch_doi(collection, object["doi"], metax_id) + await folder_op.update_folder(folder_id, doi_patch) else: raise ValueError("Object's data must be dictionary") return metax_id diff --git a/metadata_backend/api/operators.py b/metadata_backend/api/operators.py index 754d46df9..c3c15d6f5 100644 --- a/metadata_backend/api/operators.py +++ b/metadata_backend/api/operators.py @@ -455,7 +455,7 @@ async def _format_data_to_update_and_add_to_db(self, schema_type: str, accession read_data = await self.db_service.read(schema_type, accession_id) # on firs write db doesnt have yet metaxIdentifier if read_data.get("metaxIdentifier", None): - forbidden_keys.extend(["metaxIdentifier"]) + forbidden_keys.add("metaxIdentifier") if any(i in data for i in forbidden_keys): reason = f"Some items (e.g: {', '.join(forbidden_keys)}) cannot be changed." LOG.error(reason)