diff --git a/tests/test_data.py b/tests/test_data.py index c86186ecbfd0facb099e688b00e234549660b617..2bf517a6a6809aa7583b4846a1811fb4ca6115de 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -211,17 +211,14 @@ class TestApps: 'coordinate_validation_status': 'not checked', 'country': 'China', 'state': 'Beijing Shi', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], - 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': None, 'annotations': [], 'changelog': []}, + 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'changelog': []}, 'variable': {'name': 'toluene', 'longname': 'toluene', 'displayname': 'Toluene', 'cf_standardname': 'mole_fraction_of_toluene_in_air', 'units': 'nmol mol-1', 'chemical_formula': 'C7H8', 'id': 7}, 'programme': {'id': 0, 'name': '', 'longname': '', 'homepage': '', 'description': ''}, 'roles': [{'id': 2, 'role': 'resource provider', 'status': 'active', - 'contact': {'id': 4, 'person': None, - 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', + 'contact': {'id': 4, 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', 'kind': 'government', 'city': 'Dessau-Roßlau', 'postcode': '06844', 'street_address': 'Wörlitzer Platz 1', 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de', 'contact_url': 'mailto:immission@uba.de'}}}], - 'changelog': None, 'citation': 'Umweltbundesamt: time series of toluene at Shangdianzi, accessed from the TOAR database on 2023-07-28 12:00:00'}, 'data': [{'datetime': '2012-12-16T21:00:00+00:00', 'value': 21.581, 'flags': 'OK validated verified', 'version': '1.0', 'timeseries_id': 1}, {'datetime': '2012-12-16T22:00:00+00:00', 'value': 13.734, 'flags': 'OK validated verified', 'version': '1.0', 'timeseries_id': 1}, @@ -290,12 +287,9 @@ class TestApps: 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some ' 'more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': []}, 'variable': {'name': 'toluene', 'longname': 'toluene', @@ -313,7 +307,6 @@ class TestApps: 'role': 'resource provider', 'status': 'active', 'contact': {'id': 4, - 'person': None, 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', @@ -324,7 +317,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de', 'contact_url': 'mailto:immission@uba.de'}}}], - 'changelog': None, 'citation': 'Umweltbundesamt: time series of toluene at ' 'Shangdianzi, accessed from the TOAR database on ' '2023-07-28 12:00:00', @@ -380,12 +372,9 @@ class TestApps: 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some ' 'more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': []}, 'variable': {'name': 'toluene', 'longname': 'toluene', @@ -403,7 +392,6 @@ class TestApps: 'role': 'resource provider', 'status': 'active', 'contact': {'id': 4, - 'person': None, 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', @@ -414,7 +402,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de', 'contact_url': 'mailto:immission@uba.de'}}}], - 'changelog': None, 'citation': 'Umweltbundesamt: time series of toluene at ' 'Shangdianzi, accessed from the TOAR database on ' '2023-07-28 12:00:00', @@ -473,8 +460,8 @@ class TestApps: '# "additional_metadata": {\n', '# "dummy_info": "Here is some more information about the station"\n', '# },\n', - '# "roles": [],\n', - '# "annotations": [],\n', + '# "roles": null,\n', + '# "annotations": null,\n', '# "aux_images": [],\n', '# "aux_docs": [],\n', '# "aux_urls": [],\n', @@ -644,16 +631,15 @@ class TestApps: 'coordinate_validation_status': 'not checked', 'country': 'China', 'state': 'Beijing Shi', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': None, 'annotations': [], 'roles': [], 'changelog': []}, + 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'changelog': []}, 'variable': {'name': 'toluene', 'longname': 'toluene', 'displayname': 'Toluene', 'cf_standardname': 'mole_fraction_of_toluene_in_air', 'units': 'nmol mol-1', 'chemical_formula': 'C7H8', 'id': 7}, 'programme': {'id': 0, 'name': '', 'longname': '', 'homepage': '', 'description': ''}, 'roles': [{'id': 2, 'role': 'resource provider', 'status': 'active', - 'contact': {'id': 4, 'person': None, - 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', + 'contact': {'id': 4, 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', 'kind': 'government', 'city': 'Dessau-Roßlau', 'postcode': '06844', 'street_address': 'Wörlitzer Platz 1', 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de', 'contact_url': 'mailto:immission@uba.de'}}}], - 'changelog': None, 'citation': 'Umweltbundesamt: time series of toluene at Shangdianzi, accessed from the TOAR database on 2023-07-28 12:00:00'}, + 'citation': 'Umweltbundesamt: time series of toluene at Shangdianzi, accessed from the TOAR database on 2023-07-28 12:00:00'}, 'data': [{'datetime': '2012-12-16T21:00:00+00:00', 'value': 21.581, 'flags': 'OK validated verified', 'timeseries_id': 1, 'version': '1.0'}, {'datetime': '2012-12-16T22:00:00+00:00', 'value': 13.734, 'flags': 'OK validated verified', 'timeseries_id': 1, 'version': '1.0'}, {'datetime': '2012-12-16T23:00:00+00:00', 'value': 13.734, 'flags': 'OK validated verified', 'timeseries_id': 1, 'version': '1.0'}, @@ -715,12 +701,9 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': []}, 'variable': {'name': 'o3', 'longname': 'ozone', @@ -738,7 +721,6 @@ class TestApps: 'role': 'resource provider', 'status': 'active', 'contact': {'id': 5, - 'person': None, 'organisation': {'id': 2, 'name': 'FZJ', 'longname': 'Forschungszentrum Jülich', @@ -749,7 +731,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de', 'contact_url': 'mailto:toar-data@fz-juelich.de'}}}], - 'changelog': None, 'citation': 'Forschungszentrum Jülich: time series of o3 at Test_China, accessed from the TOAR database on 2023-07-28 12:00:00', 'attribution': 'Test-Attributions to be announced', 'license': 'This data is published under a Creative Commons Attribution 4.0 International (CC BY 4.0). https://creativecommons.org/licenses/by/4.0/'}, @@ -795,12 +776,9 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': []}, 'variable': {'name': 'o3', 'longname': 'ozone', @@ -818,7 +796,6 @@ class TestApps: 'role': 'resource provider', 'status': 'active', 'contact': {'id': 5, - 'person': None, 'organisation': {'id': 2, 'name': 'FZJ', 'longname': 'Forschungszentrum Jülich', @@ -829,7 +806,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de', 'contact_url': 'mailto:toar-data@fz-juelich.de'}}}], - 'changelog': None, 'citation': 'Forschungszentrum Jülich: time series of o3 at Test_China, accessed from the TOAR database on 2023-07-28 12:00:00', 'attribution': 'Test-Attributions to be announced', 'license': 'This data is published under a Creative Commons Attribution 4.0 International (CC BY 4.0). https://creativecommons.org/licenses/by/4.0/'}, @@ -881,12 +857,9 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': []}, 'variable': {'name': 'o3', 'longname': 'ozone', @@ -904,7 +877,6 @@ class TestApps: 'role': 'resource provider', 'status': 'active', 'contact': {'id': 5, - 'person': None, 'organisation': {'id': 2, 'name': 'FZJ', @@ -916,7 +888,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de', 'contact_url': 'mailto:toar-data@fz-juelich.de'}}}], - 'changelog': None, 'citation': 'Forschungszentrum Jülich: time series of o3 at ' 'Test_China, accessed from the TOAR database on ' '2023-07-28 12:00:00', @@ -1046,12 +1017,9 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None, 'changelog': [] }, 'variable': {'name': 'toluene', @@ -1069,7 +1037,7 @@ class TestApps: 'description': '' }, 'roles': [{'id': 2, 'role': 'resource provider', 'status': 'active', - 'contact': {'id': 4, 'person': None, + 'contact': {'id': 4, 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', diff --git a/tests/test_search.py b/tests/test_search.py index 5117b491e5280d35c8cd0f0cfe973bb3e2424130..11636578c0d15db010b6a2a6fdc1a251f8df8a16 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -201,8 +201,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -297,8 +295,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -403,7 +399,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -473,7 +468,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -559,7 +553,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -629,7 +622,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -750,7 +742,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -820,7 +811,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -890,7 +880,6 @@ class TestApps: 'country': 'China', 'state': 'Shandong Sheng', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, diff --git a/tests/test_stationmeta.py b/tests/test_stationmeta.py index 8bdc98ae9b94aad2e5f8ecca0f12038ab6a1f67a..e35338e977c002019175136a29f1f57955aa1e02 100644 --- a/tests/test_stationmeta.py +++ b/tests/test_stationmeta.py @@ -31,6 +31,14 @@ from toardb.test_base import ( get_test_engine, test_db_session as db, ) +from datetime import datetime +from unittest.mock import patch + +# only datetime.now needs to be overridden because otherwise daterange-arguments would be provided as MagicMock-objects! +class FixedDatetime(datetime): + @classmethod + def now(cls, tz=None): + return datetime(2023, 7, 28, 12, 0, 0) class TestApps: @@ -189,7 +197,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de', 'contact_url': 'mailto:immission@uba.de'}}], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -275,7 +282,6 @@ class TestApps: 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de', 'contact_url': 'mailto:toar-data@fz-juelich.de'}}], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -323,8 +329,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -375,7 +379,7 @@ class TestApps: 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], + 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -603,7 +607,7 @@ class TestApps: 'id': 2, 'role': 'resource provider', 'status': 'active' }], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], + 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, 'dominant_ecoregion_year2017': '-1 (undefined)', @@ -860,6 +864,7 @@ class TestApps: assert response_json == expected_resp response = client.get(f"/stationmeta/id/{response_json['station_id']}") response_json = response.json() + print(response_json) # just check special changes assert response_json['name'] == 'TTTT95TTTT' assert response_json['changelog'][1]['old_value'] == "{'name': 'Shangdianzi'}" @@ -968,23 +973,45 @@ class TestApps: headers={"email": "s.schroeder@fz-juelich.de"}) expected_status_code = 200 assert response.status_code == expected_status_code - expected_resp = {'codes': ['China11'], + # Database defaults cannot be patched within pytest + patched_response = response.json() + patched_response["changelog"][-1]["datetime"] = "" + expected_resp = {'id': 1, + 'codes': ['China11'], 'name': 'Mount Tai', 'coordinates': {'lat': 36.256, 'lng': 117.106, 'alt': 1534.0}, - 'coordinate_validation_status': '0', - 'country': '48', + 'coordinate_validation_status': 'not checked', + 'country': 'China', 'state': 'Shandong Sheng', - 'type': '0', - 'type_of_area': '0', - 'timezone': '310', + 'type': 'unknown', + 'type_of_area': 'unknown', + 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], - 'globalmeta': None} - assert response.json() == expected_resp + 'changelog': [ + { + 'author_id': 1, + 'datetime': '2023-07-05T08:23:04.551645+00:00', + 'description': 'station created', + 'new_value': '', + 'old_value': '', + 'station_id': 1, + 'type_of_change': 'created', + }, + { + 'author_id': 1, + 'datetime': '', + 'description': 'delete field roles', + 'new_value': "'roles': []", + 'old_value': "'roles': '[]'", + 'station_id': 1, + 'type_of_change': 'single value correction in metadata', + }, + ], + } + assert patched_response == expected_resp def test_delete_field_station_not_found(self, client, db): diff --git a/tests/test_timeseries.py b/tests/test_timeseries.py index b27d5fceb2a74c12a695b1512a4f5312d165439d..c30e6b44e59d952438957a5102defc79f363d09f 100644 --- a/tests/test_timeseries.py +++ b/tests/test_timeseries.py @@ -205,8 +205,6 @@ class TestApps: 'additional_metadata': {'dummy_info': 'Here is some more ' 'information about the ' 'station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -314,8 +312,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -401,7 +397,6 @@ class TestApps: 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, @@ -621,8 +616,7 @@ class TestApps: 'state': 'Beijing Shi', 'type': 'unknown', 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], + 'aux_images': [], 'aux_docs': [], 'aux_urls': [], 'globalmeta': {'climatic_zone_year2016': '6 (warm temperate dry)', 'distance_to_major_road_year2020': -999.0, 'dominant_ecoregion_year2017': '-1 (undefined)', @@ -718,8 +712,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {'dummy_info': 'Here is some more information about the station'}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], @@ -816,8 +808,6 @@ class TestApps: 'type_of_area': 'unknown', 'timezone': 'Asia/Shanghai', 'additional_metadata': {}, - 'roles': [], - 'annotations': [], 'aux_images': [], 'aux_docs': [], 'aux_urls': [], diff --git a/toardb/data/data.py b/toardb/data/data.py index 6f5969fc9ed7536859428e26a2b393caea298d2d..a8a893ef7c455b9e47fb8dfd97a2a193aacc214b 100644 --- a/toardb/data/data.py +++ b/toardb/data/data.py @@ -33,7 +33,7 @@ def get_all_data(offset: int = 0, limit: int = 100, db: Session = Depends(get_db #get all data of one timeseries -@router.get('/data/timeseries/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True) +@router.get('/data/timeseries/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True, response_model_exclude_none=True) def get_data(timeseries_id: int, request: Request, db: Session = Depends(get_db)): db_data = crud.get_data(db, timeseries_id=timeseries_id, path_params=request.path_params, query_params=request.query_params) if db_data is None: @@ -49,7 +49,7 @@ def get_version(timeseries_id: int, request: Request, db: Session = Depends(get_ #get all data of one timeseries -@router.get('/data/timeseries/id/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True) +@router.get('/data/timeseries/id/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True, response_model_exclude_none=True) def get_data2(timeseries_id: int, request: Request, db: Session = Depends(get_db)): db_data = crud.get_data(db, timeseries_id=timeseries_id, path_params=request.path_params, query_params=request.query_params) if db_data is None: @@ -58,7 +58,7 @@ def get_data2(timeseries_id: int, request: Request, db: Session = Depends(get_db #get all data of one timeseries (including staging data) -@router.get('/data/timeseries_with_staging/id/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True) +@router.get('/data/timeseries_with_staging/id/{timeseries_id}', response_model=schemas.Composite, response_model_exclude_unset=True, response_model_exclude_none=True) def get_data_with_staging(timeseries_id: int, flags: str = None, format: str = 'json', db: Session = Depends(get_db)): db_data = crud.get_data_with_staging(db, timeseries_id=timeseries_id, flags=flags, format=format) if db_data is None: diff --git a/toardb/stationmeta/crud.py b/toardb/stationmeta/crud.py index a8e61f71b2ab5721da8e7b6397e4508611f2bcb1..2af163718652faad25e01620250cb9c202f515be 100644 --- a/toardb/stationmeta/crud.py +++ b/toardb/stationmeta/crud.py @@ -605,13 +605,15 @@ def delete_stationmeta_field(db: Session, station_id: int, field: str, author_id # id can never be deleted (and of course also not changed)!!! # there are mandatory fields (from stationmeta_core), that cannot be deleted! # --> set these to their default value + new_value = "" field_table = {'roles': stationmeta_core_stationmeta_roles_table, 'annotations': stationmeta_core_stationmeta_annotations_table} # 'aux_images': stationmeta_core_stationmeta_aux_images_table, # 'aux_docs': stationmeta_core_stationmeta_aux_docs_table, # 'aux_urls': stationmeta_core_stationmeta_aux_urls_table, - if ((field == 'roles') or (field == 'annotations')): + if (field in field_table): db.execute(delete(field_table[field]).where(field_table[field].c.station_id==station_id)) + new_value = f"'{field}': []" # problem with automatic conversion of coordinates (although not explicitly fetched from database) # ==> next two lines are a workaround db_stationmeta = db.query(models.StationmetaCore).get(station_id) @@ -622,11 +624,11 @@ def delete_stationmeta_field(db: Session, station_id: int, field: str, author_id description=f"delete field {field}" old_value = get_field_from_record(db, station_id, field, db_stationmeta) db_changelog = StationmetaChangelog(description=description, station_id=station_id, author_id=author_id, type_of_change=type_of_change, - old_value=old_value, new_value="") + old_value=old_value, new_value=new_value) db.add(db_changelog) db.commit() # there's a mismatch with coordinates --> how to automatically switch back and forth?! db_stationmeta.coordinates = tmp_coordinates # hotfix: db_stationmeta.global needs to be retranslated to the dict that is understood by StationmetaGlobal - db_stationmeta.globalmeta= None + db_stationmeta.globalmeta = None return db_stationmeta diff --git a/toardb/stationmeta/stationmeta.py b/toardb/stationmeta/stationmeta.py index 5dbf4e41ffd4aba4e9a95ed546ac845d67997c95..2809f263507e661a052009f99d5637e75c252995 100644 --- a/toardb/stationmeta/stationmeta.py +++ b/toardb/stationmeta/stationmeta.py @@ -38,7 +38,7 @@ def get_stationmeta(station_code: str, fields: str = None, db: Session = Depends #get all core metadata of one station (given its ID) -@router.get('/stationmeta/id/{station_id}', response_model=schemas.Stationmeta) +@router.get('/stationmeta/id/{station_id}', response_model=schemas.Stationmeta, response_model_exclude_none=True, response_model_exclude_unset=True) def get_stationmeta_by_id(station_id: int, db: Session = Depends(get_db)): db_stationmeta = crud.get_stationmeta_by_id(db, station_id=station_id) if db_stationmeta is None: @@ -108,7 +108,7 @@ def patch_stationmeta_core(request: Request, raise HTTPException(status_code=401, detail="Unauthorized.") -@router.patch('/stationmeta/delete_field/{station_code}', response_model=schemas.StationmetaPatch) +@router.patch('/stationmeta/delete_field/{station_code}', response_model=schemas.StationmetaBase, response_model_exclude_none=True, response_model_exclude_unset=True) def delete_field_from_stationmeta_core(request: Request, station_code: str, field: str,