diff --git a/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/container_update_get_parent.py b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/container_update_get_parent.py new file mode 100644 index 0000000000000000000000000000000000000000..4d11a3b74453298e13b2185ac4fbcf5650f2bb0f --- /dev/null +++ b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/container_update_get_parent.py @@ -0,0 +1,35 @@ +# +# Copyright (C) 2025 Indiscale GmbH <info@indiscale.com> +# Copyright (C) 2025 Florian Spreckelsen <f.spreckelsen@indiscale.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +import linkahead as db + +from .utils import get_entity_name + + +def get_container_by_identifier(identifier: str) -> db.Record: + """Return a container identified by the given string identifier.""" + # First try (integer) ID + try: + identifier = int(identifier) + query = f"FIND '{get_entity_name('container_rt')}' WITH ID={identifier}" + except ValueError: + # Then fall back to name or label + query = (f"FIND '{get_entity_name('container_rt')}' WITH " + f"name='{identifier}' OR WITH " + f"'{get_entity_name('container_label_prop')}'='{identifier}'") + + return db.cached.cached_query(query) diff --git a/sample-management-custom/caosdb-server/scripting/bin/update_containers.py b/sample-management-custom/caosdb-server/scripting/bin/update_containers.py index 6334551716ec80205b3765d81907f71e69a7026d..d48491686bf52ddbd66cce6deb9a41732b243464 100755 --- a/sample-management-custom/caosdb-server/scripting/bin/update_containers.py +++ b/sample-management-custom/caosdb-server/scripting/bin/update_containers.py @@ -36,13 +36,15 @@ from linkahead.cached import cached_query, cached_get_entity_by from bis_utils import (get_do_not_insert_type_names, replace_entity_urls_by_ids, whitespace_cleanup_in_df) +from sample_helpers.container_update_get_parent import get_container_by_identifier +from sample_helpers.utils import CONSTANTS # suppress warning of diff function apilogger = logging.getLogger("linkahead.apiutils") apilogger.setLevel(logging.ERROR) -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"] logger = logging.getLogger("caosadvancedtools") @@ -55,15 +57,6 @@ def _value_in_row(key, row): return True -def _get_parent_by_identifier(parent_identifier): - """Get parent specified either by BIS ID, name, or BIS label.""" - try: - parent_identifier = int(parent_identifier) - query = f"FIND Container WITH ID={parent_identifier}" - except ValueError: - query = (f"FIND Container WITH name='{parent_identifier}' " - f"OR WITH 'BIS label'='{parent_identifier}'") - return cached_query(query) def get_parser(): @@ -111,7 +104,7 @@ def main(): if _value_in_row("Parent container", row): parent_identifier = row["Parent container"] - parent = _get_parent_by_identifier(parent_identifier) + parent = get_container_by_identifier(parent_identifier) if len(parent) == 0: logger.error( f"Couldn't find parent with identifier '{parent_identifier}' in row {index+1}.") @@ -143,7 +136,7 @@ def main(): if _value_in_row("Parent container", row): parent_identifier = row["Parent container"] # This has already been checked above for uniqueness - candidate = _get_parent_by_identifier(parent_identifier)[0] + candidate = get_container_by_identifier(parent_identifier)[0] # A bit redundant, but we need the exact Python object here that is in the parent_containers list. parent = parent_containers.get_entity_by_id(candidate.id)