Skip to content

Commit

Permalink
rebase with metax integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Mar 7, 2022
1 parent e67d7d1 commit d386c7d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions metadata_backend/api/handlers/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion metadata_backend/api/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d386c7d

Please sign in to comment.