From e2ebce2b5e534a505457dceaa7ee4919c6e6f8e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sabine=20Schr=C3=B6der?= <s.schroeder@fz-juelich.de>
Date: Fri, 9 Oct 2020 16:53:21 +0200
Subject: [PATCH] #9: patch API for deleting fields inserts a changelog entry

---
 toardb/stationmeta/crud.py        | 32 +++++++++++++++++++++++++------
 toardb/stationmeta/stationmeta.py |  2 +-
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py
index 0267db8..cecf6a1 100644
--- a/toardb/stationmeta/crud.py
+++ b/toardb/stationmeta/crud.py
@@ -195,6 +195,13 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate):
     db_stationmeta.coordinates = tmp_coordinates
     return db_stationmeta
 
+def get_field_from_record(db, station_id, field, db_stationmeta):
+    if field in ['roles', 'annotations', 'aux_images', 'aux_docs', 'aux_urls', 'globalmeta']:
+        field =  f"'{field}': " + str(getattr(db_stationmeta,field))
+    else:
+        field =  f"'{field}': " + str(getattr(db_stationmeta,field))
+    return field
+
 def patch_stationmeta(db: Session, description: str, station_id: int, stationmeta: StationmetaPatch):
     stationmeta_dict       = stationmeta.dict()
     # delete empty fields from stationmeta_dict already at this place, to be able to
@@ -227,9 +234,9 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
         if v is not None:
             # prepare changelog entry
             if (number_of_commas == 0):
-                old_value=old_value + f"'{k}': " + str(getattr(db_stationmeta,k)) + '}'
+                old_value=old_value + get_field_from_record(db, station_id, k, db_stationmeta) + '}'
             else:
-                old_value=old_value + f"'{k}': " + str(getattr(db_stationmeta,k)) + ','
+                old_value=old_value + get_field_from_record(db, station_id, k, db_stationmeta) + ','
             number_of_commas = number_of_commas - 1
             setattr(db_stationmeta,k,stationmeta_dict[k])
     result = db.commit()
@@ -344,15 +351,28 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
     return db_stationmeta
 
 
-def delete_stationmeta_field(db: Session, station_id: int, keyword: str):
-    if keyword == 'roles':
-        db.execute(delete(stationmeta_core_stationmeta_roles_table).where(stationmeta_core_stationmeta_roles_table.c.station_id==station_id))
-#       stationmeta_core_stationmeta_roles_table.delete().where(stationmeta_core_stationmeta_roles_table.c.station_id==station_id)
+def delete_stationmeta_field(db: Session, station_id: int, field: str):
+    field_table = {'roles': stationmeta_core_stationmeta_roles_table,
+                   'annotations': stationmeta_core_stationmeta_annotations_table}
+#                  'aux_images': stationmeta_core_stationmeta_aux_images_table,
+#                  'aux_docs': stationmeta_core_stationmeta_aux_docs_table,
+#                  'aux_urls': stationmeta_core_stationmeta_aux_urls_table,
+#                  'globalmeta': stationmeta_core_stationmeta_globalmeta_table}
+    # there are mandatory fields (from stationmeta_core), that cannot be deleted!
+    if ((field == 'roles') or (field == 'annotations')):
+        db.execute(delete(field_table[field]).where(field_table[field].c.station_id==station_id))
     # problem with automatic conversion of coordinates (although not explicitly fetched from database)
     # ==> next two lines are a workaround
     db_stationmeta = db.query(models.StationmetaCore).get(station_id)
     tmp_coordinates = db_stationmeta.coordinates
     db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates)
+    # also write changelog entry
+    type_of_change = get_value_from_str(CL_enum,"SingleValue")
+    description=f"delete field {field}"
+    old_value = get_field_from_record(db, station_id, field, db_stationmeta)
+    db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=type_of_change,
+                                        old_value=old_value, new_value="")
+    db.add(db_changelog)
     db.commit()
     # there's a mismatch with coordinates --> how to automatically switch back and forth?!
     db_stationmeta.coordinates = tmp_coordinates
diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py
index e212141..74b1dd2 100644
--- a/toardb/stationmeta/stationmeta.py
+++ b/toardb/stationmeta/stationmeta.py
@@ -76,4 +76,4 @@ def patch_stationmeta_core(station_code: str, field: str, db: Session = Depends(
     db_stationmeta = crud.get_stationmeta(db, station_code=station_code)
     if db_stationmeta is None:
         raise HTTPException(status_code=404, detail="Station for deleting field not found.")
-    return crud.delete_stationmeta_field(db=db, station_id=db_stationmeta.id, keyword=field)
+    return crud.delete_stationmeta_field(db=db, station_id=db_stationmeta.id, field=field)
-- 
GitLab