From aaf057af48008bde561a18f0d7a6f23339382886 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Fri, 21 Mar 2025 14:23:26 +0100
Subject: [PATCH] FEAT: add columns required for event insertion automatically

---
 .../scripting/bin/register_new_samples.py     |  6 +-
 .../sample_registration_check_column_names.py | 68 +++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_registration_check_column_names.py

diff --git a/sample-management-custom/caosdb-server/scripting/bin/register_new_samples.py b/sample-management-custom/caosdb-server/scripting/bin/register_new_samples.py
index 15e22bd..a320c0d 100755
--- a/sample-management-custom/caosdb-server/scripting/bin/register_new_samples.py
+++ b/sample-management-custom/caosdb-server/scripting/bin/register_new_samples.py
@@ -13,6 +13,7 @@ from bis_utils import (create_email_with_link_text,
                        get_options_row, send_mail_with_defaults)
 from sample_helpers.sample_registration_get_person_identifier import get_person_identifier
 from sample_helpers.sample_registration_post_processing import post_process_samples
+from sample_helpers.sample_registration_check_column_names import check_column_names
 from sample_helpers.utils import CONSTANTS, get_column_header_name, get_entity_name
 
 ERROR_PREFIX = CONSTANTS["error_prefix"]
@@ -34,8 +35,9 @@ def get_column_names(data):
     other_names = data["required_column_names"].split(',') + data["column_names"]
     starting_names.extend([name.strip()
                           for name in other_names if name.strip() not in starting_names])
-    return [get_column_header_name(name) for name in starting_names]
-
+    cnames = [get_column_header_name(name) for name in starting_names]
+    check_column_names(cnames)
+    return cnames
 
 def create_sample_entities(data):
     responsible_person_id = int(data["responsible_person"])
diff --git a/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_registration_check_column_names.py b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_registration_check_column_names.py
new file mode 100644
index 0000000..79f62dd
--- /dev/null
+++ b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_registration_check_column_names.py
@@ -0,0 +1,68 @@
+#
+# Copyright (C) 2025 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2025 Henrik tom Wörden <h.tomwoerden@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
+import logging
+
+from caosadvancedtools.datainconsistency import DataInconsistencyError
+
+from .utils import CONSTANTS, get_column_header_name, get_entity_name
+from .sample_upload_column_definitions import use_custom_names
+
+logger = logging.getLogger("caosadvancedtools")
+
+def check_column_names(names: list[str]) -> None:
+    event_and_position_properties = use_custom_names([
+        "Campaign",
+        "Biome",
+        "Device",
+        "Elevation start",
+        "Elevation stop",
+        "End date",
+        "responsible_person_event",
+        "igsn_doi_prop",
+        "Latitude start",
+        "Latitude stop",
+        "Level",
+        "locality_description_prop",
+        "locality_name_prop",
+        "Longitude start",
+        "Longitude stop",
+        "Sphere",
+        "Start date",
+    ])
+    needs_events = False
+    for name in names:
+        if name in event_and_position_properties:
+            needs_events = True
+    appended = []
+    if needs_events:
+        for name in use_custom_names([
+            "Longitude start",
+            "Latitude start",
+            "Elevation start",
+            "Start date",
+        ]):
+            if name not in names:
+                names.append(name)
+                appended.append(name)
+
+    if appended:
+        logger.warning(
+            "The following columns were added in order to allow consitent data insertion:\n"
+            f"{', '.join(appended)}"
+        )
-- 
GitLab