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

#17: REST APIs to changelog tables are build; #9: patch API for stationmeta...

#17: REST APIs to changelog tables are build; #9: patch API for stationmeta creates a separate changelog entry for every field changed in the metadata
parent 6d7ba4fa
No related branches found
No related tags found
No related merge requests found
Pipeline #47881 passed
......@@ -62,6 +62,10 @@ def get_all_stationmeta(db: Session, skip : int = 0, limit: int = None):
return db_objects
def get_stationmeta_changelog(db: Session, station_id: int):
return db.query(models.StationmetaChangelog).filter(models.StationmetaChangelog.station_id == station_id).all()
# is this internal, or should this also go to public REST api?
def get_unique_stationmeta_role(db: Session, role: int, contact_id: int, status: int):
db_object = db.query(models.StationmetaRole).filter(models.StationmetaRole.role == role) \
......@@ -189,8 +193,6 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
aux_urls_data = stationmeta_dict.pop('aux_urls', None)
globalmeta_data = stationmeta_dict.pop('globalmeta', None)
globalservice_data = stationmeta_dict.pop('globalservice', None)
# prepare changelog entry/entries
db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=1)
# there's a mismatch with coordinates --> how to automatically switch back and forth?!
# ==> the following two commands are not working
# ==> workaround
......@@ -202,14 +204,21 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates)
for k, v in stationmeta_dict.items():
if v is not None:
# prepare changelog entry/entries
db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=1)
db_changelog.old_value=str(getattr(db_stationmeta,k))
setattr(db_stationmeta,k,stationmeta_dict[k])
db_changelog.new_value=str(getattr(db_stationmeta,k))
result = db.commit()
db_changelog.description=description + f"; field: {k}"
db.add(db_changelog)
result = db.commit()
db.refresh(db_stationmeta)
# store roles and update association table
if roles_data:
for r in roles_data:
# prepare changelog entry/entries
db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=1)
db_changelog.old_value=str(getattr(db_stationmeta,"roles"))
db_role = models.StationmetaRole(**r)
db_role.role = get_value_from_str(RC_enum,db_role.role)
db_role.status = get_value_from_str(RS_enum,db_role.status)
......@@ -223,6 +232,8 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
db.refresh(db_role)
role_id = db_role.id
db.execute(insert(stationmeta_core_stationmeta_roles_table).values(station_id=station_id, role_id=role_id))
db_changelog.new_value=str(r)
db.add(db_changelog)
db.commit()
# store annotations and update association table
if annotations_data:
......@@ -279,8 +290,6 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
db.add(db_globalservice)
db.commit()
db.refresh(db_globalservice)
# add patch to changelog table
db.add(db_changelog)
db.commit()
# there's a mismatch with coordinates --> how to automatically switch back and forth?!
db_stationmeta.coordinates = tmp_coordinates
......
......@@ -42,6 +42,11 @@ def get_stationmeta(station_code: str, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="Data not found.")
return db_stationmeta
@router.get('/stationmeta_changelog/{station_id}', response_model=List[schemas.StationmetaChangelog])
def get_stationmeta_changelog(station_id: int, db: Session = Depends(get_db)):
db_changelog = crud.get_stationmeta_changelog(db, station_id=station_id)
return db_changelog
#some more gets to be tested:
# - get stationmeta_global
# - get stationmeta_aux
......
......@@ -58,6 +58,10 @@ def get_timeseries_by_unique_constraints(db: Session, station_id: int, variable_
return db_object
def get_timeseries_changelog(db: Session, timeseries_id: int):
return db.query(models.TimeseriesChangelog).filter(models.TimeseriesChangelog.timeseries_id == timeseries_id).all()
# is this internal, or should this also go to public REST api?
# do we need this at all?
def get_role_ids_of_timeseries(db: Session, timeseries_id: int):
......
......@@ -27,6 +27,11 @@ def get_timeseries(timeseries_id: int, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="Timeseries not found.")
return db_timeseries
@router.get('/timeseries_changelog/{timeseries_id}', response_model=List[schemas.TimeseriesChangelog])
def get_timeseries_changelog(timeseries_id: int, db: Session = Depends(get_db)):
db_changelog = crud.get_timeseries_changelog(db, timeseries_id=timeseries_id)
return db_changelog
#some more gets to be tested:
#
#
......
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