diff --git a/tests/test_stationmeta.py b/tests/test_stationmeta.py index e98d180984b52194de61c139f435a45bbf534e08..beba3499fc09fc8023317c03a12721d8c0f9bdd4 100644 --- a/tests/test_stationmeta.py +++ b/tests/test_stationmeta.py @@ -932,6 +932,33 @@ class TestApps: assert response_json['changelog'][1]['type_of_change'] == 'single value correction in metadata' + def test_patch_stationmeta_roles_and_annotations(self, client, db): +# {"annotations": [{"kind": "User", + response = client.patch("/stationmeta/SDZ54421?description=adding annotation text", + json={"stationmeta": + {"annotations": [{"kind": 0, + "text": "some annotation text", + "date_added": "2025-02-10 17:00", + "approved": True, + "contributor_id":1}] + } + }, + headers={"email": "s.schroeder@fz-juelich.de"} + ) + expected_status_code = 200 + assert response.status_code == expected_status_code + expected_resp = {'message': 'patched stationmeta record for station_id 2', 'station_id': 2} + response_json = response.json() + assert response_json == expected_resp + response = client.get(f"/stationmeta/id/{response_json['station_id']}") + response_json = response.json() + assert response_json['annotations'] == [{'id': 1, 'kind': 'user comment', 'text': 'some annotation text', 'date_added': '2025-02-10T17:00:00+00:00', 'approved': True, 'contributor_id': 1}] +# assert response_json['changelog'][1]['old_value'] == ... +# assert response_json['changelog'][1]['new_value'] == ... + assert response_json['changelog'][1]['author_id'] == 1 + assert response_json['changelog'][1]['type_of_change'] == 'single value correction in metadata' + + def test_delete_roles_from_stationmeta(self, client, db): response = client.patch("/stationmeta/delete_field/China11?field=roles", headers={"email": "s.schroeder@fz-juelich.de"}) diff --git a/toardb/stationmeta/schemas.py b/toardb/stationmeta/schemas.py index f544b448c3d43b18559a2ebf2cdc988167fe4746..fb2cd88b9e26c9aa3cc3327f5d7b4e4b7efb87f8 100644 --- a/toardb/stationmeta/schemas.py +++ b/toardb/stationmeta/schemas.py @@ -688,10 +688,27 @@ class StationmetaBase(StationmetaCoreBase): def order_changelog(cls, v): return sorted(v, key=lambda x: x.datetime) + @validator('roles') + def check_roles(cls, v): + if v == []: + return None + else: + return v -class StationmetaPatch(StationmetaCorePatch): - roles: List[StationmetaRolePatch] = None - annotations: List[StationmetaAnnotationPatch] = None + @validator('annotations') + def check_annotations(cls, v): + if v == []: + return None + else: + return v + + +class StationmetaPatch(StationmetaCoreCreate): + #roles: List[StationmetaRolePatch] = None + #annotations: List[StationmetaAnnotationPatch] = None + # just to get things working + roles: list = None + annotations: list = None aux_images: List[StationmetaAuxImagePatch] = None aux_docs: List[StationmetaAuxDocPatch] = None aux_urls: List[StationmetaAuxUrlPatch] = None diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py index 29670491c0357162fa2bc8ad986a2bea9a7b97e5..5dbf4e41ffd4aba4e9a95ed546ac845d67997c95 100644 --- a/toardb/stationmeta/stationmeta.py +++ b/toardb/stationmeta/stationmeta.py @@ -78,7 +78,8 @@ async def create_stationmeta_core(request: Request, # 3. update -@router.patch('/stationmeta/{station_code}', response_model=schemas.StationmetaPatch) +#@router.patch('/stationmeta/{station_code}', response_model=schemas.StationmetaPatch) +@router.patch('/stationmeta/{station_code}') def patch_stationmeta_core(request: Request, station_code: str, stationmeta: schemas.StationmetaPatch = Body(..., embed = True), @@ -108,7 +109,7 @@ def patch_stationmeta_core(request: Request, @router.patch('/stationmeta/delete_field/{station_code}', response_model=schemas.StationmetaPatch) -def patch_stationmeta_core(request: Request, +def delete_field_from_stationmeta_core(request: Request, station_code: str, field: str, access: dict = Depends(get_station_md_change_access_rights),