Skip to content
Snippets Groups Projects

Helper for CreateMetaDataBulk

Merged Thibault Lavigne requested to merge helper-for-create-metadatabulk into master
2 files
+ 286
2
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -105,10 +105,85 @@ class MetaDataMixin(object):
return metadata["id"]
def create_metadatas(
self,
element: Element,
metadatas: list,
):
"""
Create multiple metadatas on an existing element.
This method does not support cache.
:param element Element: The element to create multiple metadata on.
:param metadata_list List(Dict): The list of dict whose keys are the following:
- type : MetaType
- name : str
- value : Union[str, Union[int, float]]
- entity_id : Union[str, None]
"""
assert element and isinstance(
element, Element
), "element shouldn't be null and should be of type Element"
assert metadatas and isinstance(
metadatas, list
), "type shouldn't be null and should be of type list of Dict"
# Make a copy to avoid modifiying the metadata_list argument
metas = []
for index, metadata in enumerate(metadatas):
assert isinstance(
metadata, dict
), f"Element at index {index} in metadata_list: Should be of type dict"
assert metadata.get("type") and isinstance(
metadata.get("type"), MetaType
), "type shouldn't be null and should be of type MetaType"
assert metadata.get("name") and isinstance(
metadata.get("name"), str
), "name shouldn't be null and should be of type str"
assert metadata.get("value") and isinstance(
metadata.get("value"), (str, float, int)
), "value shouldn't be null and should be of type (str or float or int)"
assert metadata.get("entity_id") is None or isinstance(
metadata.get("entity_id"), str
), "entity_id should be None or a str"
metas.append(
{
"type": metadata.get("type").value,
"name": metadata.get("name"),
"value": metadata.get("value"),
"entity_id": metadata.get("entity_id"),
}
)
if self.is_read_only:
logger.warning("Cannot create metadata as this worker is in read-only mode")
return
created_metadatas = self.request(
"CreateMetaDataBulk",
id=element.id,
body={
"worker_version": self.worker_version_id,
"worker_run_id": self.worker_run_id,
"metadata_list": metas,
},
)["metadata_list"]
for meta in created_metadatas:
self.report.add_metadata(element.id, meta["id"], meta["type"], meta["name"])
return created_metadatas
def list_metadata(self, element: Element):
"""
List all metadata linked to an element
This method does not support cache
List all metadata linked to an element.
This method does not support cache.
:param element Element: The element to list metadata on.
"""
Loading