From 421f0ee2c4e2c3efca1a71a7e2f628de0ab64fa0 Mon Sep 17 00:00:00 2001
From: Florian Spreckelsen <f.spreckelsen@indiscale.com>
Date: Tue, 4 Feb 2025 18:09:26 +0100
Subject: [PATCH] WIP: Add treatment for events

---
 .../src/ext/js/ext_samplemanagement.js        |  3 -
 .../sample_helpers/sample_upload_get_event.py | 94 +++++++++++++++++++
 2 files changed, 94 insertions(+), 3 deletions(-)
 create mode 100644 sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_upload_get_event.py

diff --git a/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/ext_samplemanagement.js b/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/ext_samplemanagement.js
index afa88df..a0fc26c 100644
--- a/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/ext_samplemanagement.js
+++ b/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/ext_samplemanagement.js
@@ -38,10 +38,7 @@ const ext_samplemanagement = function($, navbar, log, form_elements, form_panel,
     ];
     const required_column_names = [
         "LinkAhead ID",
-        "Date collected start",
         "Main User",
-        "Gear",
-        "Gear configuration",
         "Latitude start",
         "Longitude start",
         "Collection",
diff --git a/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_upload_get_event.py b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_upload_get_event.py
new file mode 100644
index 0000000..a149876
--- /dev/null
+++ b/sample-management-custom/caosdb-server/scripting/bin/sample_helpers/sample_upload_get_event.py
@@ -0,0 +1,94 @@
+#
+# 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
+import pandas as pd
+
+from caosadvancedtools.datainconsistency import DataInconsistencyError
+
+from .utils import get_column_header_name, get_entity_name
+from ..bis_utils import return_value_if_not_none
+
+
+def add_event_to_sample(sample: db.Record, data: pd.Series) -> db.Record:
+    """Take a given sample and attach an event Record from the
+    data. Then, return the sample.
+
+    """
+
+    _perform_sanity_checks(sample, data)
+    
+
+    return sample
+
+
+def _create_position(mode: str, lat: float, lng: float, ele: float):
+    pos = db.Record()
+    if mode.lower() == "start":
+        pos.add_parent(get_entity_name("StartPosition"))
+    elif mode.lower() == "stop":
+        pos.add_parent(get_entity_name("StopPosition"))
+    else:
+        pos.add_parent(get_entity_name("Position"))
+
+    pos.add_property(name=get_entity_name("latitude"), value=lat)
+    pos.add_property(name=get_entity_name("longitude"), value=lng)
+    pos.add_property(name=get_entity_name("elevation"), value=ele)
+
+    return pos
+
+
+def _perform_sanity_checks(sample, data):
+
+    if (get_column_header_name("end_date") in data and
+            return_value_if_not_none(data[get_column_header_name("end_date")]) is not None):
+        if (get_column_header_name("start_date") not in data or
+                return_value_if_not_none(data[get_column_header_name("start_date")]) is None):
+            raise DataInconsistencyError(
+                f"Sample with {get_entity_name('entity_id')} {sample.id} has a "
+                f"{get_column_header_name('end_date')} but no valid "
+                f"{get_column_header_name('start_date')}."
+            )
+
+    for name in ["start", "stop"]:
+        bool_list = [get_column_header_name(f"{val}_{name}") in data for val in [
+            "latitude", "longitude", "elevation"]]
+        raise_error = False
+        if bool_list.any():
+            if not bool_list.all():
+                raise_error = True
+            elif [return_value_if_not_none(data[get_column_header_name(f"{val}_{name}")]) is None for val in ["latitude", "longitude", "elevation"]].any():
+                raise_error = True
+
+        if raise_error:
+            raise DataInconsistencyError(
+                f"Sample with {get_entity_name('entity_id')} {sample.id} has an "
+                f"invalid {name} position.  Please make sure that latitude, longitude, "
+                "and elevation are provided."
+            )
+
+    # only need to check lat since we already checked that if lat is
+    # present, lng and ele are present, too
+    if (get_column_header_name("latitude_stop") in data and
+            return_value_if_not_none(data[get_column_header_name("latitude_stop")]) is not None):
+        if (get_column_header_name("latitude_start") not in data or
+                return_value_if_not_none(data[get_column_header_name("latitude_start")]) is not None):
+            raise DataInconsistencyError(
+                f"Sample with {get_entity_name('entity_id')} {sample.id} has a "
+                f"{get_entity_name('StopPosition')} but no valid "
+                f"{get_entity_name('StartPosition')}."
+            )
-- 
GitLab