Skip to content
Snippets Groups Projects
Commit 2127218c authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

ENH: Make more rt and property names configurable

parent 67b31746
No related branches found
No related tags found
1 merge request!1F awi sams
...@@ -211,64 +211,60 @@ def main(): ...@@ -211,64 +211,60 @@ def main():
logger.warning(f"There is no PI with ID or {get_entity_name('abbreviation_prop')} {pi}. Skipping.") logger.warning(f"There is no PI with ID or {get_entity_name('abbreviation_prop')} {pi}. Skipping.")
# Collection(s) # Collection(s)
if _value_in_row("Collection", row): if _value_in_row(get_column_header_name("Collection"), row):
collection_rt = cached_get_entity_by(query="FIND RECORDTYPE Collection") collection_rt = cached_get_entity_by(query=f"FIND RECORDTYPE '{get_entity_name('Collection')}'")
if not ";" in str(row["Collection"]): if not ";" in str(row[get_column_header_name("Collection")]):
collections = [row["Collection"]] collections = [row[get_column_header_name("Collection")]]
else: else:
collections = [coll.strip() for coll in str(row["Collection"]).split(';')] collections = [coll.strip() for coll in str(row[get_column_header_name("Collection")]).split(';')]
prop_val = [] prop_val = []
for coll in collections: for coll in collections:
try: try:
query = f"FIND RECORD Collection WITH ID={int(coll)}" query = f"FIND '{collection_rt.name}' WITH ID={int(coll)}"
except ValueError: except ValueError:
query = f"FIND RECORD Collection WITH name='{coll}'" query = f"FIND RECORD '{collection_rt.name}' WITH name='{coll}'"
try: try:
coll_rec = cached_get_entity_by(query=query) coll_rec = cached_get_entity_by(query=query)
prop_val.append(coll_rec.id) prop_val.append(coll_rec.id)
except db.EmptyUniqueQueryError: except db.EmptyUniqueQueryError:
logger.warning(f"There is no collection with name or BIS ID {coll}. Skipping.") logger.warning(f"There is no {collection_rt.name} with name or ID {coll}. Skipping.")
continue continue
if prop_val: if prop_val:
if child.get_property("Collection") is not None: if child.get_property(collection_rt.name) is not None:
child.get_property("Collection").datatype = db.LIST("Collection") child.get_property(collection_rt.name).datatype = db.LIST(collection_rt.name)
child.get_property("Collection").value = prop_val child.get_property(collection_rt.name).value = prop_val
else: else:
child.add_property(id=collection_rt.id, name=collection_rt.name, datatype=db.LIST( child.add_property(id=collection_rt.id, name=collection_rt.name, datatype=db.LIST(
"Collection"), value=prop_val) collection_rt.name), value=prop_val)
# Treat Container Contents # Treat Container Contents
if _value_in_row("Container Contents", row): if _value_in_row(get_column_header_name("Container Contents"), row):
if not (_value_in_row("PI", row) and _value_in_row("Collection", row)): contents_prop = cached_get_entity_by(
logger.error( query=f"FIND PROPERTY '{get_entity_name('Container Contents')}'"
f"Container Contents are given for container {child.id} but it " )
"is missing PI and/or Collection info. No updates have been performed."
)
return 1
contents_prop = cached_get_entity_by(query="FIND PROPERTY 'Container Contents'")
if child.get_property(contents_prop.name) is not None: if child.get_property(contents_prop.name) is not None:
child.get_property(contents_prop.name).value = row["Container Contents"] child.get_property(contents_prop.name).value = row[get_column_header_name("Container Contents")]
else: else:
child.add_property(id=contents_prop.id, name=contents_prop.name, child.add_property(id=contents_prop.id, name=contents_prop.name,
value=row["Container Contents"]) value=row[get_column_header_name("Container Contents")])
# Treat PDF Report # Treat PDF Report
if _value_in_row("PDFReport", row): if _value_in_row(get_column_header_name("PDFReport"), row):
pdf_id = row["PDFReport"] pdf_id = row[get_column_header_name("PDFReport")]
try: try:
pdf_id = int(pdf_id) pdf_id = int(pdf_id)
pdf_rec = cached_query(f"FIND FILE PDFReport WITH ID={pdf_id}") pdf_rec = cached_query(f"FIND FILE {get_entity_name('PDFReport')} WITH ID={pdf_id}")
if not pdf_rec: if not pdf_rec:
logger.warning( logger.warning(
f"There is no PDFReport with Bis ID {pdf_id}, so no PDF is attached to container {child.id}.") f"There is no PDFReport with ID {pdf_id}, so no PDF is attached to container {child.id}.")
else: else:
if child.get_property("PDFReport") is not None: if child.get_property(get_entity_name("PDFReport")) is not None:
child.get_property("PDFReport").value = pdf_id child.get_property(get_property("PDFReport")).value = pdf_id
else: else:
child.add_property(id=pdf_rt.id, name=pdf_rt.name, value=pdf_id) child.add_property(id=pdf_rt.id, name=pdf_rt.name, value=pdf_id)
except ValueError: except ValueError:
logger.warning( logger.warning(
f"There is no valid Bis ID provided for container {child.id}." f"There is no valid ID provided for container {child.id}."
f"Provided was {pdf_id}. Skipping") f"Provided was {pdf_id}. Skipping")
# This is a workaround for weird merging errors in the # This is a workaround for weird merging errors in the
...@@ -276,13 +272,13 @@ def main(): ...@@ -276,13 +272,13 @@ def main():
# following release. # following release.
merged = [] merged = []
for par in parent_containers: for par in parent_containers:
if (data['BIS ID'] == par.id).any(): if (data[id_column_name] == par.id).any():
# A container to be updated is used as another containers parent: # A container to be updated is used as another containers parent:
child = child_containers.get_entity_by_id(par.id) child = child_containers.get_entity_by_id(par.id)
# All parents have a child sample property with a value (which may # All parents have a child sample property with a value (which may
# be empty). No child sample has this property, so the following is # be empty). No child sample has this property, so the following is
# okay without checks: # okay without checks:
prop = par.get_property("Child container") prop = par.get_property(child_container_prop.name)
child.add_property(name=prop.name, id=prop.id, value=prop.value) child.add_property(name=prop.name, id=prop.id, value=prop.value)
merged.append(par) merged.append(par)
for par in merged: for par in merged:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment