From 39dfd2ecff7eeb1d2b578b8f38ddf66ef7f19fbf Mon Sep 17 00:00:00 2001
From: schroeder5 <s.schroeder@fz-juelich.de>
Date: Mon, 15 Aug 2022 14:23:05 +0000
Subject: [PATCH] added funtionality of suppressing changelog, if stationmeta
 is patched

---
 toardb/stationmeta/crud.py | 93 ++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 43 deletions(-)

diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py
index a1adfa5..f4a0029 100644
--- a/toardb/stationmeta/crud.py
+++ b/toardb/stationmeta/crud.py
@@ -327,16 +327,18 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
     # still to be done:
     # collect all old/new_values from different sections!
 
+    no_log = (description == 'NOLOG')
     stationmeta_dict       = stationmeta.dict()
     # delete empty fields from stationmeta_dict already at this place, to be able to
     # distinguish between "single value correction in metadata" and "comprehensive metadata revision"
     # (see controlled vocabulary "CL_vocabulary")
     stationmeta_dict2 = {k: v for k, v in stationmeta_dict.items() if v is not None}
     number_of_elements = len(stationmeta_dict2)
-    if (number_of_elements == 1):
-        type_of_change = get_value_from_str(toardb.toardb.CL_vocabulary,"SingleValue")
-    else:
-        type_of_change = get_value_from_str(toardb.toardb.CL_vocabulary,"Comprehensive")
+    if not no_log:
+        if (number_of_elements == 1):
+            type_of_change = get_value_from_str(toardb.toardb.CL_vocabulary,"SingleValue")
+        else:
+            type_of_change = get_value_from_str(toardb.toardb.CL_vocabulary,"Comprehensive")
     roles_data         = stationmeta_dict.pop('roles', None)
     annotations_data   = stationmeta_dict.pop('annotations', None)
     aux_images_data    = stationmeta_dict.pop('aux_images', None)
@@ -351,17 +353,19 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
     tmp_coordinates = db_obj.coordinates
     db_stationmeta = db.query(models.StationmetaCore).get(station_id)
     db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates)
-    old_value="{"
-    number_of_commas = number_of_elements - 1
+    if not no_log:
+        old_value="{"
+        number_of_commas = number_of_elements - 1
     for k, v in stationmeta_dict.items():
         if v is not None:
             # prepare changelog entry
-            field = get_field_from_record(db, station_id, k, db_stationmeta)
-            if (number_of_commas == 0):
-                old_value=old_value + field + '}'
-            else:
-                old_value=old_value + field + ','
-            number_of_commas = number_of_commas - 1
+            if not no_log:
+                field = get_field_from_record(db, station_id, k, db_stationmeta)
+                if (number_of_commas == 0):
+                    old_value=old_value + field + '}'
+                else:
+                    old_value=old_value + field + ','
+                number_of_commas = number_of_commas - 1
             setattr(db_stationmeta,k,stationmeta_dict[k])
     # do the following for every entry that uses the controlled vocabulary!
     # this should be improved!
@@ -379,19 +383,20 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
     # store roles and update association table
     if roles_data:
         # prepare changelog entry/entries
-        description = description + f"; add role"
-        db_old_roles = get_stationmeta_roles(db, station_id)
-        old_value = old_value + "'roles': {"
-        for oldr in db_old_roles:
-            old_role = get_stationmeta_role_by_id(db, oldr.role_id)
-            old_value=old_value + "{'role': '" + get_str_from_value(toardb.toardb.RC_vocabulary,old_role.role) + \
-                                "', 'status': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
-                                "', 'contact_id': " + str(old_role.contact_id) + '}'
-        if (number_of_commas == 0):
-            old_value=old_value + '}'
-        else:
-            old_value=old_value + ','
-        number_of_commas = number_of_commas - 1
+        if not no_log:
+            description = description + f"; add role"
+            db_old_roles = get_stationmeta_roles(db, station_id)
+            old_value = old_value + "'roles': {"
+            for oldr in db_old_roles:
+                old_role = get_stationmeta_role_by_id(db, oldr.role_id)
+                old_value=old_value + "{'role': '" + get_str_from_value(toardb.toardb.RC_vocabulary,old_role.role) + \
+                                    "', 'status': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
+                                    "', 'contact_id': " + str(old_role.contact_id) + '}'
+            if (number_of_commas == 0):
+                old_value=old_value + '}'
+            else:
+                old_value=old_value + ','
+            number_of_commas = number_of_commas - 1
         for r in roles_data:
             db_role = models.StationmetaRole(**r)
             db_role.role = get_value_from_str(toardb.toardb.RC_vocabulary,db_role.role)
@@ -410,20 +415,21 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
     # store annotations and update association table
     if annotations_data:
         # prepare changelog entry/entries
-        description = description + f"; add annotation"
-        db_old_annotations = get_stationmeta_annotations(db, station_id)
-        for olda in db_old_annotations:
-            old_annotation = get_stationmeta_annotation_by_id(db, olda.annotation_id)
-            old_value=old_value + "{'kind': '" + get_str_from_value(toardb.toardb.RC_vocabulary,old_role.kind) + \
-                                "', 'text': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
-                                "', 'date_added': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
-                                "', 'approved': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
-                                "', 'contributor_id': " + str(old_role.contact_id) + '}'
-        if (number_of_commas == 0):
-            old_value=old_value + '}'
-        else:
-            old_value=old_value + ','
-        number_of_commas = number_of_commas - 1
+        if not no_log:
+            description = description + f"; add annotation"
+            db_old_annotations = get_stationmeta_annotations(db, station_id)
+            for olda in db_old_annotations:
+                old_annotation = get_stationmeta_annotation_by_id(db, olda.annotation_id)
+                old_value=old_value + "{'kind': '" + get_str_from_value(toardb.toardb.RC_vocabulary,old_role.kind) + \
+                                    "', 'text': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
+                                    "', 'date_added': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
+                                    "', 'approved': '" + get_str_from_value(toardb.toardb.RS_vocabulary,old_role.status) + \
+                                    "', 'contributor_id': " + str(old_role.contact_id) + '}'
+            if (number_of_commas == 0):
+                old_value=old_value + '}'
+            else:
+                old_value=old_value + ','
+            number_of_commas = number_of_commas - 1
         for a in annotations_data:
             db_annotation = models.StationmetaAnnotation(**a)
             # check whether annotation is already present in database
@@ -502,10 +508,11 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
         db.refresh(db_globalmeta)
         stationmeta_dict2 = {k: v for k, v in stationmeta_dict2['globalmeta'].items() if v is not None}
         old_value = str(old_global)
-    db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=type_of_change,
-                                        old_value=old_value, new_value=str(stationmeta_dict2))
-    db.add(db_changelog)
-    db.commit()
+    if not no_log:
+        db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=type_of_change,
+                                            old_value=old_value, new_value=str(stationmeta_dict2))
+        db.add(db_changelog)
+        db.commit()
     # there's a mismatch with coordinates --> how to automatically switch back and forth?!
     db_stationmeta.coordinates = tmp_coordinates
     status_code = 200
-- 
GitLab