Skip to content
Snippets Groups Projects
Commit e2ebce2b authored by Sabine Schröder's avatar Sabine Schröder
Browse files

#9: patch API for deleting fields inserts a changelog entry

parent 90d30342
No related branches found
No related tags found
No related merge requests found
Pipeline #48110 passed
......@@ -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
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment