diff --git a/sample-management-custom/caosdb-server/scripting/bin/export_container_csv.py b/sample-management-custom/caosdb-server/scripting/bin/export_container_csv.py index a23c47a849941e089d81f1b8afccf26e4ef02b98..2ddc7bdd3c592727b08e59e5e0090cd473d42eb5 100755 --- a/sample-management-custom/caosdb-server/scripting/bin/export_container_csv.py +++ b/sample-management-custom/caosdb-server/scripting/bin/export_container_csv.py @@ -37,17 +37,17 @@ from linkahead.exceptions import (EmptyUniqueQueryError, from bis_utils import (create_email_with_link_text, get_description_row, get_email_from_username, send_mail_with_defaults) +from sample_helpers.utils import (CONSTANTS, get_column_header_name, + get_entity_name) # suppress warning of diff function -apilogger = logging.getLogger("caosdb.apiutils") +apilogger = logging.getLogger("linkahead.apiutils") apilogger.setLevel(logging.ERROR) logger = logging.getLogger("caosadvancedtools") -CACHE = {} - -ERROR_PREFIX = 'Something went wrong: ' -ERROR_SUFFIX = ' Please conatct <a href="mailto:biosamples@geomar.de">biosamples@geomar.de</a> if you encounter this issue.' +ERROR_PREFIX = CONSTANTS["error_prefix"] +ERROR_SUFFIX = CONSTANTS["error_suffix"] def cached_query(query, unique=False): @@ -66,18 +66,20 @@ def reverse_semicolon_separated_list(value): def generate_label_text(entity): - if entity.get_property("Custom label") is not None and entity.get_property("Custom label").value: - label = entity.get_property("Custom label").value + custom_label_prop_name = get_entity_name("custom_label_prop") + if entity.get_property(custom_label_prop_name) is not None and entity.get_property(custom_label_prop_name).value: + label = entity.get_property(custom_label_prop_name).value else: - label = entity.get_property("BIS label").value if entity.get_property( - "BIS label") is not None else None + container_label_prop_name = get_entity_name("container_label_prop") + label = entity.get_property(container_label_prop_name).value if entity.get_property( + container_label_prop_name) is not None else None if not label: if not entity.name: return entity.id return f"{entity.id} {entity.name}" if f"{label}" == f"{entity.id}": - # prevent special case of BisLabel = BisID resulting in a preview + # prevent special case of Label = ID resulting in a preview # that looks like "id, id". return entity.id return f"{entity.id} {label}" @@ -85,7 +87,9 @@ def generate_label_text(entity): def extract_parent_container(record, key): - possible_parents = cached_query(f"FIND Container WHICH REFERENCES {record.id}") + possible_parents = cached_query( + f"FIND {get_entity_name('container_rt')} WHICH REFERENCES {record.id}" + ) values = [] for par in possible_parents: @@ -103,9 +107,11 @@ def extract_bis_id(record, key): def extract_custom_label(record, key): - if record.get_property("Custom label") is not None: + custom_label_prop_name = get_entity_name("custom_label_prop") + + if record.get_property(custom_label_prop_name) is not None: - return record.get_property("Custom label").value + return record.get_property(custom_label_prop_name).value return None @@ -117,8 +123,9 @@ def extract_pi(record, key): pi_id = record.get_property("PI").value pi_rec = cached_query(f"FIND RECORD Person WITH ID={pi_id}", unique=True) - if pi_rec.get_property("Abbreviation") is not None and pi_rec.get_property("Abbreviation").value is not None: - return pi_rec.get_property("Abbreviation").value + abbr_prop_name = get_entity_name("abbreviation_prop") + if pi_rec.get_property(abbr_prop_name) is not None and pi_rec.get_property(abbr_prop_name).value is not None: + return pi_rec.get_property(abbr_prop_name).value return pi_id return None @@ -145,11 +152,14 @@ def extract_collection(record, key): def extract_container_type(record, key): - if record.get_property("ContainerType") is not None and record.get_property("ContainerType").value is not None: - ct_id = record.get_property("ContainerType").value - ctr = cached_query(f"FIND ContainerType WITH ID={ct_id}", unique=True) - if ctr.get_property("Abbreviation") is not None and ctr.get_property("Abbreviation").value is not None: - return ctr.get_property("Abbreviation").value + abbr_prop_name = get_entity_name("abbreviation_prop") + containertype_rt_name = get_entity_name("containertype_rt") + + if record.get_property(containertype_rt_name) is not None and record.get_property(containertype_rt_name).value is not None: + ct_id = record.get_property(containertype_rt_name).value + ctr = cached_query(f"FIND {containertype_rt_name} WITH ID={ct_id}", unique=True) + if ctr.get_property(abbr_prop_name) is not None and ctr.get_property(abbr_prop_name).value is not None: + return ctr.get_property(abbr_prop_name).value return ct_id return None @@ -159,7 +169,7 @@ def extract_storage_chain(record, key): def find_referencing_containers(eid): containing_containers = [] - candidates = cached_query(f"FIND CONTAINER WHICH REFERENCES {eid}") + candidates = cached_query(f"FIND {get_entity_name('container_rt')} WHICH REFERENCES {eid}") if len(candidates) > 1: logger.debug(f"Entity {eid} is referenced by more than one container.") return [] @@ -183,25 +193,25 @@ def default_extractor(record, key): EXTRACTORS = { - "BIS ID": extract_bis_id, - "BIS label": default_extractor, - "Collection": extract_collection, - "Container Contents": default_extractor, - "Container size": default_extractor, - "Container type": extract_container_type, - "Custom label": extract_custom_label, - "PDFReport": default_extractor, - "PI": extract_pi, - "Parent container": extract_parent_container, - "Storage chain": extract_storage_chain, + get_column_header_name("entity_id"): extract_bis_id, + get_column_header_name("container_label_prop"): default_extractor, + get_column_header_name("Collection"): extract_collection, + get_column_header_name("container_size_prop"): default_extractor, + get_column_header_name("containertype_rt"): extract_container_type, + get_column_header_name("custom_label_prop"): extract_custom_label, + get_column_header_name("PDFReport"): default_extractor, + get_column_header_name("PI"): extract_pi, + get_column_header_name("Parent container"): extract_parent_container, + get_column_header_name("Storage chain"): extract_storage_chain, } # List of sample properties to be ignored because they are treated # otherwise. Similar, but not identical to SPECIAL TREATMENT. IGNORE_KEYS = [ - "Child container", # To be changed by updating the child, not the parent. - "ContainerType", # handled separately above - "Responsible", # Not to be changed via upload + # To be changed by updating the child, not the parent. + get_column_header_name("child_container_prop"), + get_column_header_name("containertype_rt"), # handled separately above + get_column_header_name("Responsible"), # Not to be changed via upload ] @@ -230,7 +240,7 @@ def retrieve_containers(data): try: container.append( cached_query( - f"FIND RECORD Container WITH id = '{bis_id}'", + f"FIND RECORD {get_entity_name('container_rt')} WITH id = '{bis_id}'", unique=True)) except EmptyUniqueQueryError as e: # we want to warn about these @@ -241,7 +251,8 @@ def retrieve_containers(data): try: container.append( cached_query( - f"FIND RECORD Container WITH id = '{next_bis_id}'", + f"FIND RECORD {get_entity_name('container_rt')} WITH id = '{ + next_bis_id}'", unique=True)) found_at_least_one_in_range = True except EmptyUniqueQueryError as e: @@ -355,7 +366,7 @@ def main(): f"{ke}\n\nPlease contact your admin." ) else: - msg = "{}export_sample_csv.py was called without the JSON file in args.{}".format( + msg = "{} export_container_csv.py was called without the JSON file in args. {}".format( ERROR_PREFIX, ERROR_SUFFIX) logger.error(msg) diff --git a/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/default_constants.yml b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/default_constants.yml index 729d795245c2b34e68e1b36d8b4cd47c1bb07d0b..ff0dd5afbad69e014f0f4e440660c43065d5a1d1 100644 --- a/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/default_constants.yml +++ b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/default_constants.yml @@ -25,6 +25,7 @@ entity_names: container_rt: Container container_size_prop: "Container size" containertype_rt: ContainerType + custom_label_prop: "Custom label" labelcounter_prop: Counter labelcounter_rt: LabelCounter responsible_rt: Responsible