diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py index 8584be453e99f1ef66e9740a4bff92dd95837480..3ffc22ece1b928ed6ebebc6189c95bd2ea9477b2 100644 --- a/toardb/stationmeta/crud.py +++ b/toardb/stationmeta/crud.py @@ -174,7 +174,7 @@ def create_stationmeta(db: Session, stationmeta: StationmetaCreate): db_stationmeta.coordinates = tmp_coordinates 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() roles_data = stationmeta_dict.pop('roles', None) annotations_data = stationmeta_dict.pop('annotations', None) @@ -187,10 +187,10 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio # ==> the following two commands are not working # ==> workaround # 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) 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) for k, v in stationmeta_dict.items(): if v is not None: @@ -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) 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 - 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() # store annotations and update association table if annotations_data: @@ -227,13 +234,13 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio db.commit() db.refresh(db_annotation) 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() # store aux_images if aux_images_data: for i in aux_images_data: 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.commit() db.refresh(db_aux_image) @@ -241,7 +248,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio if aux_docs_data: for d in aux_docs_data: 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.commit() db.refresh(db_aux_doc) @@ -249,7 +256,7 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio if aux_urls_data: for u in aux_urls_data: 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.commit() db.refresh(db_aux_url) @@ -258,14 +265,14 @@ def patch_stationmeta(db: Session, stationmeta_core_id: int, stationmeta: Statio db_global = models.StationmetaGlobal(**globalmeta_data) if 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.commit() db.refresh(db_global) # store globalservice if globalservice_data: db_globalservice = models.StationmetaGlobalService(**globalservice) - db_globalservice.station_id = stationmeta_core_id + db_globalservice.station_id = station_id db.add(db_globalservice) db.commit() db.refresh(db_globalservice) diff --git a/toardb/stationmeta/schemas.py b/toardb/stationmeta/schemas.py index 20d6a2aa1c2fc4f1e36c28c1c2ac9a07a0b6c7b5..67266c6a81cd38427c8934c326cd2a24c7feddfb 100644 --- a/toardb/stationmeta/schemas.py +++ b/toardb/stationmeta/schemas.py @@ -411,17 +411,13 @@ class StationmetaRoleBase(BaseModel): class StationmetaRolePatch(BaseModel): - role: str = None - status: str = None - contact: Contact = None - - @validator('role') - def check_role(cls, v): - return tuple(filter(lambda x: x.value == int(v), RC_enum))[0].string - - @validator('status') - def check_status(cls, v): - return tuple(filter(lambda x: x.value == int(v), RS_enum))[0].string + role: str + status: str +# at the moment contact_id has to be given... +# ==> in the future: give unique contact_email +# patching stationmeta should not result in creating new contacts! +# ==> still to do: check, whether contact already exists (otherwise patching cannot be done) + contact_id: int class Config: orm_mode = True diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py index 86c5eb95ada246cf30726c8d12d4bd8391c4fcb8..e3161b459925fb836afca9b2d74550b684502110 100644 --- a/toardb/stationmeta/stationmeta.py +++ b/toardb/stationmeta/stationmeta.py @@ -59,15 +59,9 @@ def create_stationmeta_core(stationmeta: schemas.StationmetaCreate = Body(..., e raise HTTPException(status_code=400, detail="Station already registered.") return crud.create_stationmeta(db=db, stationmeta=stationmeta) -@router.patch('/stationmeta/', response_model=schemas.StationmetaPatch) -def patch_stationmeta_core(stationmeta: schemas.StationmetaPatch = Body(..., embed = True), db: Session = Depends(get_db)): - found = False - for station_code in stationmeta.codes: - if not found: - db_stationmeta_core= crud.get_stationmeta_core(db, station_code=station_code) - 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!") +@router.patch('/stationmeta/{station_code}', response_model=schemas.StationmetaPatch) +def patch_stationmeta_core(station_code: str, stationmeta: schemas.StationmetaPatch = Body(..., embed = True), 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 patching not found.") + return crud.patch_stationmeta(db=db, station_id=db_stationmeta.id, stationmeta=stationmeta)