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

#9: adding roles produces human readable changelog entries

parent 0daad653
No related branches found
No related tags found
No related merge requests found
Pipeline #47992 passed
...@@ -4,7 +4,7 @@ Create, Read, Update, Delete functionality ...@@ -4,7 +4,7 @@ Create, Read, Update, Delete functionality
""" """
from sqlalchemy import cast, Text, insert, update, delete from sqlalchemy import cast, Text, insert, update, delete, select
from typing import List from typing import List
from geoalchemy2.types import Geometry from geoalchemy2.types import Geometry
from geoalchemy2.elements import WKBElement, WKTElement from geoalchemy2.elements import WKBElement, WKTElement
...@@ -19,7 +19,7 @@ from .models import StationmetaCore, StationmetaChangelog, stationmeta_core_stat ...@@ -19,7 +19,7 @@ from .models import StationmetaCore, StationmetaChangelog, stationmeta_core_stat
from toardb.generic.models import RS_enum, RC_enum from toardb.generic.models import RS_enum, RC_enum
from .schemas import get_coordinates_from_geom, get_geom_from_coordinates, StationmetaCreate, StationmetaPatch, Coordinates from .schemas import get_coordinates_from_geom, get_geom_from_coordinates, StationmetaCreate, StationmetaPatch, Coordinates
from pydantic import ValidationError from pydantic import ValidationError
from toardb.utils.utils import get_value_from_str from toardb.utils.utils import get_value_from_str, get_str_from_value
def get_stationmeta_core(db: Session, station_code: str): def get_stationmeta_core(db: Session, station_code: str):
...@@ -62,11 +62,20 @@ def get_all_stationmeta(db: Session, skip : int = 0, limit: int = None): ...@@ -62,11 +62,20 @@ def get_all_stationmeta(db: Session, skip : int = 0, limit: int = None):
return db_objects return db_objects
# is this internal, or should this also go to public REST api?
# - get_stationmeta_roles
# - get_stationmeta_changelog
# - get_unique_stationmeta_role
# - get_stationmeta_role_by_id
# - get_unique_stationmeta_annotation
def get_stationmeta_roles(db: Session, station_id: int):
return db.execute(select([stationmeta_core_stationmeta_roles_table]).where(stationmeta_core_stationmeta_roles_table.c.station_id == station_id))
def get_stationmeta_changelog(db: Session, station_id: int): def get_stationmeta_changelog(db: Session, station_id: int):
return db.query(models.StationmetaChangelog).filter(models.StationmetaChangelog.station_id == station_id).all() 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): 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) \ db_object = db.query(models.StationmetaRole).filter(models.StationmetaRole.role == role) \
.filter(models.StationmetaRole.contact_id == contact_id) \ .filter(models.StationmetaRole.contact_id == contact_id) \
...@@ -74,8 +83,10 @@ def get_unique_stationmeta_role(db: Session, role: int, contact_id: int, status: ...@@ -74,8 +83,10 @@ def get_unique_stationmeta_role(db: Session, role: int, contact_id: int, status:
.first() .first()
return db_object return db_object
def get_stationmeta_role_by_id(db: Session, role_id):
return db.query(models.StationmetaRole).filter(models.StationmetaRole.id == role_id).first()
# is this internal, or should this also go to public REST api?
def get_unique_stationmeta_annotation(db: Session, text: str, contributor_id: int): def get_unique_stationmeta_annotation(db: Session, text: str, contributor_id: int):
db_object = db.query(models.StationmetaAnnotation).filter(models.StationmetaAnnotation.text == text) \ db_object = db.query(models.StationmetaAnnotation).filter(models.StationmetaAnnotation.text == text) \
.filter(models.StationmetaAnnotation.contributor_id == contributor_id) \ .filter(models.StationmetaAnnotation.contributor_id == contributor_id) \
...@@ -205,11 +216,11 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet ...@@ -205,11 +216,11 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
for k, v in stationmeta_dict.items(): for k, v in stationmeta_dict.items():
if v is not None: if v is not None:
# prepare changelog entry/entries # prepare changelog entry/entries
db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=1) desc = description + f"; field: {k}"
db_changelog = StationmetaChangelog(description=desc, station_id=station_id, author_id=1, type_of_change=1)
db_changelog.old_value=str(getattr(db_stationmeta,k)) db_changelog.old_value=str(getattr(db_stationmeta,k))
setattr(db_stationmeta,k,stationmeta_dict[k]) setattr(db_stationmeta,k,stationmeta_dict[k])
db_changelog.new_value=str(getattr(db_stationmeta,k)) db_changelog.new_value=str(getattr(db_stationmeta,k))
db_changelog.description=description + f"; field: {k}"
db.add(db_changelog) db.add(db_changelog)
result = db.commit() result = db.commit()
db.refresh(db_stationmeta) db.refresh(db_stationmeta)
...@@ -217,8 +228,16 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet ...@@ -217,8 +228,16 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
if roles_data: if roles_data:
for r in roles_data: for r in roles_data:
# prepare changelog entry/entries # prepare changelog entry/entries
db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=1, type_of_change=1) desc = description + f"; add role"
db_changelog.old_value=str(getattr(db_stationmeta,"roles")) db_changelog = StationmetaChangelog(description=desc, station_id=station_id, author_id=1, type_of_change=1)
db_old_roles = get_stationmeta_roles(db, station_id)
oldvalue=''
for oldr in db_old_roles:
old_role = get_stationmeta_role_by_id(db, oldr.role_id)
oldvalue=oldvalue + "{'role': '" + get_str_from_value(RC_enum,old_role.role) + \
"', 'status': '" + get_str_from_value(RS_enum,old_role.status) + \
"', 'contact_id': " + str(old_role.contact_id) + '}'
db_changelog.old_value=oldvalue
db_role = models.StationmetaRole(**r) db_role = models.StationmetaRole(**r)
db_role.role = get_value_from_str(RC_enum,db_role.role) db_role.role = get_value_from_str(RC_enum,db_role.role)
db_role.status = get_value_from_str(RS_enum,db_role.status) db_role.status = get_value_from_str(RS_enum,db_role.status)
...@@ -232,7 +251,7 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet ...@@ -232,7 +251,7 @@ def patch_stationmeta(db: Session, description: str, station_id: int, stationmet
db.refresh(db_role) db.refresh(db_role)
role_id = db_role.id role_id = db_role.id
db.execute(insert(stationmeta_core_stationmeta_roles_table).values(station_id=station_id, role_id=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_changelog.new_value=oldvalue + str(r)
db.add(db_changelog) db.add(db_changelog)
db.commit() db.commit()
# store annotations and update association table # store annotations and update association table
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment