From cbad53cddfd6d0cd3194b8e8970ee82d856134fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sabine=20Schr=C3=B6der?= <s.schroeder@fz-juelich.de> Date: Sat, 3 Oct 2020 12:36:28 +0200 Subject: [PATCH] #9: added API for deleting fields (f. ex. roles) from stationmeta --- toardb/stationmeta/crud.py | 24 ++++++++++++++++-------- toardb/stationmeta/stationmeta.py | 7 +++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py index 3ffc22e..0a74642 100644 --- a/toardb/stationmeta/crud.py +++ b/toardb/stationmeta/crud.py @@ -4,7 +4,7 @@ Create, Read, Update, Delete functionality """ -from sqlalchemy import cast, Text, insert, update +from sqlalchemy import cast, Text, insert, update, delete from typing import List from geoalchemy2.types import Geometry from sqlalchemy.orm import Session @@ -207,18 +207,11 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc db_object = get_unique_stationmeta_role(db, db_role.role, db_role.contact_id, db_role.status) if db_object: role_id = db_object.id - print("found role_id:",role_id) else: - print("did not find role: now inserting new role") - print(r) - print(db_role.role) - print(db_role.status) - print(db_role.contact_id) db.add(db_role) db.commit() db.refresh(db_role) role_id = db_role.id - print("did not find role: inserted new role_id ",role_id) db.execute(insert(stationmeta_core_stationmeta_roles_table).values(station_id=station_id, role_id=role_id)) db.commit() # store annotations and update association table @@ -279,3 +272,18 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc # there's a mismatch with coordinates --> how to automatically switch back and forth?! db_stationmeta.coordinates = tmp_coordinates 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) + # 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) + db.commit() + # there's a mismatch with coordinates --> how to automatically switch back and forth?! + db_stationmeta.coordinates = tmp_coordinates + return db_stationmeta diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py index e3161b4..240a3a9 100644 --- a/toardb/stationmeta/stationmeta.py +++ b/toardb/stationmeta/stationmeta.py @@ -65,3 +65,10 @@ def patch_stationmeta_core(station_code: str, stationmeta: schemas.StationmetaPa if db_stationmeta is None: raise HTTPException(status_code=404, detail="Station for patching not found.") return crud.patch_stationmeta(db=db, station_id=db_stationmeta.id, stationmeta=stationmeta) + +@router.patch('/stationmeta/delete_field/{station_code}', response_model=schemas.StationmetaPatch) +def patch_stationmeta_core(station_code: str, field: str, db: Session = Depends(get_db)): + 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) -- GitLab