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

#9: changed patch syntax to standard way; fixed patch for adding roles

parent 5b112a36
Branches
No related tags found
No related merge requests found
Pipeline #47348 passed
...@@ -174,7 +174,7 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate): ...@@ -174,7 +174,7 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate):
db_stationmeta.coordinates = tmp_coordinates db_stationmeta.coordinates = tmp_coordinates
return db_stationmeta return db_stationmeta
def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: StationmetaPatch): def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatch):
stationmeta_dict = stationmeta.dict() stationmeta_dict = stationmeta.dict()
roles_data = stationmeta_dict.pop('roles', None) roles_data = stationmeta_dict.pop('roles', None)
annotations_data = stationmeta_dict.pop('annotations', None) annotations_data = stationmeta_dict.pop('annotations', None)
...@@ -187,10 +187,10 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -187,10 +187,10 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
# ==> the following two commands are not working # ==> the following two commands are not working
# ==> workaround # ==> workaround
# stationmeta_dict2 = {k: v for k, v in stationmeta_dict.items() if v is not None} # stationmeta_dict2 = {k: v for k, v in stationmeta_dict.items() if v is not None}
# db.query(models.StationmetaCore).filter(models.StationmetaCore.id == stationmeta_core_id).update(stationmeta_dict2) # db.query(models.StationmetaCore).filter(models.StationmetaCore.id == stationm_id).update(stationmeta_dict2)
db_obj = models.StationmetaCore(**stationmeta_dict) db_obj = models.StationmetaCore(**stationmeta_dict)
tmp_coordinates = db_obj.coordinates tmp_coordinates = db_obj.coordinates
db_stationmeta = db.query(models.StationmetaCore).get(stationmeta_core_id) db_stationmeta = db.query(models.StationmetaCore).get(station_id)
db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates) db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates)
for k, v in stationmeta_dict.items(): for k, v in stationmeta_dict.items():
if v is not None: if v is not None:
...@@ -207,12 +207,19 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -207,12 +207,19 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
db_object = get_unique_stationmeta_role(db, db_role.role, db_role.contact_id, db_role.status) db_object = get_unique_stationmeta_role(db, db_role.role, db_role.contact_id, db_role.status)
if db_object: if db_object:
role_id = db_object.id role_id = db_object.id
print("found role_id:",role_id)
else: 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.add(db_role)
db.commit() db.commit()
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=stationmeta_core_id, role_id=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() db.commit()
# store annotations and update association table # store annotations and update association table
if annotations_data: if annotations_data:
...@@ -227,13 +234,13 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -227,13 +234,13 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
db.commit() db.commit()
db.refresh(db_annotation) db.refresh(db_annotation)
annotation_id = db_annotation.id annotation_id = db_annotation.id
db.execute(insert(stationmeta_core_stationmeta_annotations_table).values(station_id=stationmeta_core_id, annotation_id=annotation_id)) db.execute(insert(stationmeta_core_stationmeta_annotations_table).values(station_id=station_id, annotation_id=annotation_id))
db.commit() db.commit()
# store aux_images # store aux_images
if aux_images_data: if aux_images_data:
for i in aux_images_data: for i in aux_images_data:
db_aux_image = models.StationmetaAuxImage(**i) db_aux_image = models.StationmetaAuxImage(**i)
db_aux_image.station_id = stationmeta_core_id db_aux_image.station_id = station_id
db.add(db_aux_image) db.add(db_aux_image)
db.commit() db.commit()
db.refresh(db_aux_image) db.refresh(db_aux_image)
...@@ -241,7 +248,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -241,7 +248,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
if aux_docs_data: if aux_docs_data:
for d in aux_docs_data: for d in aux_docs_data:
db_aux_doc = models.StationmetaAuxDoc(**d) db_aux_doc = models.StationmetaAuxDoc(**d)
db_aux_doc.station_id = stationmeta_core_id db_aux_doc.station_id = station_id
db.add(db_aux_doc) db.add(db_aux_doc)
db.commit() db.commit()
db.refresh(db_aux_doc) db.refresh(db_aux_doc)
...@@ -249,7 +256,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -249,7 +256,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
if aux_urls_data: if aux_urls_data:
for u in aux_urls_data: for u in aux_urls_data:
db_aux_url = models.StationmetaAuxUrl(**u) db_aux_url = models.StationmetaAuxUrl(**u)
db_aux_url.station_id = stationmeta_core_id db_aux_url.station_id = station_id
db.add(db_aux_url) db.add(db_aux_url)
db.commit() db.commit()
db.refresh(db_aux_url) db.refresh(db_aux_url)
...@@ -258,14 +265,14 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio ...@@ -258,14 +265,14 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio
db_global = models.StationmetaGlobal(**globalmeta_data) db_global = models.StationmetaGlobal(**globalmeta_data)
if db_global.climatic_zone: if db_global.climatic_zone:
db_global.climatic_zone = get_value_from_str(CZ_enum,db_global.climatic_zone) db_global.climatic_zone = get_value_from_str(CZ_enum,db_global.climatic_zone)
db_global.station_id = stationmeta_core_id db_global.station_id = station_id
db.add(db_global) db.add(db_global)
db.commit() db.commit()
db.refresh(db_global) db.refresh(db_global)
# store globalservice # store globalservice
if globalservice_data: if globalservice_data:
db_globalservice = models.StationmetaGlobalService(**globalservice) db_globalservice = models.StationmetaGlobalService(**globalservice)
db_globalservice.station_id = stationmeta_core_id db_globalservice.station_id = station_id
db.add(db_globalservice) db.add(db_globalservice)
db.commit() db.commit()
db.refresh(db_globalservice) db.refresh(db_globalservice)
......
...@@ -411,17 +411,13 @@ class StationmetaRoleBase(BaseModel): ...@@ -411,17 +411,13 @@ class StationmetaRoleBase(BaseModel):
class StationmetaRolePatch(BaseModel): class StationmetaRolePatch(BaseModel):
role: str = None role: str
status: str = None status: str
contact: Contact = None # at the moment contact_id has to be given...
# ==> in the future: give unique contact_email
@validator('role') # patching stationmeta should not result in creating new contacts!
def check_role(cls, v): # ==> still to do: check, whether contact already exists (otherwise patching cannot be done)
return tuple(filter(lambda x: x.value == int(v), RC_enum))[0].string contact_id: int
@validator('status')
def check_status(cls, v):
return tuple(filter(lambda x: x.value == int(v), RS_enum))[0].string
class Config: class Config:
orm_mode = True orm_mode = True
......
...@@ -59,15 +59,9 @@ def create_stationmeta_core(stationmeta: schemas.StationmetaCreate = Body(..., e ...@@ -59,15 +59,9 @@ def create_stationmeta_core(stationmeta: schemas.StationmetaCreate = Body(..., e
raise HTTPException(status_code=400, detail="Station already registered.") raise HTTPException(status_code=400, detail="Station already registered.")
return crud.create_stationmeta(db=db, stationmeta=stationmeta) return crud.create_stationmeta(db=db, stationmeta=stationmeta)
@router.patch('/stationmeta/', response_model=schemas.StationmetaPatch) @router.patch('/stationmeta/{station_code}', response_model=schemas.StationmetaPatch)
def patch_stationmeta_core(stationmeta: schemas.StationmetaPatch = Body(..., embed = True), db: Session = Depends(get_db)): def patch_stationmeta_core(station_code: str, stationmeta: schemas.StationmetaPatch = Body(..., embed = True), db: Session = Depends(get_db)):
found = False db_stationmeta = crud.get_stationmeta(db, station_code=station_code)
for station_code in stationmeta.codes: if db_stationmeta is None:
if not found: raise HTTPException(status_code=404, detail="Station for patching not found.")
db_stationmeta_core= crud.get_stationmeta_core(db, station_code=station_code) return crud.patch_stationmeta(db=db, station_id=db_stationmeta.id, stationmeta=stationmeta)
if db_stationmeta_core:
found = True
if found:
return crud.patch_stationmeta(db=db, stationmeta_core_id=db_stationmeta_core.id, stationmeta=stationmeta)
else:
raise HTTPException(status_code=400, detail="Station not found!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment